简体   繁体   English

3层架构 - 在业务层中放置SQL查询是否可行

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

My asp.net project is based on three layer architecture. 我的asp.net项目基于三层架构。

(Data Access Layer) DAL - (class library) (数据访问层)DAL - (类库)

    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) 我已将所有查询放入(业务逻辑层)BLL类(所有BLL类都在单独的类库项目中)

eg EmployeeBLL 例如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. 我在三层架构上看到了一些博客文章,其中sql查询是在BLL中构建的,这就是为什么我开发项目在BLL中保留sql查询但现在我觉得我应该将它们移动到DAL。

so my questions are 所以我的问题是

  1. Is it okay to keep sql queries in BLL or I should move them to DAL? 是否可以在BLL中保留sql查询,或者我应该将它们移动到DAL?
  2. Is it okay to use datatables for moving data between layers or I should use DTO's instead? 是否可以使用数据表在层之间移动数据,或者我应该使用DTO?
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. 把它们放在它们所属的DAL中。

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. 我更喜欢使用DTO,我认为这是可行的方法,但使用DataTables是可以接受的。

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. 如果您开始将sql查询等硬连接到业​​务对象中 - 那么移动(例如)移动到SQL而不是oracle可能意味着重构业务层和数据层中的对象。

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. 更好的方法是拥有数据层和业务层都引用的共享对象(或接口)。

You should check ORMs like NHibernate or Entity Framework 4+, but as you are working with Oracle NHibernate is better IMO. 你应该检查像NHibernate或Entity Framework 4+这样的ORM,但是当你使用Oracle时,NHibernate是更好的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. ORM基本上代表您的DAL,它将负责以您当前映射到的数据库的方言为您创建SELECT,INSERT,UPDATE和DELETE语句。

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. 它将抽象您的数据库,以便您将来可以创建新的映射并将您的域对象映射到MySQL。 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. 学习NHibernate(或其他ORM)是一项投资,但IMO值得您花时间,如果您将来使用.NET和RDBMS。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM