Pergunta

I'm trying to create a simple three tier 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.

Foi útil?

Solução

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

Online References

Hope that helps.

Outras dicas

Try searching for the repository pattern, I think it'll answer your needs.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top