Look at my Controller (I'm using Dependency Injection to manager dependencies):
public RoleController(IRoleRepository roleRepository, ISiteRepository siteRepository, IUserRepository userRepository, IDbContext dbContext)
{
_roleRepository = roleRepository;
_siteRepository = siteRepository;
_userRepository = userRepository;
_dbContext = dbContext;
}
Having a class with many dependencies is a code smell? Right?
But, In my example I need to associate Users
and Sites
in a Role
, then I need to these dependencies to doing this association.
Some people on a mailing list I was told that having too many dependencies is a sign that something is probably wrong. But I see no other way. I separated my responsibilities, there is something in that situation I do not know how to treat? Is something wrong?
Update:
I need Repositories and DbContext because DbContext is my UnitOfWork, repositories don't save.
This example is a simple CRUD with some other functionalities like Associations in the View with a GRID.
Update 2:
I'm using a architecture where my UI Layer is the MVC.
You could instantiate a new context in the repository, but then if you used multiple repositories in one controller, each would end up with a separate context. Later you'll use multiple repositories in the Course controller, and you'll see how a unit of work class can ensure that all repositories use the same context.
In Spring MVC, we can create multiple controllers at a time. It is required to map each controller class with @Controller annotation.
Question: Can there be two+ models in 1 controller? Answer: yes. Create a wrapper model, put other two models in it.
If you want to use repositories directly in controller, then you must write domain logics in the controller and it is not a good practice. Controllers have to be small and only forward requests to domain logic or services. So in my idea it's better to use services in controller.
I don't believe it's a bad thing, given that you manage dependencies with a good DI framework (i.e. not by using poor man's DI). This way, you explicitly say that the controller will need all these things, because it will. (Note that in many other parts of your application, this might not be a valid argument - the controller is special in the way that it is where you control and direct the program flow, so there's a natural explanation why it needs to see lots of parts of the application...)
However, if you really want to limit the number of dependencies in this specific case, it could make sense to create a MembershipService
, which does all the work concerned with Users
, Sites
and Roles
. That would then have a dependency on those three repositories, and your controller would only have a dependency on the membership service.
In response to your update: You could register the unit of work (i.e. the db context) as a "per web request" singleton - this is possible with Castle Windsor and many other DI frameworks. Then you can let your repositories depend on it and do all the changes, and let the controller depend on it for saving, and they will all get the same instance handed to them by the DI framework.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With