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
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

66%
+2 −0
Q&A How to separate DB query logic from the application other than implementing a repository on top of an ORM?

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,...

posted 3y ago by Alexei‭

Answer
#1: Initial revision by user avatar Alexei‭ · 2021-02-04T07:47:43Z (about 3 years ago)
 > 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<T>).

 > 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<TEntity> Set<TEntity>() where TEntity : class;
   IQueryable<TEnt> ReadSet<TEnt>() where TEnt : class;
   Task<int> 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](https://www.thereformedprogrammer.net/is-the-repository-pattern-useful-with-entity-framework-core/).