Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics

Dashboard
Notifications
Mark all as read
Q&A

How to separate DB query logic from the application other than implementing a repository on top of an ORM?

+5
−1

I've been doing a lot of reading on implementing the repository pattern in C# projects and found controversy, or shall I say some strong criticism, made by seemingly very smart people with previous experience with the pattern, saying that one is better off not implementing a repository on top of an ORM like Entity Framework Core.
The argument made is that EF Core is already a repository implementation, with the Context being the unit of work and each DbSet being a repository. (I don't normally do unit testing so whatever point made about that is irrelevant to me.)

p.s. I'm not sure whether the criticism applies only to generic repositories or not.

The thing is, I'm failing to see a sound alternative to separate data access code from my Application layer (business logic implementation). The suggestions I found include consuming the ORM directly from the Application layer, or doing CQRS, or even moving to a microservices architecture. The latter two I'm not interested in; the first doesn't give me the flexibility of implementing my business logic without polluting it with query logic, particularly considering that more than one database will most likely be required in this application.

Here's how I want my architecture to look like:

Architecture diagram

(n.b. I haven't decided about using ViewModels/DTOs for the WebApp and WebAPI instead of referencing Domain models, yet.)

For those familiar with the criticism/argument I mention, I'd like to ask how else can one achieve proper separation of concerns in my proposed architecture? Or, in other words, is there a better alternative to implementing the Data Persistence layer other than via the repository pattern?

Fun fact: This is the architecture I was planning (and started implementing) for the Codidact Core software about a year ago, before my hiatus.

Thank you!

Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

4 comments

Posted a more elaborate/specific version of this to Software Engineering SE: How best to implement a Data Access layer for this architecture? Marc.2377‭ 28 days ago

Check this article for a good insight about your issue. Alexei‭ 28 days ago

Your question from Software Engineering SE is no longer available (I guess it was removed in the meantime). Alexei‭ 23 days ago

I deleted it myself due to poor reception. Never liked that site anyway. Here's an archived version. Marc.2377‭ 22 days ago

1 answer

+2
−0

p.s. I'm not sure whether the criticism applies only to generic repositories or not.

This applies to generic repositories as most of their operations (e.g. get entity by id, update the entity, delete entity) are already being done by the DbSet).

consuming the ORM directly from the Application layer (..) the first doesn't give me the flexibility of implementing my business logic without polluting it with query logic, particularly considering that more than one database will most likely be required in this application.

My approach is hybrid in this case. If your application is mainly using a relational database for the operational parts (CRUD) and some others for querying external information, notifying other APIs etc. I would take the following approach:

  • consuming the ORM directly from the Application layer for the use cases that involve the primary relational database. That is, your application layer might inject the ICustomDbContext which exposes the minimum required to get/update the data:

    DbSet Set() where TEntity : class; IQueryable ReadSet() where TEnt : class; Task SaveChangesAsync(CancellationToken cancellationToken);

This indeed couples the application to the ORM and you have some queries in the application. However, queries are actually part of the business logic and abstractions tend to complicate things (i.e. poor performance in some cases).

It is very unlikely that you will need to change the ORM in the future, but it still works if you change the relational database.

An exception for this rule is the cases when you need to write a direct query due to performance reasons. This should be placed in another class (repository) because changing the provider might need revisiting the query.

  • interacting with other databases - if your application needs to talk to other databases, the logic for this can be included in a repository class for each case. This is required because other databases come with their own specifics and you do not want to mix those specifics with the application business logic

  • consuming APIs or other services - this can be put in a separate service class. In a clean architecture, these services are implemented in the Infrastructure layer and injected (dependency inversion) in the application layer.

Note: for extensive information about why generic repositories are not fashionable anymore and how to replace them with "generic services", check out this article.

Why does this post require moderator attention?
You might want to add some details to your flag.

4 comments

Thanks. Does your approach change in any way in the case where the secondary database is non relational? Marc.2377‭ 28 days ago

... and when some queries will be made to both databases, sharing a TransactionScope. Marc.2377‭ 28 days ago

For non-relational databases (I have little experience with them) I would use a repository for each of them. Never used a TransactionScope like this, but I guess I would have the transaction outside of the Application (in some repository or service). Is it recommended to work like this? I thought that it is recommended to have a transaction on the relational database and eventual consistency on other systems, but I guess it also depends on scalability constraints. Alexei‭ 28 days ago

Good point. I think I have one use case for updating both the relational and document datastores in one atomic operation, but for the majority of cases you are correct. Marc.2377‭ 27 days ago

Sign up to answer this question »