Click here to Skip to main content
15,911,360 members
Articles / Programming Languages / C#
Article

The Way I See a DAL

Rate me:
Please Sign up or sign in to vote.
1.15/5 (12 votes)
3 Jul 2008CDDL1 min read 25.3K   75   11   4
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.

C#
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:

C#
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:

C#
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)


Written By
Engineer
Romania Romania
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralYour questions Pin
existenz_3-Jul-08 23:14
existenz_3-Jul-08 23:14 
GeneralRe: Your questions Pin
strofo4-Jul-08 1:50
strofo4-Jul-08 1:50 
The best part in having procedures is that you can have an alias for the columns you are getting; I am not assuming that the names are matching, I'm making sure they are matching (it's not luck); Linq does something similar. I didn't try to recreate an orm, just a part of the functionality to ease my coding, with little changes to the current projects.
GeneralRe: Your questions Pin
existenz_7-Jul-08 4:45
existenz_7-Jul-08 4:45 
GeneralEsti inca foarte departe de luminitza de la capatul tunelului Pin
filoteanuadrian3-Jul-08 10:28
filoteanuadrian3-Jul-08 10:28 

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

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