I am developing a project integrated with Dependency Injection (just for reference, I'm using Unity).
The problem is that I have some Manager classes with several methods and in many cases I have dependencies used only in one method.
public class UserReportManager : IUserManager
{
private UserRepository UserRepository {get;set;}
private TeamRepository TeamRepository {get;set;}
private CacheRepository CacheRepository {get;set;}
private WorkgroupRepository WorkgroupRepository {get;set;}
public UserManager(UserRepository userRepository,
TeamRepository teamRepository,
CacheRepository cacheRepository ,
WorkgroupRepository workgroupRepository,
... // Many other dependencies
)
{
UserRepository = userRepository;
TeamRepository = teamRepository;
CacheRepository = cacheRepository ;
WorkgroupRepository = workgroupRepository;
... // Setting the remaining dependencies
}
public void CreateReportAboutMostActivesUsers(){
// Uses only UserRepository
}
public void CreateReportAboutUserInsideTeams(int teamID){
// Uses UserRepository and TeamRepository
}
public void CreateReportAboutUserHistory(){
// Uses UserRepository and CacheRepository
}
public void CreateReportAboutUsersInsideWorkgroup(int workgroupID){
// Uses UserRepository and WorkgroupRepository
}
}
The UserManager is instantiated in this way:
DependencyFactory.Resolve<IUserManager>();
Note: DependencyFactory is just a wrapper to simplify the access to the UnityContainer
Is it OK to have classes like that in the example? Is there a better way to implement avoiding to instantiate all the unnecessary dependencies?