Celebrating the Death of DoFinalConstruction()
In a previous post I explained how the Entity Framework supported custom construction of entity objects in Orcas beta 1 and before. Unfortunately, the whole mechanism was way too complex and brittle. So, we went back to the drawing board (or the whiteboard as the case may be) and gave more thought to what we were really trying to accomplish. At that point someone, Pablo Castro I think, pointed out that if a class has no constructors the CLR will automatically supply a default constructor which takes no parameters. Further, this default constructor is actually sufficient for the framework—the ObjectMaterializer’s only requirement on the constructors for the objects it creates is that there be a constructor which takes no parameters.[i]
So the only change required to simplify the custom construction logic story was to modify EdmGen’s class generation system to no longer output any constructors for entity classes. Now if you want to add construction logic, you need only implement a constructor in the partial class of your entity object class just like you would for any class. The only requirement for the constructors is that if you implement a constructor which takes parameters you must be sure to also implement one which takes no parameters (because the CLR will not supply a default constructor if you supply any constructors and the ObjectMaterializer requires a constructor with no parameters).
So in the next CTP (after beta 1), the Entity Framework no longer defines the method DoFinalConstruction or requires any of the mechanisms described in the previous article. Good bye DoFinalConstuction() and good riddance.
[i] Note: We are evaluating a further change to make the ObjectMaterializer even more flexible so that it could construct object instances using factory methods and not even require a parameterless constructor, but we have not yet committed to that change, and it certainly won’t be in the next CTP.