Open Closed

Generic Unit of Work with Generic Repository #1315

ofir1234 created

Hi. I read a lot of the abp documentation about the generic repository and the uow pattern. But actually I find it hard to find if you have this ability : a generic unit of work working with a generic repository. The concept is explained widely here: <a class="postlink" href=""> ... rk-Pattern</a> (I'm talking about the mix between the generic uow and the generic repository they are talking about in the end of the article)

Usage example:

var x = uow.Repository<Contact>().GetAll().ToList());
var y = uow.Repository<Manager>().GetAll().ToList());

This means that I can hold (in my application service) an object called uow and then (at runtime) decide to access the contacts repository or the managers repository or any other repository I want. This also saves me from injecting (in the constructor of my application service) all of the repositories I need, for example :

Public MyAppService(IRepository<Contact> cr, IRepository<Manager> mr)
// ...

If I have something like 15 types of entities, this constructor is going to be huge and its a bit overhead. The best approach is if could only inject this uow object and select my repository from this uow (as I showed in the previous code and in the article example).

So, do you have any support for this concept / can you give a short example for how to do it in ASP.NET Boilerplate?

Thanks :)

1 Answer(s)
  • 0
    hikalkan created
    Support Team

    Inject IIocResolver and use it like that:

    var x = _iocResolver.Resolve<IRepository<Contact>>().GetAllList();

    So, this is almost identical with your case.


    I don't like this approach for a few reasons:

    1. It's better to explicitly declare dependencies in the constructor. So, inject which repository you need. If you have 15 repository injections, you probably doing something wrong. Follow Single Responsibility Principle.

    2. ABP's Unit Of Work is ambient. We don't have to explicitly inject and use it. We can control UOW scope by attributes and UOW manager if needed (see UOW docs for more: <a class="postlink" href=""> ... UowDetails</a>)