vendredi 27 décembre 2019

Registering and injecting generic repositories into unit of work class with simple injector [closed]

I have a generic repository class which is being inherited from a generic repository interface. In the other hand I have a handful number of domain classes and because proper UoW & repository patterns imply that repositories must reside inside the unit of work class, I want all my repositories to reside inside the unit of work class. But I don't want to inject all those generic repositories into unit of work class one by one. Instead I have a GetRepository method in my unit of work class with the following signature:

IRepository<T> GetRepository<T>() where T : class , IEntity;

The problem is I'm using a RepositoryFactory class which actually is a wrapper around the container and I don't like it because it introduces service locator anti pattern.

Now what I want is to know how to register all of these generic repositories with a single registration command with a scoped lifestyle and also how to inject them as an IEnumerable of some type like IEnumerable<IRepository<IEntity>> into my unit of work's constructor, So later to be able to return the proper repository from the list with invoking unit of work's GetRepository() method instead of using RepositoryFactory service locator class?

All suggestions or code samples are much appreciated.

Aucun commentaire:

Enregistrer un commentaire