Data Transfer Objects in BLL layer or a separate layer?

65 views Asked by At

I have:

  • DAL layer -> entities and DbContext (Entity Framework)
  • BLL layer -> services
  • UI layer -> Razor Pages

My services in BLL layer converts entities to data transfer objects and returns data transfer objects to UI layer. The UI layer also passes data transfer objects to BLL service.

Should I place data transfer objects in BLL layer or create a separate layer?

1

There are 1 answers

0
David Guida On

Each layer should have its own Models/ViewModels.

The Persistence layer has EF Entities. Those should not surface to the upper layers. Otherwise, you'll be forever tied to EF.

The BL contains your Domain Models. The Persistence layer will take care of the mapping Entity->Domain Model (and vice-versa).

The UI has its own ViewModels and takes care of the mapping.

Now, with that said, you could manage to use Domain Model classes as EF Entities, achieving what is called "persistence ignorance". This usually requires some "magic", and is normally done by avoiding any kind of library-specific data annotation attribute on the properties (like [Table], [ForeignKey] and so on). The db mapping is handled via IEntityTypeConfiguration.