Build Your Own Data Access Layer Series

19 commentsWritten on August 23rd, 2009 by
Categories: Build Your Own DAL

I'm definitely not a fan of building your own Data Access Layer (DAL), since there are plenty of powerful and mature options already available. However, we have 2 customers at work who simply don't let us use any existing libraries/tools as a DAL and want us to just use straight ADO.NET. I don't want to get into their reasons for this, but the reality of the situation is that whenever we have to develop projects for them, we need to use a custom built DAL. I've never seen a custom built DAL that i found acceptable, let alone one that i actually wanted to use.

A lot of people typically go the code generation route when faced with this situation, which is exactly what we have done in the past. Been there, done that, hated it with a passion for various reasons. One of my coworkers recently started a new project for one of these customers, and he started implementing a new DAL. I had to review this, and while it had some good ideas there was a large amount of repetitive and error-prone code that still needed to be written by developers for every table. So i set out to come up with something better. If we did have to use a custom DAL for these customers, i wanted to make sure that it would at least avoid having us write repetitive, error-prone code for every table that we needed to use. Oh, and without having to resort to code generation. Since we are all NHibernate users (when customers don't have a problem with us using it, that is) i wanted something that was somewhat similar in ease-of-use though it could obviously never match its feature set, power and maturity.

I spent about 24 working hours (in total) on this, and i believe i came up with something that is acceptable for most simple forms-over-data applications. This DAL allows you to write your entity classes as POCO's, offers 'out-of-the-box' CRUD functionality for every mapped table, and has lazy loading for reference properties (so you don't need to pollute your entity classes with foreign key properties). There is also a simple session-level cache, and there is some functionality to ease the pain of using simple, custom queries (with that i mean: every query that is not a select all or select by id and that doesn't join other tables).

Compared to a real ORM, it is missing a lot: there is no Unit Of Work implementation, no automated change tracking of entities, no dirty checks, no collection support, no advanced querying possibilities, no statement batching, no serious caching functionality, no transitive persistence, and a whole host of features that something like NHibernate gives you for free. Each and every one of those features comes with a great cost of complexity and development time to get 'right' so it truly doesn't make a lot of sense to do all of this yourself.

In this series, we're going to go over the entire implementation of this DAL and throughout the series i will point out its shortcomings and try to explain the complexity that would be required to make it truly powerful. The purpose of this series is basically to:

  • Show you that you really don't need to resort to code generation to build your own custom DAL
  • Show you what kind of complexity is involved with the implementation of a good DAL
  • Convince you that you typically are better off with simply using something that is already available as a mature, powerful and proven solution

These are the posts that this series consists of:

  1. Mapping Classes To Tables
  2. Out Of The Box CRUD Functionality
  3. Hydrating Entities
  4. Session Level Cache
  5. Lazy Loading
  6. Executing Custom Queries
  7. Bringing It All Together
  8. Conclusions
  9. Enabling Bulk Inserts

I will update the list above with actual links to the posts as soon as they are published.

Note: the code of this series can be found here

  • http://blogs.msdn.com/nblumhardt Nicholas Blumhardt

    Before NHibernate was really off the ground, many of us seem to have written ORMs as a rite of passage.

    I didn’t have a good understanding of any existing ORMs the first time I tried this, so the results were pretty unspectacular. More important than any lack of technical insight, I had no idea just how big this task really is – something you’re obviously familiar with :)

    It will be very interesting to see what you distill as the ‘essential’ features, looking forward to the rest of the series!

  • Pingback: Reflective Perspective - Chris Alcock » The Morning Brew #418

  • Pingback: Daily Links for Monday, August 24th, 2009

  • http://www.dotnetoutsource.com/ Chanva

    Thanks. I think it is very helpful. Yes, some clients don’t like any ORM product, he think it is too heavy. So that we are using native ADO.NET.

  • http://idevone.wordpress.com Gregory Mostizky

    I wonder if you considered something like Spring JDBC Template approach.
    It’s in java so I don’t know if something similar exists in .net land,
    but if not it should be somewhat simple to implement.

    The basic idea is rather simple – users write all the SQL themselves,
    but the framework helps with repetitive and boiler plate code.

    For example, you define a mapper that will map some result set columns to entity.
    Then you reuse that mapper every time you query for some type of entity.

    Check this out for some examples of how it is done: http://www.vogella.de/articles/SpringJDBC/article.html

  • http://www.twitter.com/brendankowitz Brendan

    What about:
    9. Linq Provider :p
    At that point, it nearly doesn’t matter what orm is underneath…

    …actually, who in their right mind would bypass every single opensource AND commercial orm on the market and go with ado.net?! Event MS are in the game with Linq-to-sql/Entity Framework…

    I say, you should spend half your allocated time dedicated to powerpoint presentations and rallying to change their minds to use a real orm, then you’ll still finish ahead of schedule and with less bugs and improved future maintenance :)

  • http://www.jacopretorius.net Jaco Pretorius

    Erm… Linq-to-Sql is a built-in ORM? It’s not a library…

  • http://davybrion.com Davy Brion

    doesn’t really matter whether it’s bundled with the .NET framework or not… they weren’t interested in using Linq-To-Sql or any other ORM for that matter

    one of those customers is now wiling to migrate the data layer of one of their projects (which used code generation) to entity framework though…

  • Pingback: Daily Links for Monday, August 24th, 2009 | LaptopHeaven's Blog

  • Pingback: IdeaBlade DevForce – Model Setup Walk-through – Background « One Man went to Mow…

  • http://dotnet.uni.lodz.pl/whut Whut

    Hey,

    I want to mention that I used ideas from your series to implement my own mapper for use in MS SharePoint, it’s at http://osm.codeplex.com. Thanks for sharing yours, nice work!

  • Pingback: Is there life out there (or what is your ideal ORM for PHP)? | SymfonyLab

  • Pingback: My First Data Access Layer Amends Part 1 | Jonathan Stowell

  • Pingback: Request/Response Service Layer: Handling Requests

  • Pingback: Build Your Own Data Access Layer: Conclusions

  • Pingback: Build Your Own Data Access Layer: Executing Custom Queries

  • Pingback: Build Your Own Data Access Layer: Session Level Cache

  • Norman_mackay

    Check out LLBLGen, it’s a commercial product used by large companies ,it allows you to generate your model seperately and then  generate your DAL in native LLBLGen, nHibernate, EntityFramework 1 & 4 & LINQ2SQL. It’s got great support to views, stored procedures etc which is important when re-writing frontends onto legacy databases.

    I’ve done a few apps using LINQ2SQL but it has several drawbacks including regenerating the model but it was fast and lightweight (unlike EF), we looked at Telerik ORM but it is way too buggy and seems a little forgotten by Telerik.

    After looking at all the alternatives we settled on LLBLGen.

    http://www.llblgen.com/defaultgeneric.aspx

    A guy called Frans Bouma created it.

    Norman.

  • Pingback: Links to “Build your own X Series” in .NET « Insight's Delight