So i'm using Autofac in my application that looks as follow (from top -down approach).
The project is in MVC 4 and i added the AutoFac.MVC4 beta.
- Web App Client (the web application - presentation layer): namespace = AppBase.Web
- Web App Core (eg. various actionresults, ...): namespace = AppBase.Web.Core
- Services (eg. AuthenticationService, MemberService, TaskService): namespace = AppBase.Service
- Data (includes Repositories, ...): namespace = AppBase.Data
- Domain (POCO objects for Code Fist): namespace = AppBase.Domain
I included Autofac in my Web App Client with the following initialization code (for now):
public void RegisterContainersUsingAutofac()
{
var builder = new ContainerBuilder();
builder.RegisterModelBinders(Assembly.GetExecutingAssembly());
builder.RegisterModelBinderProvider();
builder.RegisterControllers(typeof(MvcApplication).Assembly);
IContainer container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
}
Web App Client references the core, domain and services layer. The Service layer references the Data layer (for Repositories).
I have an interface in my data layer called iRepository, all my Repositories implement these
I'm wondering, what code should i add to my "RegisterContainersUsingAutofac" method, to automaticly (in a generic way) add all Repositories and Services, so i shouldn't add them myselve everytime. Keep in mind, the datalayer isn't referenced (i can, but i don't think it should).
PS. I used Unity in the past, but i like the generic methods that AutoFac has (in one project, i have over +/-180 lines with adding repositories and services, so this is something i don't want anymore).