应该如何我的业务逻辑与我的数据层进行交互? [英] How should my business logic interact with my data layer?

查看:107
本文介绍了应该如何我的业务逻辑与我的数据层进行交互?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我的工作让我的计划草案。

So I'm working on making a draft of my program.

这是我的计划:

GUI
---
Business Logic
---
Data

您应该能够在没有问题,更换任何 GUI 数据层。
每一层手表本身。因此, GUI 将调用从业务逻辑方法和方法总是会返回一个状态,也许一些数据。如何在GUI应的数据作出响应,应该总是在GUI层决定。业务逻辑应该在这个没有影响。
因此,与图形用户界面和业务逻辑之间的关系已经得到解决。
我希望你能跟着我。

You should be able to replace either GUI or the Data layer without issues. Every layer watches itself. So GUI will call methods from Business logic and the methods will always return a status and perhaps some data. How the GUI should respond to the data, should always be decided in the GUI layer. Business logic should have no influence over this. So the relations with GUI and business logic has been solved. I hope you can follow me.

现在的更具体的东西。
我的资料层的计划,是使用一个数据库。
现在,应该如何从数据层业务逻辑调用方法?

Now for something more concrete. My plan for the data layer, is to use an database. Now, how should Business Logic call methods from the data layer?

也许我应该让一个枚举,对应于不同的硬codeD SQL查询,该数据层是知道的?

Perhaps I should make an enum, that corresponds to different hardcoded SQL queries, which the data layer is aware of?

例如

Datalayer.GetResults(Queries.GetAllCustomersIDs);

查询是一个枚举。

Queries being an enum.

如果这是正确的做法,应该GetResults回报?一个字符串数组?但如果查询有哪些多维数据?

If this is the right way, what should GetResults return? a string array? but what if the query has multidimensional data?

我应该再有2个泛型方法呢?

Should I then have 2 generic methods instead?

Datalayer.GetSingleDimensionResults(SingleDimensionQueries.GetAllCustomersIDs);
Datalayer.GetMultipleDimensionResults(MultiDimensionQueries.GetAllCustomers);

或者我应该可能有一个查询各种数据的要求?

Or should I perhaps have a query for each kind of data request?

Datalayer.GetAllCustomerIDs;
DataLayer.GetAllCustomers;

等。

推荐答案

在一般情况下,我用做的是:

In general, what I use to do is:

数据层:

对于数据访问,我创建了一个接口,每一个对象。每个接口列出了所讨论的对象的所有公共数据访问方法。为了保持数据,创建容器类型,为每个对象为好,这只能是数据结构或简单的类。我也是靠语言的数据集,如列表,握着我的数据,所以我没有链接到一个特定的数据库类型。在那之后,我创建一个实现数据接口的类,这个类有所有的SQL和访问数据库,所以在数据存储技术改变的情况下,这是将被改变的唯一类。

For data access, I create an Interface, for each object. Each interface lists all the public data access methods for the object in question. To hold the data, I create container types, for each object as well, which can be structs or simple classes only with data. I also rely on language data set, like lists, to hold my data, so I not linked to a particular database type. After that, I create a class that implements the data interfaces, this class has all SQL and access the database, so in case of change in the data storage technology, this is the only class that will be changed.

业务层:

是否所有数据的逻辑,如何验证,从数据接口至极方法应该被调用,以何种顺序。这个类接收和发送数据到数据存储或GUI,利用货柜,其中数据类型我的容器如上所述(例如列表)。

Does all the logic with data, how to validate, wich methods from the data interfaces should be called and in which order. This class receives and "send" data to the data storage or GUI, using containers (lists for example) where the data types are my containers mentioned above.

GUI:

调用业务逻辑方法和显示/格式的数据presentation。有没有逻辑在这里除了调用业务逻辑的正确的方法。

Calls business logic methods and show / format data presentation. There's no logic here other than call the right methods of the business logic.

