Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

Feedback on unit of work and repositories

$
0
0

Hey, I've tried to implement the repository pattern with unit of work found in this example in the docs for my asp.net core 2.0 app.

I'm unsure if I did it correctly and i would be very happy on getting feedback on it! Any comment on if I did something wrong or can improve is very welcome!

Here is the code on github.

So far it seems to work. But i'm not certain I understand it completely. I'm not even sure if I actually use the same context for both entities because the constructor for the GeneralRepository takes an ApplicationDbContext. Now the idea is that the unitofwork passes it's own context to this contructor. But I fear the dependency injection will automatically create a new one for each time I use the GeneralRepository. Meaning; if I do this in the controller:

var model = new IndexViewModel
 {
          Companies = await _unitOfWork.CompanyRepository.GetAsync(),
          Persons = await _unitOfWork.PersonRepository.GetAsync(includeProperties: "Company")
 };

I fear this will actually create two contexts. one for each? So in reality the dbcontext would be instantiated three times, one in unitofwork then one for each repository? If that is the case then it would be better to just do it the normal way withot unitof work. But I don't know if this is true or how to find out.


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>