Click here to Skip to main content
16,004,828 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.5K   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 
> All the mappings were done on tables, but all my applications run stored procedures that receive different data from relational tables!

That's the idea of an or mapper. It maps something onto objects. Any or mapper does that and actually most of them can handle stored procedures, views and tables without any problems. They can map your objects to subsets of tables (particular columns), full tables, etc..

> I observed that the entities and the fields coming from the procedures were the same, so why not automatically map them into entities?

The idea from a multi layered architecture, which is probally the reason why you want a DAL anyway, is that your Business layer does not know anything from your DAL. The reason that your properties of the classes are matching column names is pure luck and shouldn't be a reason to assume this is always the case. This is also one of the reasons why or mappers like LinqToSql, Entity Framework, NHibernate etc.. use mapping definitions.
GeneralRe: Your questions Pin
strofo4-Jul-08 1:50
strofo4-Jul-08 1:50 
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.