scripni
scripni

Reputation: 2164

In which layer should i put my repository?

Scenario

Data Access Layer

Business Layer

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

Upvotes: 6

Views: 5314

Answers (3)

Muflix
Muflix

Reputation: 6798

I think repository interface should be in business layer, but implementation of this interface should be in data layer. Interface enable unit testing, but repository implementation is not responsibility of business layer.

Upvotes: 2

Steve Kennedy
Steve Kennedy

Reputation: 5402

I like the accepted answer. Ideally, having a completely layer dedicated to the Repositories sounds right.

But I think, in a traditional 3-tier only application (e.g. "Data->Business->UI"), I would stick the Repositories in the Data layer. The reason I would put them in the Data Layer, is because they deal strictly with data access. The reason I would NOT put them in the Business Layer, is because they should NOT have any business logic to them.

Upvotes: 7

Oded
Oded

Reputation: 499212

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.

Upvotes: 11

Related Questions