LINQ To SQL - Going POCO and more……! Part 2.
In part 1 of this post I made the code available for a framework that I named Golden Gate, which provided the pieces to use a POCO approach in using the LINQ To SQL OR/M along with Specifications (for dynamic querying and equality testing) and Fetching Strategies (providing flexible load options to suit the eager or lazy load requirements). What was missing in the code download was an example that utilised the DataContext classes ability to provide lazy loading or deferred loading as it terms it. The code below demonstrates how to create your POCO entities to allow for this.
//For the One to Many side of a relationship private EntitySet<RingtailPage> _pages; public IList<RingtailPage> Pages { get { return _pages; } set { _pages.Assign(value); } } //For the Many to One side of a relationship private EntityRef<RingtailDocument> _document; public RingtailDocument Document { get { return _document.Entity; } set { _document.Entity = value; } }
Whilst the use of the EntitySet<T> and EntityRef<T> dont exactly fit the purist POCO approach, it’s a small price to pay when you consider that access to them are wrapped by an accessor mutator that exposes the Entity in question or an IList<T> where T is the entity in question. Matthew Charles explains why EntitySet<T> and EntityRef<T> are required for LINQ To SQL lazy loading.
No comments yet. Be the first.
Leave a reply








