如何在c#中实现三层架构
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13786549/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to implement 3 tiers architecture in c#
提问by Tr?n Minh
I read many articles talk about 3 tiers architecture in c# but i see that:
我读过很多关于 c# 中的 3 层架构的文章,但我看到:
Almost use Bussiness Logic Layer(BLL) as a object mapping corresponding table in database. This BLL object has some methods like these:
+
GetData(): return this;+
Update(this);+
Insert(this);+
Delete(this);This BLL object calls corresponding DAL(Data Access Layer) to execute to corresponding table of database.
- I think if with above methods, we can oly get a record and update it. But if we have got many records and want to put it into a dataset, how can we update all records changed? Must we use loop for all records to do that?
- Moreover, in the database, there are many tables related to each other, so how can we put them into dataset, make relations and
add, update, delete records like using
BindingSourcewith aComboboxand aDataGridView? And how can we seperate code in this situation?
几乎使用业务逻辑层(BLL)作为对象映射数据库中的对应表。这个 BLL 对象有一些这样的方法:
+
GetData():返回这个;+
Update(this);+
Insert(this);+
Delete(this);该 BLL 对象调用相应的 DAL(数据访问层)执行到数据库的相应表。
- 我想如果用上面的方法,我们只能得到一个记录并更新它。但是如果我们有很多记录并且想把它放到一个数据集中,我们如何更新所有改变的记录?我们必须对所有记录使用循环才能做到这一点吗?
- 而且,在数据库中,有很多相互关联的表,那么我们如何将它们放入数据集中,建立关系并像使用
BindingSourceaCombobox和a一样添加,更新,删除记录DataGridView?在这种情况下我们如何分离代码?
回答by Soner G?nül
There are 2 cool project about three tier architecture in C#.
C#中有两个关于三层架构的很酷的项目。
3-tier architecture in C#and T?h?r?e?e? ?L?a?y?e?r? ?A?r?c?h?i?t?e?c?t?u?r?e? ?i?n? ?C?#? ?.?N?E?T?. Both projects are amazing specificly for you.
C#和T?h?r?e?e 中的3 层架构??层??建筑学??在??C?#??。?网?. 这两个项目都特别适合您。




回答by Moiz


The above describe a very simple architecture of a 3-tier model.
上面描述了一个非常简单的三层模型架构。
- DAL (Data Access Layer) interacts with Database directly, so all the SQL operation are being done within DAL only.
- BLL (Business Logic Layer) works like a mediator between DAL and the Presentation Tire.
- No direct communication is allowed between DAL and Presentation Layer.
- Although there is no physical presence of the Entity Layer, but Entity encapsulates all the information/data and passes it from one layer to the other.
- So all the Database object name and the Database Schema is being restricted inside the DAL which gives an extra security layer to the application.
- As Business rules/logics are being defined inside BLL, any update to business logic will not impact the DAL and the presentation layer
- DAL(数据访问层)直接与数据库交互,所以所有的SQL操作都只在DAL内部完成。
- BLL(业务逻辑层)就像 DAL 和 Presentation Tire 之间的中介。
- DAL 和表示层之间不允许直接通信。
- 虽然实体层没有实体存在,但是实体封装了所有的信息/数据,并将其从一层传递到另一层。
- 因此,所有数据库对象名称和数据库架构都被限制在 DAL 内,这为应用程序提供了额外的安全层。
- 由于业务规则/逻辑是在 BLL 中定义的,对业务逻辑的任何更新都不会影响 DAL 和表示层


This diagram describes an actual implementation of a 3-tier model.
此图描述了 3 层模型的实际实现。
- Data Access Service and the Database Server can be hosted in single Server.
- Mostly SQL Server 2000/2005/2008 or Oracle can be hosted on Windows 2000/2003 Server.
- Business Server exposes all the operation through Web Service /Remoting/WCF.
- A highly configured server with Windows 2000/2003 can be used to host the Business Service, or else Microsoft BizTalk Server also can be used for this.
- Presentation Tier or the Client Consumes the Service exposed at Business Server by using the Proxy through http:// pipeline.
- Client can be any standalone machine with the application is being installed in case of Desktop Application (Win-Form or Console Application), or having a Browser to run Web Application.
- Data/Information are being encapsulated by entity and transferred from one location to another over network followed by all the network protocol.
- 数据访问服务和数据库服务器可以托管在单个服务器中。
- 大多数 SQL Server 2000/2005/2008 或 Oracle 可以托管在 Windows 2000/2003 Server 上。
- Business Server 通过 Web Service /Remoting/WCF 公开所有操作。
- 可以使用带有 Windows 2000/2003 的高度配置的服务器来托管业务服务,或者 Microsoft BizTalk Server 也可以用于此目的。
- 表示层或客户端通过 http:// 管道使用代理来消费在业务服务器上公开的服务。
- 在桌面应用程序(Win-Form 或控制台应用程序)的情况下,客户端可以是安装了应用程序的任何独立机器,或者具有运行 Web 应用程序的浏览器。
- 数据/信息由实体封装,并通过网络从一个位置传输到另一个位置,并遵循所有网络协议。

