I just installed the July CTP of Visual Studio on my machine in order to check out changes and updates to some of the problems I've been having in Beta 2. It looks like there are some changes, but nothing major. I plunked on my West Wind Web Store project which has been running well on 2.0 and let ‘er rip. Aside from a handful of interface and class updates the update went well.

 

There are two things I'm having problems with:

Script Callbacks which have changed considerably (grrrr...)
Generics and Constraints
 

I'll talk about the script callback stuff in another post. Here's some info on the Generics stuff:

 

I’ve been lightly experimenting with Generics to manage my DataRow mapping Entity scheme that I use to provide strongly typed access to content retrieved from the database. Basically this scheme maps its content to an underlying datarow. In 1.1 this had to be done via an explicit implementation to get the proper type returned so there was a special Entity property that every class had to implement to map the proper type. It was pretty simple, but still sort of a hassle.

 

In 2.0 I was able to replace that code with a generic version that looks like this:

 

public class wwBusiness<EntityType> : wwBusiness

{

      // *** To implement an Entity mechanism use code like this:

      public EntityType Entity

      {

            get

            {

                  if (this.m_GenericEntity == null || this.IsEntityDirty)

                        return (EntityType) this.GetGenericEntity(false);

 

                  return (EntityType) this.m_GenericEntity;

            }

            set { this.m_GenericEntity = value; }

      }

      public EntityType m_GenericEntity = default(EntityType);

}

 

So the only thing the instance class now has to do is derive from this generic class like this:

 

public class busInvoiceGeneric : wwBusiness<wws_invoiceRow>

 

where wws_invoiceRow is the Entity type. From there on in the behavior is exactly the same as the 1.1 application. Much simpler. In the 1.1 implementation each class pretty much had to implement this property setup.

 

But in the July CTP there’s a problem with the code above. This code:

 

if (this.m_GenericEntity == null)

 

doesn’t work any longer. The problem here is that the m_GenericEntity is fo type EntityType which is the generic template type passed into the class. The Generic implementation can make no assumption about the type passed so it can’t assume the type is a reference type that can be null. In theory somebody could be passing in an int or bool value both of which can’t be null.

 

Luckily there’s a workaround for this behavior in the form of Constraints in Generics. Constraints let you specify what type of type (<g>) – class or struct – can be used, what classes and interfaces are allowed for the generic type parameter.

public class wwBusiness<EntityType> : wwBusiness
      
where EntityType: class, new()

 

If you're new to Generics (like me <g>) you might notice the default() keyword which is used to return a default value for a type. The issue is that value and reference types have a different idea of what a default value. So for a reference type you get back null and for a value type you get back well a value. For numerics that value is 0, for bool it's false.

 

BTW, the way Generics works here is really very, very useful. I’m still trying to wrap my head around how to best apply Generics in my existing code, but the above is an example where Generics drastically reduces the requirements on subclass implementations. Rather than having to explicitly implement anything the type value is specified in the class definition. Even nicer by using the Generic type as a base class and then inheriting from it, this code change doesn’t even affect any of my code in the framework. Well, the Factory classes have to be changed to use the Generic versions of the class, but otherwise the rest of the framework can happily run with the new code.