Mosh-Production's Blog

June 4, 2009

Data Access Hell

Filed under: Coding Horror — Shani Natav @ 7:40 am
Tags: , , ,

With all the story about the airplane crash (the airfrance flight) I forgot to do my homework (Too much news reading)

Ā ( think they are drinking Mai-Tai with jack and sawyer right about now).
I’ve been up and busy drawing sequence diagrams while watching marathon’s of Lost (from the beggining) And I have to admit I didn’t do much work of any kind (My team mates are alittle angry, as we have a small project to deliver), and what I realy should be doing is drawing sequence diagrams.

So I wanted to share with a little thought I had.
For the project, I was making a DAL (data access layer) based on Linq to Sql, and the next idea i’ll present here, didn’t quite Fit in:

MyDataContext dB = new MyDataContext();

The Data context is a realy nice code generated class. Now I can access all the tables through properties

var result = dB.SomeTable.FirstOrDefault(someitem => someitem.id == 1);

But what i’m getting is a data access layer, tightly coupled with my business objects… which is excatly what my lecturer would lower my grade about…
so what I did, after alot of thoughts, was, A whole new set of object that will Handle the persistence, in the manner of:

CustomerDAL cust = new CustomerDAL(); //Data Access Object
Customer cust = new Customer(); //business object, can read data object and bind all the properties from the data access object to himself.

So I killed the coupling, and everything seems nice. The DAL objects reside in the DAL namespace and business logic is one tier above them, in the business logic layer.
The application only uses the the business Object

Customer cust = Customer.GetCustomer(name, password);

The customer business logic handles the persistence layer by himself and translates the DAL object to a temporary business object: (this code in Customer class


CustomerDAL prox = DAL.CustomersDALs.GetUser(name, password); //extension method to the CustomerDALs Table
HandleProxyItem(prox); //read Fields

You can perform update to the DAL with the business object (don’t forget we are working with a detached from context Item):


UpdateProxyItem(prox);
DAL.CustomersDALs.UpdateCustomer(prox); //yet another extension method

Let’s see what my honourable Dr. would say about that (Maor says i’m going too deep for a small project, but what does he know…)

Meanwhile, play this game:

3 Comments »

  1. Dear Friend
    You are articles are nice and excellent concept. I would like to invite to newly launched .NET Programming website the codegain.com on 1st of this June 2009. Currently CodeGain has more than 400 articles within the a month under the followings categories C#, VB.NET,ASP.NET,WPF,WCF,WFF,LINQ, AJAX, JQuery, JavaScript, Sql Servers , Oracle and more. To more list of categories visit the http://www.codegain.com. I have seen you are writing greatest article to web portal, Iā€™m kindly asking you publish your article in codegain.com also and support to grow the CodeGain share this with your friends also. I am expecting good response from you. You can contact me using info@codegain.com.

    Thank you
    RRaveen
    codegain.com

    Comment by RRaveen — July 3, 2009 @ 2:56 am | Reply

  2. Hi

    Comment by Mosh — June 5, 2009 @ 3:29 pm | Reply

    • Hi to you too, nice way to advertise your link šŸ™‚
      can you put a link to our ugly blog in your site?

      Comment by Mosh Productions — June 7, 2009 @ 9:48 am | Reply


RSS feed for comments on this post. TrackBack URI

Leave a comment

Blog at WordPress.com.