Answer the question
In order to leave comments, you need to log in
How to make complex queries using repositories and joining different tables?
Greetings!
I was taught to work with domain driven design. As part of this approach, I was asked to share data access across repositories. That is, for one database table in our company, it is proposed to use one repository. With simple queries, everything is clear.
How to use data from several tables within this approach? At the database level, this is solved simply - join and subqueries. At the repository level, it's not clear to me how to do this. I see two options. Use requests with join and subqueries to get all the necessary data as a special case of some task, make a separate method in the repository that will be able to do this. The second option is to receive data with different requests and combine them at the service level with some kind of mapping.
Mappings in both cases will have to be used because, for example, if we want to send a list of orders with goods via the API, then the order object will have an array field with a collection of goods objects of the corresponding type.
All of this is slow.
Probably I do not understand how to properly design such an application. Please help me to solve this simple problem. Thanks in advance to all who respond.
Answer the question
In order to leave comments, you need to log in
solution to this simple problem.
Tip on the forehead. Following OOP, you describe the objects of the system as objects from the outside world. And everything is great with you until you start normalizing the database schema. Apparently this worries you?
But look at it all from the point of view of the principle of closure. What the repository does inside should not bother anyone. It must have an interface for the overlying layers, and that's it.
And for repositories, do not create them for each table, this is superfluous.
There are business models, for example Group and User
In addition to the obvious 2 tables, 3 UserInGroup is added
You obviously need only 2 repositories, 3 repositories will be superfluous and will only confuse
You can use views from the database side, or storage factories from the code side.
It's fashionable to just statically bake into a separate class first if that's really the bottleneck. If the pattern shows itself, then it can be issued in the form of a factory.
Look in C # in the Entity Framework, almost as you said, only there the request is constructed from collections of classes that are able to map a specific subject area. But there is already a full-fledged ORM with optimizations such as a deferred query to the database: the Linq-To-Entity technology implements relational logic, so it is fashionable to postpone a cumbersome SQL query until the very last moment.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question