ZedBee
ZedBee

Reputation: 2378

3 Layer Architecture - Is it fine to put sql queries in Business layer

My asp.net project is based on three layer architecture.

(Data Access Layer) DAL - (class library)

    private static string connString ="";

    private static OracleConnection conn;

    public static OracleConnection OpenConn()
    {
        if (conn==null)
        {
            conn = new OracleConnection(connString);
        }
        if (conn.State != ConnectionState.Open)
        {
            conn.Open();
        }

        return conn;
    }

    public static DataTable Select(string query)
    {
        DataTable dt = new DataTable();
        OracleDataAdapter da = new OracleDataAdapter(query, OpenConn());
        da.Fill(dt);
        return dt;
    }

    public static void Execute(string query)
    {
        OracleCommand cmd = new OracleCommand(query, OpenConn());
        cmd.ExecuteNonQuery();
    } 

I have put all my queries in (Business Logic Layer) BLL classes (All BLL classes are in separate class library project)

e.g EmployeeBLL

public static class EmployeeBLL
{
    public static DataTable Employees()
    {
       DataTable dt = new DataTable();
        string q = string.Format("select * from employees");
        dt = OraDAL.Select(q);
        return dt;
    }

    public static DataTable AddEmployee(string name)
    {
        DataTable dt = new DataTable();
        string q = string.Format("INSERT INTO employees (ename) VALUES('{0}')", name);
        dt = OraDAL.Select(q);
        return dt;
    }
}

I have seen some blog posts on three layer architecture where sql queries are constructed in BLL and that's why I developed the project keeping sql queries in BLL but now I feel I should move them to DAL.

so my questions are

  1. Is it okay to keep sql queries in BLL or I should move them to DAL?
  2. Is it okay to use datatables for moving data between layers or I should use DTO's instead?

Upvotes: 3

Views: 2432

Answers (3)

NDJ
NDJ

Reputation: 5194

The beauty of splitting your application into layers is that if you ever need to change data repository, you can do so with a minimum of pain; plus you can test your objects in isolation with mocks, etc.

If you start to hard wire sql queries, etc. into the business objects - then moving, say, to SQL instead of oracle might mean refactoring objects within the business layer as well as the data layer.

I personally don't think business objects should see datatables. A better approach would be to have shared objects (or interfaces) which both the data layer and business layer reference.

Upvotes: 3

pero
pero

Reputation: 4259

You should check ORMs like NHibernate or Entity Framework 4+, but as you are working with Oracle NHibernate is better IMO.

The ORM will basically represent your DAL and it will take the responsibility to create SELECT, INSERT, UPDATE and DELETE statements for you in a dialect of the database you are currently mapping to.

It will allow you to do queries on your domain model instead of on tables. And that is what you want to do. It will abstract your database so you can in future make new mappings and map your domain objects to MySQL for example. Or do additional mapping on some in-memory database to allow you to run fast integration test.

Learning NHibernate (or other ORM) is an investment but IMO it's worth your time if you will be working with .NET and RDBMS in future.

Upvotes: 3

Big Daddy
Big Daddy

Reputation: 5224

Is it okay to keep sql queries in BLL or I should move them to DAL?

It's okay, but I don't think it's the right thing to do. Put them in your DAL where they belong.

Is it okay to use datatables for moving data between layers or I should use DTO's instead?

I prefer to use DTOs and I think this is the way to go, but it's acceptable to use DataTables.

Upvotes: 3

Related Questions