Click here to Skip to main content
Licence CDDL
First Posted 3 Jul 2008
Views 11,573
Downloads 30
Bookmarked 10 times

The Way I See a DAL

By | 3 Jul 2008 | Article
An article on designing a DAL in a personal way

Introduction

After looking for a generic DAL class and ORM frameworks I did not find one that suited my way of seeing things. All the mappings were done on tables, but all my applications run stored procedures that receive different data from relational tables! I observed that the entities and the fields coming from the procedures were the same, so why not automatically map them into entities? So I have developed a generic DAL that does just that.

Using the Code

The code has 3 classes: a DBHelper that does all the conversions needed from the database into OO, a ConnectionHelper that actually does all the work, and a GenericDAL that wraps/masks the ConnectionHelper and exposes a clean interface for the programmer.

To use the code just copy the 3 clases into your project and inherit the GenericDAL in your own DAL.

namespace strofo.DAL {
    public class NewCitiesDal : GenericDAL<City> {
        public static List<City> GetAll() {
            SqlCommand cmd = new SqlCommand("Cities_SelectAll");
            cmd.CommandType = CommandType.StoredProcedure;

            return GetListFromCommand(cmd);
        }
    }
}

The best part of this is that if you want to add a new field, you just have to modify the entity and/or stored procedures; no modifications of the DAL or the table in the database are needed.

You can get a single entity from the GenericDAL using a code like this one:

public static City GetById(int ID) {
    SqlCommand cmd = new SqlCommand("Cities_SelectById");
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.Add(DBHelper.ToSqlParameter("@ID", ID));

    return GetItemFromCommand(cmd);
}

or just a single value:

public static int Insert(City city) {
    SqlCommand cmd = new SqlCommand("Cities_Insert");
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.Add(DBHelper.ToSqlParameter("@Name", city.Name));
    cmd.Parameters.Add(DBHelper.ToSqlParameter("@ImgPath", city.ImgPath));

    return DBHelper.ToInt32(GetValueFromCommand(cmd), -1);
}

Hopefuly you like this and use it!

License

This article, along with any associated source code and files, is licensed under The Common Development and Distribution License (CDDL)

About the Author

strofo

Engineer

Romania Romania

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralYour questions Pinmemberexistenz_23:14 3 Jul '08  
GeneralRe: Your questions Pinmemberstrofo1:50 4 Jul '08  
GeneralRe: Your questions Pinmemberexistenz_4:45 7 Jul '08  
GeneralEsti inca foarte departe de luminitza de la capatul tunelului Pinmemberfiloteanuadrian10:28 3 Jul '08  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120517.1 | Last Updated 3 Jul 2008
Article Copyright 2008 by strofo
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid