I have a three-layered application. Each of parts have dependencies from another part of my solution. I used to implement IDependencyResolver
inside of MVC project. This is a wrong way, because it leads to violating the architectural rules of layer separation. My MVC project had reference to DAL layer. It's a bad coding practices. I know that I can create a separate class library project. It will has references to any other projects of my solution. It will resolve all dependencies. I heard that it not the best way. There is a better way. Each projects of my solution should resolve it owns dependencies. I don't understand how to put it all together. So I found this useful article: Dependency Injection Best Practices in an N-tier Modular Application but it seems too difficult and complex. Is there another way? I have a similar structure of solution. UserRepository
returns a requsted user.
public interface IUserRepository
{
IEnumerable<UserEntity> GetAll();
}
public class UserRepository : IUserRepository
{
public IEnumerable<UserEntity> GetAll()
{
// some code
}
}
UserService
can has a few different dependencies.
public interface IUserService
{
IEnumerable<UserModel> GetAll();
}
public class UserService : IUserService
{
private readonly IUserRepository userRepository;
private readonly ISecondRepository secondRepository;
private readonly IThirdRepository thirdRepository;
public UserService(IUserRepository userRepository, ISecondRepository secondRepository, IThirdRepository thirdRepository)
{
this.userRepository = userRepository;
this.secondRepository = secondRepository;
this.thirdRepository = thirdRepository;
}
public IEnumerable<UserModel> GetAll()
{
// some code
}
}
And finally UserController
constructor might has a lot of different dependencies.
My question is about what is the right and the simplest way to resolve these dependencies avoiding violating the architectural rules?