When you use edmgen.exe with /mode:FullGeneration or /mode:EntityClassGeneration you get a file emitted with a bunch of generated code in it.
In particular, that generated code contains a class derived from ObjectContext (such as NorthwindContext) and you also get a bunch of classes that represent your entity types from the EDM (tables and views from the store).
Looking at the code-gen'd file itself it has;
- Assembly level attributes from the System.Data.Objects.DataClasses namespace such as EdmSchema and EdmRelationship. We saw these a little bit back in this post.
Looking in a little more detail at NorthwindContext what do you get;
- Public partial class derived from ObjectContext.
- Constructors that mirror the base class constructors.
- Properties that give quick and easy access to EntitySets such as;
[global::System.ComponentModel.BrowsableAttribute(false)]
public global::System.Data.Objects.ObjectQuery<Customers> Customers
{
get
{
if ((this._Customers == null))
{
this._Customers = base.CreateQuery<Customers>("[Customers]");
}
return this._Customers;
}
}
private global::System.Data.Objects.ObjectQuery<Customers> _Customers = null;
- Methods that allow quick and easy additions to be made to EntitySets such as;
public void AddToCustomers(Customers customers)
{
base.AddObject("Customers", customers);
}
so that's all pretty self-explanatory and I like the fact that the the properties such as Customers above are explicitly calling functions that I can see whereas in (say) the LINQ to SQL world you have "magic properties" that are somehow assigned values by the base class which is pretty clever but a little opaque and (initially) a bit disconcerting :-)
And then looking at one of the entity types such as Customers what do you get there;
- Public partial class derived from EntityObject. What does EntityObject look like? I dropped in a class diagram below - there's quite a lot to it when you chase down the base class and the 3 interfaces involved so I'll revisit this in another post adding more to that post where I talked a little about using a custom type.
- Marked as Serializable
- Marked with a DataContract attribute.
- Marked with an EdmEntityType attribute.
- Then we have public properties on those types corresponding to what we've defined in the EDM and;
- Marked with EdmScalarProperty or EdmRelationshipNavigationProperty (and perhaps others)
- Marked up with DataMember attributes for WCF for scalar properties or [XmlIgnore,SoapIgnore] for navigation properties.
So, nothing too magical going on inside of that generated code but very nice not to have to type it all in yourself for those (probably most) occasions where you'd use it rather than bake your own.
Posted
Mon, Aug 27 2007 4:31 PM
by
mtaulty