我正在尝试创建一个简单的三层项目,并且正在按计划实现业务逻辑层,现在,这就是我的 BL 的样子
//The Entity/BO
public Customer
{
public int CustomerID { get; set; }
public string CustomerName { get; set; }
public UserAccount UserAccount { get; set; }
public List<Subscription> Subscriptions { get; set; }
}
//The BO Manager
public class CustomerManager
{
private CustomerDAL _dal = new CustomerDAL();
private Customer _customer;
public void Load(int customerID)
{
_customer = GetCustomerByID(customerID);
}
public Customer GetCustomer()
{
return _customer;
}
public Customer GetCustomerByID(int customerID)
{
return _dal.GetCustomerByID(customerID);
}
public Customer GetCustomer()
{
return _customer;
}
public UserAccount GetUsersAccount()
{
return _dal.GetUsersAccount(_customer.customerID);
}
public List<Subscription> GetSubscriptions()
{
// I load the subscriptions in the contained customer object, is this ok??
_customer.Subscriptions = _customer.Subscriptions ?? _dal.GetCustomerSubscriptions(_customer.CustomerID);
return _customer.Subscriptions;
}
您可能会注意到,我的对象管理器实际上只是我的真实对象 (Customer) 的容器,这就是我放置业务逻辑的地方,这是一种将业务实体与业务逻辑解耦的方式,这就是我的方式通常使用它
int customerID1 = 1;
int customerID2 = 2;
customerManager.Load(1);
//get customer1 object
var customer = customerManager.GetCustomer();
//get customer1 subscriptions
var customerSubscriptions = customerManager.GetSubscriptions();
//or this way
//customerManager.GetSubscriptions();
//var customerSubscriptions = customer.Subscriptions;
customerManager.Load(2);
//get customer2
var newCustomer = customerManager.GetCustomer();
//get customer2 subscriptions
var customerSubscriptions = customerManager.GetSubscriptions();
如您所见,它一次只包含 1 个对象,如果我需要管理客户列表,我可能必须创建另一个管理器,如 CustomerListManager
我的问题是,这是实现三层/层设计 BL 的正确方法吗? 或者关于如何实现它的任何建议。谢谢。
最佳答案
正如其他人之前提到的,您应该看看 Repository 模式。我还建议检查工作单元模式以及领域驱动设计,以了解您应用程序的总体架构。
您还可以查看 .NET 的对象关系映射 (ORM) 框架,例如 Entity Framework或 NHibernate如果这是您的选择。
为了让您抢先一步,这里有一些重要的资源可以帮助您走上正确的道路:
书籍
- Applying Domain-Driven Design and Patterns通过吉米·尼尔森
- Domain-Driven Design埃里克埃文斯
- Domain Driven Design Quickly (免费电子书)
在线引用资料
- Design Patterns for Data Persistence通过杰里米·米勒
- Repository is the new Singleton通过 Ayende Rahien
- The Repository Pattern (MSDN)
- The Unit Of Work Pattern And Persistence Ignorance通过杰里米·米勒
希望对您有所帮助。
关于c# - 实体管理器 : 3 Tier/Layer Application Question,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6318462/