Entity Manager : 3 Tier/Layer Application Question
I'm trying to create a simple three tier开发者_C百科 project and I'm on track on implementing the Business Logic layer, right now, this is how my BL looks like
//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;
}
As you may notice, my object manager is really just a container of my real object (Customer) , and this is where I put my business logic, kinda way of decoupling the business entity to the business logic, this is how I typically use it
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();
As you can see it only holds 1 object at a time, And if i need to manage List of Customers, I probably have to create another manager like CustomerListManager
My question is, Is this the right way of implementing the BL of a three tier/layer design? Or any suggestion on how would you implement it. Thanks.
As others have mentioned before you should have a look at the Repository pattern. I would also recommend to check out the Unit Of Work pattern as well as Domain Driven Design for your application's architecture in general.
You might also have a look into object relational-mapping (ORM) frameworks for .NET like Entity Framework or NHibernate if that's an option for you.
To give you a head-start here are some great resources to get you on the right road:
Books
- Applying Domain-Driven Design and Patterns by Jimmy Nilsson
- Domain-Driven Design by Eric Evans
- Domain Driven Design Quickly (Free eBook)
Online References
- Design Patterns for Data Persistence by Jeremy Miller
- Repository is the new Singleton by Ayende Rahien
- The Repository Pattern (MSDN)
- The Unit Of Work Pattern And Persistence Ignorance by Jeremy Miller
Hope that helps.
Try searching for the repository pattern, I think it'll answer your needs.
精彩评论