Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In which layer should i put my repository?

Scenario

Data Access Layer

  • EF's generated .edmx and classes
  • Used only to access the SQL Database and pass the data forward to the business layer

Business Layer

  • Business entities : contain all validation logic, marked with the [DataContract] attribute so that they can be passed as parameters to my web service

Problem

I want to use the repository pattern with this approach. The repository would contain all CRUD operations to be performed on the database, accepting and returning business layer entities. This means that the repository will reside in the business layer, because only the business layer can reference the data layer, not the other way round. I'm also planning to use the data layer assembly in other projects, that's why I would like to have the repository in the data layer, not the business layer (which is particular for this project).
What do you recommend? Should I keep the repository in the business layer and write one for each different business layer? Or should I keep the repository inside the data layer, not accepting or returning business entities.
Or, as an alternative, can anyone recommend a different approach, that would yield a more logical, scalable architecture?

Thanks for reading, waiting for answers

like image 445
scripni Avatar asked Aug 30 '10 10:08

scripni


People also ask

What layer does the repository interface belong to?

The repository interface belongs in the domain layer. The repository implementation should be elsewhere, but the literature is not clear. Options include: infrastructure layer (probably the most consonant with DDD precepts); application layer; persistence layer; repository layer or sub-layer. No.

How to move the mappings inside of the repository layer?

Move the mappings inside of the repository layer. This can be achieved by passing the model (instead of the specific selector) to the repository method and have the definition of the selector defined inside of the repository layer. Then why would I choose the first approach for this article?

What is the difference between a repository and a caching layer?

If the application is implementing some caching strategy to make the data available to different requests, the repository is the layer where that caching mechanism is implemented. A repository handles only one type of data (entity).

Which layer repositories belong in DDD?

Which layer Repositories belong: the Domain Layer, Persistence Layer or something in the middle? I would say there are three distinct layers in DDD applications - the inner domain layer, the outer application layer, and the external world (includes the API/UI).


1 Answers

A repository is an abstraction over the data layer - to afford persistence ignorance to your application. It should only deal with data access and nothing more. It should not have any business logic.

The repository can and should accept and return DTOs (Data Transfer Objects) - these are simple objects that do not have behavior of their own and are used to transfer data between layers.

I would put it between the DAL and the BLL and only use it for data access from the BLL.

like image 198
Oded Avatar answered Sep 29 '22 02:09

Oded