How to use Repository Interface that uses Generics with Dependency Injection?

14,481

Solution 1

Is this OK?

Sure. There's personal preference on whether to use constructor injection like you have or property injection. Constructor injection is cleaner since you don't have to have lot of parameters to your constructor, but it's safer as well.

what's the point of Unity to register Interface to concrete type

One reason is so that you can unit test MyClass without having to use your actual repository that hits a database. You can "fake" out the repository to return hard-coded values to test against.

Solution 2

As noted by D Stanley, a dependency must be a concrete interface. Otherwise, where are you going to declare T? Your dependent class could be generic, but you still have to say "T is a Person" at some point.

That said, Unity handles registering generic types pretty nicely.

Let's say you implement IRepository<T> with a generic class Repository<T> that wraps a DbSet<T> (or whatever).

The following registration and resolves will then work (which includes injecting into any constructors):

container.RegisterType(typeof(IRepository<>), typeof(Repository<>));

// no specific registration needed for the specific type resolves
container.Resolve(<IRepository<Person>);
container.Resolve(<IRepository<Order>); 

If you needed a specific override of a type (says the Items repository is special for whatever reason so it has a fully implemented ItemRepository class), just register that specific implementation after the generic one:

container.RegisterType<IRepository<Item>, ItemRepository>();

Resolving IRespository<Item> will now get your specific implementation.

For the record, I think this can only be done in code, and not configuration files. Someone feel free to correct that assumption.

Solution 3

You could consider using an Aggregate Service that bundles several other services together, but you should also look carefully to see if your MyClass is trying to do too much; having a very large number of dependencies can be a indication of that.

Solution 4

This seems ok except one missing point; you need UnitOfWork pattern to enable transactions. Without UnitOfWork pattern applied, all repositories try to commit db operations on different contexts.

Share:
14,481
atconway
Author by

atconway

I am a Practice Lead Consultant designing and building applications in web and .NET and surrounding technologies with a concentration in the following: JavaScript/TypeScript/Angular/React, C#/VB.NET, ASP.NET Core/MVC, Web API/WCF, EF, and SQL Server placing an emphasis on OOP, Architecture, and Design. I thoroughly enjoy web and Microsoft development technologies and have been a proponent of their languages and platforms since I began work as an engineer 20+ years ago.

Updated on June 05, 2022

Comments

  • atconway
    atconway almost 2 years

    I am attempting to use the following Generic Repository Interface for DI and constructor injection:

    public interface IRepository<TEntity> : IDisposable where TEntity : class
    

    The problem is in order to define an instance of the Interface, I must provide the class type like this:

    private IRepository<Person> _personRepository;
    

    The issue with this is if I'm using DI (and I'm using Unity for IoC framework), then I have to define multiple instances in my constructor to get all repository interfaces I need to work with like this:

    public MyClass(IRepository<Person> personRepository,
                   IRepository<Orders> ordersRepository,
                   IRepository<Items> itemsRepository,
                   IRepository<Locations> locationsRepository)
    {
      _personRepository = personRepository;
      _OrdersRepository = ordersRepository; 
      _itemsRepository = itemsRepository;
      _locationsRepository = locationsRepository;
    }
    

    Questions:

    1. Is this OK?
    2. If not where am I lost on this concept?
    3. Even if this is proper, what's the point of Unity to register Interface to concrete type? I've already done it because the generic repository forced me on declaration.

    Please help clear this up for me, and I appreciate all your help!