I recently decided something similar by making a small redesign that made everything a lot easier. This might work for you too. You can try to remove specific interfaces, such as ITeamEmployeeRepository and ILoanedItemRepository from your project. The way I did it was using extension methods. Here is an example:
public static class RepositoryExtensions { public static TeamEmployee GetById( this IRepository<TeamEmployee> repository, int id) { return repository.Single(e => e.TeamEmployeeId == id); } public static IQueryable<Salesman> GetActiveSalesmen( this IRepository<ISalesmanRepository> repository) { return repository.Where(salesman => salesman.Active); }
After that, I created an IRepositoryFactory that allowed me to create repositories of a specific type:
public interface IRepositoryFactory { IRepository<T> CreateNewRepository<T>(); }
Using this interface, it is easy to create an implementation of this factory that asks the container to create a specific Repository<T> . RepositoryFactory might look like this:
public class RepositoryFactory : IRepositoryFactory { public IRepository<T> CreateNewRepository<T>() { return ObjectFactory.GetInstance(typeof(Repository<T>)); } }
With this construct, you only need to register a specific RepositoryFactory using the IRepositoryFactory interface, and you're done. Instead of injecting an IRepository<ITeamEmployeeRepository> into an old project, you now enter an IRepositoryFactory and let the client call the CreateNewRepository<T> method. Due to the use of extension methods, you can invoke type-specific methods in the repository.
Another advantage of this is that you do not need to ITeamEmployeeRepository methods that were originally defined in ITeamEmployeeRepository for each implementation.
This project worked very well in my situation, especially because my IRepository<T> interfaces use expression trees. Of course, it is not possible for me to see if such a design works for you, but I hope that it does.
Good luck.
Steven
source share