简体   繁体   中英

Auto-Register Generic Repositories with Simple Injector

I'm having trouble finding a way to register my generic repositories using Simple Injector (v3).

I have a base class GenericRepository<T> and an interface ILinkRepository . Both the base class and interface implement IGenericRepository<T> . My implementation of the repository, LinkRepository , inherits GenericRepository<T> and implements ILinkRepository .

Then I have a service class which takes the repository via the constructor LinkService(ILinkRepository repository) .

I want to auto-register my generic repositories via Simple Injector so I've tried this:

container.Register(typeof(IGenericRepository<>), new []
{
    typeof(IGenericRepository<>).Assembly
});

When Simple Injector tries to verify my container I get an error:

The constructor of type LinkService contains the parameter with name 'repository' and type ILinkRepository that is not registered. Please ensure ILinkRepository is registered...

So is there any other way to tell Simple Injector that ILinkRepository is a LinkRepository instance without directly registering it like below?

container.Register<ILinkRepository, LinkRepository>()

Simple Injector's batch registration facilities focus primarily on registration on generic interfaces. The ILinkRepository is a non-generic interface with a one-to-one mapping to a non-generic implementation.

If you want to batch-register these kind of non-generic abstractions, you will have to write a LINQ query yourself that reflects over the assembly and gets the types and loop over the query to register those types. Nothing fancy and nothing complicated. The documentation even shows some examples of this.

But there's a reason Simple Injector doesn't help you with this. A design with such non-generic repository interfaces with base classes is awkward to say the least, and as this article describes , such design violates three out of five SOLID principles, which is an indication that it might not be the best design from a maintainability perspective.

So my advice is to only use a generic repository interface (possibly with non-generic implementations) and move any entity specific operations to its own abstraction, such as this article and the previously referenced article describes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM