After studing the Autofac documentation, and some stackoverflow questions
Best Practices for IOC Container,
IoC Container. Inject container
I understand that I need put container on highes level,and after that pass it thrue needed classes.
f.ex I have high level class controller
public class AccountController : Controller
{
private readonly IUserManager userManager;
public AccountController(IUserManager userManager) //I configure Global.asax and there will
{ //be UserManager class. OK.
this.userManager = userManager;
}
}
on lower level I have UserManager class whit parameter IUnitOfWork. Should I put there new UnitOfWork instance or do it with some container?
public class UserManager : IUserManager
{
readonly IUnitOfWork _unitOfWork;
public UserManager (IUnitOfWork unitOfWork)
{
this._unitOfWork = unitOfWork;
}
}
Piece of autofac configuration.
conteinerBuilder.RegisterType<UserManager>().As<IUserManager>()
.WithParameter("unitOfWork",new UnitOfWork()) //This is what bothers me
//Like I understand we wanna
//work with abstraction not with some
//implementations but there I must use
//UnitOfWork
Tell me which way to dig, I'm completely confused. I'll be greateful for any help. =)
IUserManagertoUserManagerand the second ofIUnitOfWorktoUnitOfWork. I personally don't use AutoFac but any container should resolve recursively so that when it resolves the UserManager it will automatically use UnitOfWork registration. - Wiktor Zychla