三层架构由 3 个主要层组成
-
PL 表示层
-
BLL 业务逻辑层
-
DAL 数据访问层
每个顶层只询问下面的层,从不看到它上面的任何东西。
当他们问你你将如何构建你的 BLL 时,你可以这样写:
namespace Company.BLL
{
// let's create an interface so it's easy to create other BLL's if needed
public interface ICompanyBLL
{
public int Save(Order order, UserPermissions user);
}
public class Orders : ICompanyBLL
{
// Dependency Injection so you can use any kind of BLL
// based in a workflow for example
private Company.DAL db;
public Orders(Company.DAL dalObject)
{
this.db = dalObject;
}
// As this is a Business Layer, here is where you check for user rights
// to perform actions before you access the DAL
public int Save(Order order, UserPermissions user)
{
if(user.HasPermissionSaveOrders)
return db.Orders.Save(order);
else
return -1;
}
}
}
作为我正在创建的项目的一个实例:
PL 都是公开的服务,我的 DAL 处理对数据库的所有访问,我有一个处理 2 个的 Service Layer服务的版本,旧的 ASMX 和新的 WCF 服务,它们通过 Interface 公开,因此我可以轻松地即时选择用户将使用的服务
public class MainController : Controller
{
public IServiceRepository service;
protected override void Initialize(System.Web.Routing.RequestContext requestContext)
{
...
if (thisUser.currentConnection.ws_version == 6)
// Use old ASMX Web Service
service = new WebServiceRepository6(url, ws_usr, ws_pwd);
else if (thisUser.currentConnection.ws_version == 7)
// Use the brand new WCF Service
service = new WebServiceRepository7(url, ws_usr, ws_pwd);
...
}
}
在上面的代码中,我只是简单地使用依赖注入来分离另一层的知识,因为在这一层(表示层,因为这是 MVC 项目中的控制器)它不应该关心如何调用服务并且用户使用ServiceA 而不是ServiceB... 它需要知道的是调用IService.ListAllProjects() 将给出正确的结果。
你开始划分提议,如果服务连接出现问题,你知道这与表示层无关,它是服务层(在我的情况下),它很容易修复并且可以轻松部署一个新的@987654329 @ 而是再次发布整个网站...
我还有一个助手,它包含我在所有项目中使用的所有 业务对象。
希望对你有帮助。