Architecture – Business Logic Entities and Data Access Layer Entities

Architecturebusiness-logiccouplingentitylayers

I am thinking on how to structure a project. I wonder if it is a good practice to use different entity classes between the BL and DAL in order to decouple these layers.

I have worked on projects where entity classes where in a separated project/package. Then BL and DAL used the same entities.

But I can imagine that a better approach is to define an entity class for every layer and its assembly. What are the objective pros and cons of such approach ?

Best Answer

BL, entities and DAL

The purpose of the BL is to implement the domain logic that work with domain entities. The definition of these entities is business driven.

The purpose of the DAL is then to organise the persistence of the domain entities. The idea is to hide from the upper layers the ugly details of the data access and the database. If it's well done, you could change the database under the surface, and still leave the higher layers unchanged.

Unsurprisingly, the domain entities are shared across both layers: BL needs them to carry out domain activities, but DAL needs them to organise their persistence.

Decouple BL and DAL ?

Decoupling components is generally a very good idea. However, layers are not independent components. Layers are a logical groupings of related classes.

If fact , you could as well redraw your layer diagram, and imagine a layer between BL and DAL that contains the Entities. And all the sudden, the layers would appear clean and decoupled.

What are the alternatives ?

You propose as alternative to use two different kind of entities in the different layers. So you would have proper domain entities in the BL. And you would have intermediate data access entities in the DAL.

Now imagine you'd implement this perfect scheme. How would you then make the link between the BL entity and the DAL entity ? Somehow these would have to know each other to synchronize data. So you would end up having a strong coupling between the layers anyway. It's just that it would be less obvious.