我正在尝试创建一个简单的三层项目,并且正在实施业务逻辑层,现在,这就是我的 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;
}
您可能会注意到,我的对象管理器实际上只是我的真实对象(客户)的容器,这是我放置业务逻辑的地方,一种将业务实体与业务逻辑解耦的方式,这就是我通常使用它的方式
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 的正确方法吗?或有关如何实施它的任何建议。谢谢。