的容器(C#)小code例如

Small code example of a container (C#)

    //Interface for Department class data access. DataStorage assembly

    namespace DataStorage
    {
        public interface IDepartmentDS
        {
            void Open();  //Open data conection
            void Close(); //Close data conection
            List<Repositories.Department> List(); //Gets all departments (from data base)
        }
    }


    //This class holds all data regarded a department. There's no logic here. Repositories assembly

    namespace Repositories
    {
        public class Department
        {
            [Browsable(false)]
            public Department()
            {
            }

            [Browsable(false)]
            public Department(String Symbol, String Name)
            {
                this.Symbol = Symbol;
                this.DeptName = Name;
            }

            public Department(Department department)
            {
                this.Symbol = department.Symbol;
                this.DeptName = department.DeptName;
            }

            [Browsable(false)]
            public String Symbol { get; set; }

            public String DeptName { get; set; }
        }
    }


    //This class implements the data manipulation itself, accessing the real database.
    //However the data exchange outside this class is done via repositories classes and 
    //Generics - Lists mainly

    public class DataStorage : IDepartmentDS
    {
       //Here I use to put generic functions to connect with the database, format stored
       //procedure parameters list etc.

       //Implementation of the List method declare in the Department Interface
           List<Repositories.Department> IDepartmentDS.List()
            {
                String query = String.Format("SELECT * FROM {0}", DepartmentTable);
                int rows = 0;
                DataSet ds = ExecSqlCommand(query, out rows); //this method is private to this class

                if (ds == null)
                    return null;

                List<Repositories.Department> list = new List<Repositories.Department>();
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    list.Add(new Repositories.Department((String)row[DepFN_Symbol], (String)row[DepFN_DepName]));
                    //DepFN_Symbol and the others are just const variables representing the column index
                }

                return list;
            }

    }

public class DepartmentLogic
{
   public DepartmentLogic()
   {
      .....
   }

   public List<Repositories.Department> GetAllDepartments()
   {
      //Here I create an Instance of the DataStorage but using the Department interface
      //so I restrict the access to Department data methods only. It could be a good 
      //idea here to use the factory pattern.

      IDepartmentDS department = (IDepartmentDS) new DataStorage();
      department.Open();

      List<Repositories.Department> departments = department.List();

      department.Close();

      return departments;
   }

}

此业务逻辑的例子是,确实是,很简单,只是表明如何从存储层的数据,但只要你有机会获得的数据,你可以在你想要的方式操纵它。就在这里评论:也许这个解决方案,应重新鑫卡特如果与成千上万的申请一个非常繁忙的服务器实现的,因为它可以使用大量内存

This Business logic example is, indeed, very simple, just shows how to retrieve data from Storage layer, but as long as you have access to data, you can manipulate it in the way you want. Just a comment here: maybe this solution should be re-thinked if implemented in a very busy server with thousands of requisitions because it can use lots of memory.

有关的业务逻辑和也UI的角度来看,所有的数据都使用通用集装箱像解释模块之间传送。所有这些模块之间的连接点是容器类,所以所有的类都比较不那么脱钩。

For the business logic and also UI point of view, all data are communicated between modules using general purpose containers like Lists. The link point between all those modules are the containers classes so all the classes are more less well decoupled.

UI使得申请到业务逻辑类,所以它就像一个服务提供商。这样做的那样,改变UI将不会影响下面的类吧。

UI makes requisitions to the Business logic classes, so it acts like a service provider. Doing in that way, changing the UI will not affect the classes below to it.

业务逻辑请求和发送数据,使用通用数据,因此更改数据库/存储技术在数据存储类不应该影响它。

The Business logic requests and send data to the Data storage classes using general purpose data, so changing the data base / storage technology should not affect it.

这是我用来做的方式,我试图改善它;)

That's the way I use to do and I'm trying to improve it ;)

这篇关于应该如何我的业务逻辑与我的数据层进行交互?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