Complex querys with repository template

0

I am building a project with the Web API and for this I am building on the Onion architecture.

To separate the layers I am using the Repository Pattern (Repository Pattern) for accessing data and one layer above is the services layer.

In the service layer I inject the repositories and in that way I can do a CRUD in a very clean way and everything is perfectly separate.

What I do not like is that when I have to make a more complicated operation with linq as a join , first I have to call the Repository of table X and execute a Get to get data, then I have to do that same operation with the Repository of table Y.

Once I have this data I can do a join , this is not optimal since before doing the join I have to make 2 calls to the database to get the data.

I have searched on Google but I can not find a solution for this design flaw, has anyone else encountered this problem? What solution have you implemented?

    
asked by betoramiz 31.01.2018 в 03:06
source

1 answer

0

Very well, I will propose the solution that I found to my problem.

In my main module, called dashboard that shows a kind of mini-report, this report is a query in database that involves three tables (clients, credit and payments).

My problem is that I wanted to make a join of the clients table with the payments and credits table. To achieve this, he had to consult three repositories, first obtain customer information, then credit information and finally the payment information, and finally make a join with the three data sets.

Obviously it is not an optimal approach since there are too many calls to the database. The truth is I do not know which is the most optimal approach to solve this problem, my conclusion is that the repository pattern is not the best for this type of scenarios.

The way I solved this is as follows: This query seemed to me more appropriate to leave it in a method within the Customers repository, with a very specific name (ObteneReporteDepagosDelMes) and within this method I wrote the linq query that involves the three models.

    
answered by 25.02.2018 / 00:31
source