C# – Separation of retrieving data and business objects between DAL and BLL Layers

Architecturebusiness-logiccdatabasenet

I did some research before posting this question. Among other questions or post, one of those is provided below. I could not get a clear mind how to determine..

Business Objects within a Data Access Layer

I have a Repository and the Business Layers call the repository to retrieve the data. For example, say I have the following classes for BLL and DAL:

class BllCustomer
{
    public int CustomerId {get; set;}
    public String Name {get; set;}
    public BllAddress Address {get; set;}
}

class BllAddress
{
     public int AddressId {get; set;}
     public String Street {get; set;}
     public String City {get; set;}
     public String ZipCode {get; set; }
}

class DalCustomer 
{
    public int CustomerId {get; set;}
    public String Name {get; set;}
    public int AddressID {get; set;}
}

class DalAddress
{
     public int AddressId {get; set;}
     public String Street {get; set;}
     public String City {get; set;}
     public String ZipCode {get; set; }
}

If the BLL wants to retrieve a Customer object, it would call GetCustomerById(customerId) in DAL.

The following are my concerns I could not get a clear mind:

  1. I cannot see how to determine what object the GetCustomerById in DAL should return? Should it return BllCustomer or DalCustomer?

  2. Where should be the retrieving (and/or converting to Business object) of the address associated with the customer?

If the DAL returns Dal objects then, the logic to retrieve and fill in the Address can only be in the BLL. If the DAL returns BLL objects, then the logic to retrieve and fill in the Address can be either in the BLL or DAL. Currently the DAL is returning the Business Objects and the logic to fill it in is in the DAL.

From what I read, I guess there is no right or wrong. From the link included above, there are people saying one way and the others are saying the other way. But how do I determine which would works best for my case?

Any help would be appreciated.

Best Answer

I cannot see how to determine what object the GetCustomerById in DAL should return? Should it return BllCustomer or DalCustomer?

It should return a DalCustomer object, returning a BllCustomer object will break the single-responsibility principle. You can view the DalCustomer object as the interface or contract consumed by the business layer (or consumer). In effect if it returned a BllCustomer the DAL would have to cater for every business layer object that calls it or could potentially call it.

Where should be the retrieving (and/or converting to Business object) of the address associated with the customer?

The conversion should be done in a view model or manager. You need to have a intermediary to call your service or data access component. If you feel the need you can have a conversion in your BllCustomer object. But then when you swap your DAL from MSSQL to Oracle for example your returned object (or interface) must remain the same.

Preferably your business layer should also be independent of your Data Layer. The Business Layer is responsible for your business rules. It is here you will add your validations using a validation framework to enforce your business rules.

Related Topic