Carlos Ble

Carlos Ble

I am a professional software developer, I solve problems.

I also teach and mentor developers to build better software.

Developing software since 2001.

Can I help you?

  • Do you need high quality tailor-made software?
  • Need training on TDD, clean code or refactoring?
  • Is it risky? do you need advise?
  • May I pair with you to write better code?

Events

Upcoming public training courses:

  1. [Online - en Español] 25 y 26 de Junio
    Test Doubles con JavaScript - Online
  2. [in English] July 7, 8 & 9
    TDD (open to the public) - Tenerife, Canary Islands
  3. [en Español] 14 y 15 Julio
    TDD (en abierto) - Gran Canaria, Canary Islands
  4. [in English] October 13, 14 & 15
    TDD (open to the public) - London, UK
  5. [en Español] 29, 30 y 31 de Octubre.
    TDD (en abierto) - Madrid, Spain

Conferences:

  1. I'll be at SocratesUK 2014
  2. I'll be at the London Test Gathering Workshops.

Linq to Sql Horrors

Linq to Sql is a kind of ORM for .Net by Microsoft. Working with Linq to Sql can be a nightmare.

  • The basics:

In order to send or retrieve objects to or from the database, you need a DataContext instance. This is an instance of a class that inherits DataContext. It is created by Visual Studio with drag and drop. One context is generated for every database you access to. This generated context is inside a .cs file which contains also the classes that map database tables. The mapping is contained in a .dbml file (xml) generated in the same folder. You should not change any of this generated files. If a table changes in the database you must regenerate with the IDE.

Select example:
If the name of the database is ProductionDB, the DataContext will be ProductionDBDataContext.

  1. var context = new ProductionDBDataContext(connectionString);
  2. var userEntity = (from user in context.Users
  3. where user.name == "carlos"
  4. select user).First();

Insert example:

  1. var context = new ProductionDBDataContext(connectionString);
  2. var user = new User();
  3. user.name = "manolo"
  4. context.Users.InsertOnSubmit(user);
  5. context.SubmitChanges();
  • The main issue:

The same context instance have to be used for all the operations performed on a relational object instance. This would throw an exception:

  1. var context = new ProductionDBDataContext(connectionString);
  2. var userEntity = (from user in context.Users
  3. where user.name == "carlos"
  4. select user).First();
  5. // Imagine this is part of another method in another place
  6. // but with the same userEntity instance that was passed through:
  7. userEntity.age = "30";
  8. var otherContextInstance = new ProductionDBDataContext(connectionString);
  9. otherContextInstance.Users.InsertOnSubmit(user);
  10. otherContextInstance.SubmitChanges();

So you can't just pass relational objects from one method or layer to anoter, you have to always cope with the "same context instance problem".

  •  Solutions
  1. Pass the context instance along with the relational objects, in all the places where you need to perform database access:   someMethod(user, context);
    Bad choice:
    This leads to spaguetti code quickly. Why  does my business layer have to know about a database context?
  2. Ok so we can make a Singleton for the context instance and make sure it is the same for all the application:
    public static ProductionDBDataContext AppContext = new ProductionDBDataContext(connString);
    Bad choice: When more than one request hit the server at the same time you get concurrency problems. The context will be trying to insert in one thread while trying to update or select in other. Weird race conditions happens and exceptions are thrown.
  3. Create a context for every thread. Every thread will have its own context Singleton. We found this solution thanks to Rick Strahl and his great post. However Rick's code can be simplified this way:
  1. public class ContextFactory
  2. {
  3. [ThreadStatic]
  4. private static ProductionDBDataContext context;
  5. private string connString = "settings to access the database ...";
  6.  
  7. public static DataContext Context(){
  8. if (context == null)
  9. context = new ProductionDBDataContext(connString);
  10. return context;
  11. }
  12. }

The ThreadStatic attribute ensures that every thread will access a different instance. So the "context" is not shared amont threads. This simplifies Ricks's code. But our problems didn't stop here unfortunatelly :-(

You might expect the thread to die when the response is sent back to the client side, but it doesn't. Sometimes IIS reuses the thread for other requests, and the context is not a new, fresh instance. Moreover, the context caches some queries and that leads to several issues:

  1. The optimistic concurrency control nightmare. The default behavior is for "Linq to Sql" to check every db column on update, to see if there are more than one simultaneous attemp to persist the entity. If so, it raises the ChangeConflictException. This can be avoided by editing the dbml and marking every column with "Never" in the UpdateCheck property. Guess that.... when you regenerate the dbml file, this is overwritten and you loose your configuration. More information on conflicts here.
    One way to deal with conflicts is to catch the exception and force the update:

    1. var someEntity = ... // query here
    2. try
    3. {
    4. context.SubmitChanges(ConflictMode.ContinueOnConflict);
    5. }
    6. catch (ChangeConflictException e)
    7. {
    8. context.Refresh(RefreshMode.KeepCurrentValues, someEntity);
    9. }

    However, this does not avoid the cache invalidation problem.

  2. The cache invalidation problemis the last one we have found and solved so far. The solution is to drop the cache as the new request enters the server. To refresh the cache just create a new instance of the context:
    1. public class ContextFactory
    2. {
    3. [ThreadStatic]
    4. private static ProductionDBDataContext context;
    5. private string connString = "settings to access to database ...";
    6.  
    7. public static DataContext Context(){
    8. if (context == null)
    9. context = new ProductionDBDataContext(connString);
    10. return context;
    11. }
    12. public static void FlushContext(){
    13. context = new ProductionDBDataContext(connString);
    14. }
    15.  
    16. }

    Then, in the first layer of the server side (where the request is received) we call FlushContext to make sure it is new for this request. (For us is our JsonRpcController, a custom make controller hierarchy).

For now, we can rest assured that our DataContext is:

  • Unique for every thread so it is thread-safe.
  • Unique for every request so there are no cache problems among different requests.
  • If two different parts of the application need a context's instance in the same request, our factory gives a single reference (Singleton), making sure that only the first access initializes it (because we check whether it is null before instantiating).
Enjoyed reading this post?
Subscribe to the RSS feed and have all new posts delivered straight to you.
  • http://www.sharpbites.com alberto

    DataContext sounds like uow. Why not use session per request in httpmodule?

  • http://www.carlosble.com Carlos Ble

    How do you implement that with Linq to Sql Alberto?
    We are basically using a “db session” per request now.

    Thanks

  • http://www.sharpbites.com alberto

    Here’s an example on how to do it for nhibernate, I guess you can adapt that for your needs:
    http://ayende.com/blog/4101/do-you-need-a-framework