I’ve been at the patterns & practices Summit here in Sydney this week. Today I made my contribution and presented an session on LINQ – or – Language INtegrated Query. There were a few things that I really didn’t get a chance to cover in my session in much detail. The first was the way that associations between entities in DLINQ work, and the other was how you can intercept DLINQ calls before they get sent to the database and replace them with your own logic.

What I want to do in this post is just provide some quick coverage on how you can do this by sub-classing the DataContext class and implementing some methods and attaching some meta data. But before I can go into detail – we need some starter code:

DLINQFirstCut

Now – in order to intercept calls (for updates for example) you need to create your own sub-class of the DataContext class and add a public method to it which takes two instances of the entity type you are trying to persist. You then need to decorate it with the UpdateMethod attribute – this is what tells the context object to call the method on updates when the SubmitChanges method is called.

DLINQCustomContext

There are three different attributes that can be applied to methods:

  • InsertMethodAttribute
  • DeleteMethodAttribute
  • UpdateMethodAttribute

I’m not exactly why there isn’t a matching SelectMethodAttribute, although there are other ways to get the data out of the database – for example the ExecuteQuery method in which you specify the type parameter for the entity and pass in the query text and parameters.