这些是推荐的层:
表示(MVC、WPF、其他):仅包含表示逻辑(从不包括业务逻辑),控制器仅处理与应用程序/服务层的通信以协调通信。
分布式服务(远程门面):由于您将有很多客户端,其中一些是 Windows 应用程序,而另一些是 Web 应用程序,建议创建一个远程服务层(WCF 服务或 Web 服务),将业务层暴露给消费者(最好发送和接收 DTO)。
应用层:处理与域层的通信,协调事务逻辑和技术服务的层,如果您使用 DTO,它将域对象转换为 DTO,反之亦然
领域层:包含实体和值对象,这是按照面向对象领域对象封装数据和逻辑设计的业务逻辑的核心。如果使用存储库模式,它还可以包含存储库接口。以及不适合单个实体的逻辑的域服务。
数据访问:使用像 NHibernate 或 EF 这样的 ORM 或任何数据访问技术将实体映射到数据库表中。
基础设施/通用:基础设施代码和横切技术服务,如日志记录
我将尝试给出一个关于每一层的小例子:一个假设的不完整的例子说你想激活一个采购订单
表示层(MVC):
public class PurchaseOrderController
{
public ActionResult ActivateOrder(int id)
{
var response = _orderManagementService.ActivateOrder(id); // Call distributed service (Web Service)
if(response.Succeed)
return new SuccessActionResult();
else
return new FailedActionResult(response.Reason);
}
}
分布式服务层(Web Service):
public class OrderManagementWebService : IOrderManagementService
{
private readonly IOrderExecutionService _orderService;
public OrderManagementWebService(IOrderExecutionService orderService)
{
_orderService = orderService; // Order Service from application service
}
public ActivationResult ActivateOrder(int id)
{
var response = _orderService.ActivateOrder(id); // Call the application layer to execute the logic
if(
}
}
应用层:
public class OrderExecutionService : IOrderExecutionService
{
private IOrderRepository _orderRepository;
public OrderExecutionService(IOrderRepository orderRepository)
{
_orderRepository = orderRepository;
}
public ActivationResult ActivateOrder(int id)
{
var order = _orderRepository.GetById(id); // Get the order from repository
try
{
order.Activate(); // Call business logic inside the order entity
return new ActivationResult { Success = true } ;
}
catch(ActivationException ex)
{
LogFactory.GetLog().Exception(ex); // Call log from infrastructure layer
return new ActivationResult { Success = false, Reason = ex.Message } ;
}
}
}
领域层:
public class PurchaseOrder : Entity
{
// Properties and fields (Data)
public int Id { get; private set; }
public Customer Customer { get; private set; }
// Methods (contains business logic)
public void Activate()
{
if(Customer.IsBlacklisted)
throw new InvalidCustomerException(...);
if(_lineItems.Count == 0)
throw new NoItemsException(...);
this.SetStatus(OrderStatus.Active);
.....
}
}
存储库(数据访问层):
public class OrderRepository : IOrderRepository
{
public PurchaseOrder GetById(int id)
{
// data access code to access ORM or any data access framework.
}
}
基础设施:
public class Logger : ILogger
{
public void Exception(Exception ex)
{
// write exception to whatever
}
}