Persistence Ignorant POCOS part 4 – LinqToSql

Posted : Wednesday, 23 March 2011 10:47:04

In this, the fourth post in a series comparing the developer experience of hooking up a POCO using various ORMs I shall be creating an LinqToSql implementation of the persistence layer of my application. Linq2Sql although now officially deprecated by Microsoft still has a considerable following in the community and various open-source projects mean that, despite the lack of official support, LinqToSql isn’t going anywhere soon. While I haven’t used this particular persistence platform for a few years I do fondly remember how easy it was to get up and running with. The only aspect of LinqToSql that I have no prior experience of is defining the mapping configuration without using attributes. Given that the goal of this exercise is to create truly ignorant POCO entities, then defining the mapping in xml files is the only option!

So lets rock and roll!!! As with the previous posts, the first step is to create the basic project skeleton. I added a new class library called LcaModel.Linq2Sql to my application and as before, to this project I added an implementation of each of the repositories and a class to handle the wiring up of the dependencies:


In order to use all the LinqToSql goodness I had to add a reference to the System.Data.Linq assembly which can be found in the .Net tab of the add reference dialogue box:


In striking similarity to the EntityFramework, LinqToSql facilitates data-access, change-tracking and lazy-loading via an object known as a DataContext (compared to ObjectContext in EntityFramework). The constructor of this class has a two-parameter overload which accepts a connection string and an instance of an XmlMappingSource object. As the name suggests this object is responsible for mapping a CLR object to a database table. So following the convention used in the NHibernate example I added a folder to my project called “Mapping”. To this folder I added a single xml file called “Mapping.xml” and set its build action to “copy always”, at this point the xml file contained only the default xml document attribute added by visual studio. Next I added a binding to the Linq2SqlModule class to create an instance of XmlMappingSource from the xml file and altered the constructor of each of the repository implementations to accept an instance of string and this XmlMappingSource. I then stored class level reference to each of these. The load() method of the Linq2SqlModule is shown below:

   20         public override void Load()

   21         {

   22             //check there is a connection string

   23             var connectionStringSection = ConfigurationManager.ConnectionStrings["LightsCameraAuction"];

   24             if (connectionStringSection == null || string.IsNullOrEmpty(connectionStringSection.ConnectionString))

   25                 throw new ApplicationException("no connection string defined");


   27             Bind<AuctionService>()

   28                 .ToSelf()

   29                 .WithMetadata(MetadataBindingParamName, MetadataBindingParamValue);


   31             Bind<XmlMappingSource>()

   32                 .ToMethod(ctx => XmlMappingSource.FromUrl("Mapping/Mapping.xml"))

   33                 .InSingletonScope();


   35             Bind<IItemRepository>()

   36                 .To<Linq2SqlItemRepository>()

   37                 .When(c => c.ParentContext.Binding.Metadata.Get(MetadataBindingParamName, "") == MetadataBindingParamValue)


  • (This will not appear on the site)