Build Your Own Data Access Layer: Conclusions

15 commentsWritten on August 29th, 2009 by
Categories: Build Your Own DAL

Note: This post is part of a series. Be sure to read the introduction here.

Building your own DAL is almost never a cost-efficient solution. In this case, i wrote this DAL in 24 working hours, but it is limited in scope, power, flexibility and functionality. Having said that, i do think it's better than every single custom DAL i've come across so far. Taking it to the next step however would take a lot more effort, which truly is never worth it. As you try to provide more functionality, overall complexity of your custom DAL will increase heavily and the effort you'll eventually spend on it will more than outweigh any of the downsides that might come with using something that already exists. Building your own DAL is an undertaking that should always be questioned, and shouldn't be considered unless the alternative of doing so is even worse.

If however, you're in a situation where it does make sense, then i hope this series might have been helpful for you. I've shown that you can come up with something relatively decent without having to resort to code generation, without having to spend an insane amount of effort on it, and without having to write repetitive and error-prone code in your application code. Those were the goals i had in mind when i started working on this DAL, and i think i've succeeded at achieving those goals. The final result is an easy-to-use DAL which is far from as powerful as already existing solutions, but it is pretty good for the scenario's where we intend to use this.

The code itself is clear, easy to maintain and in some cases, very easy to extend as well. In total, this DAL is slightly less than 1100 lines of code, and i think the complexity of the code is relatively low so everyone should be able to understand what's going on, how it works, and where things could be modified in order to fix issues or to add new features.

Also, this series of blog posts helps in figuring out how it works since pretty much every aspect of it is now documented pretty extensively :)

All in all, i had a lot of fun in writing both the DAL and this series of posts (which took another 8 hours in total).

In the introduction of this series, i mentioned the following:

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

So tell me, did the series succeed in these listed goals? Would you still go for code generation if you had to create a custom DAL? Would you still prefer to use a custom DAL over something that already exists? How would you react to having to use this particular DAL? What would need to be added or modified before you would find it acceptable? What are your thoughts on this in general?

I'd be very interested in hearing about it, so please do post your opinions in the comments :)

  • Lajjne

    Wow, excellent series of posts. I’ve been using CodeSmith to generate custom DALs on occasion, but after reading about your solution I’m never touching a code generator again… Just one question: where can I download the code :-)

  • Nima

    Thanks for these great series.It helped me to understand what’s under the hood of a real DAL and I learn a lot of tricks.
    I wonder if you could post the code base so we could see it as a whole ( I just want to view the code and I won’t use it I promise :) )
    Thanks again and I’m looking forward for your inspiring posts

  • http://davybrion.com Davy Brion

    sorry, i can’t offer the code as a download

    but every part of the code has been included in these posts…

  • http://sergejus.blogas.lt Sergejus

    Davy, great series! I was thinking on usin you DAL for the mobile development until post about Proxy class generation and CastleProxy. Do you know whether it can be run on .NET Framework Compact Edition or do you know any other Proxy generator that is capable of running on .NET Framework CE?

  • http://davybrion.com Davy Brion

    @Sergejus

    good question, but i’m afraid i don’t know the answer… if DynamicProxy doesn’t, you might want to check out LinFu… not sure that it would work there either though

  • Pingback: New and Notable 364 : Sam Gentile's Blog (if (DeveloperTask == Communication && OS == Windows)

  • Diego Mijelshon

    Davy,

    Great, great, great work.

    Your series, (along with Ayende’s comments) are the best programming-related stuff I’ve read in a long time.

    Your contribution to the community is bigger than you can imagine.

    Just one question: what’s the license of the code you posted?

  • http://davybrion.com Davy Brion

    @Diego

    i hadn’t really thought about that yet, so i added the following to the introduction post:

    “Note: I can’t provide a downloadable version of the code that is shown in these posts, but all of it is indeed shown and covered in this series and may be reused under the terms of the Creative Commons Attribution 2.0 License.”

  • Cedric

    Hi Davy,

    Thanks you a lot for this great series. I haven’t used NHibernate or any ORM solution yet (@ work we use mostly stored procedure, typed dataset, etc …). It made me really understand basically how seems to works an ORM solution and what benefit you could get from using one against writing your own stuff. It was very interesting.

    Thanks again for this awesome series !

  • stefan

    Thanks for this awesome series…

  • http://thefrozencoder.ca William

    Davy,

    Great series, just read through it. I have a couple of observations.

    Class: Session
    Method: ctor
    Line: hydrater = new EntityHydrater(metaDataStore, this, sessionLevelCache);

    Maybe I read to fast but there was no 3 argument EntityHydrater ctor method

    ——–
    Class: EntityHydrater
    Method: CreateProxy
    Line: var proxy = proxyGenerator.CreateClassProxy(referenceInfo.ReferenceType, new[] { new LazyLoadingInterceptor(tableInfo, session) });

    Where does session come from?

    • http://davybrion.com Davy Brion

      @William

      you are correct… in the entity hydration post i kept the constructor with the session out of it, because the session is only used for lazy loading, and i didn’t want to mention the session at that point because it wasn’t relevant (yet)

      so the session in the CreateProxy method is actually passed in in the (missing) 3 argument constructor.

  • http://thefrozencoder.ca William

    @Davy

    Ok makes sense there.

    I threw your code together and got it to compile however when the call to:

    public TableInfo GetTableInfoFor(Type entityType)

    In the MetaDataStore class does not return a tableInfo object, it seems that it does not find the entity by key. The code is loading the entities from my external assembly fine as a quick step through looks like it should work.

    Any pointers in what the issue may be?

  • http://davybrion.com Davy Brion

    @William

    no idea :s

    i’d let it break when it can’t find the type, then inspect the metadatastore to see if other types are missing, or if you’re passing the wrong type somewhere

  • Pingback: Build Your Own Data Access Layer Series