Monday, August 6, 2007

Join Tables and Domain Models: The LINQ-Way

I blogged about Join Tables and Domain Models before, where I showed different ways to deal with 'association-entities' in Ruby and C# 2.0.

Now in C# 3.0, LINQ offers another interesting way to express a domain model.

Assume the following object model:

ClassDiagram1

Article has associated Users through the 'association-entities' Readings. Now LINQ allows us to express this association in another elegant way:

private readonly List<Reading> readings = new List<Reading>();
public IEnumerable<User> Readers {
   get {
       return from reading in readings
                  select reading.User;
       }
}

When I think about LINQ its mostly in the context of querying data, which is usually a common task in the data access layer. There LINQ allows us to specify what part of a domain model should be loaded.
But the above example shows us, that LINQ can be used in other scenarios, even in expressing our domain model itself.

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...