Autofac Trick&Tips
- Skanowanie dla typów
Skanowanie w bibliotece "Services", gdzie nazwa interfejsu kończy się nazwą "Service"
protected void Application_Start() { var builder = new ContainerBuilder();builder.RegisterControllers(typeof(MvcApplication).Assembly);
builder.RegisterAssemblyTypes(Assembly.Load("Services")) .Where(t => t.Name.EndsWith("Service")) .AsImplementedInterfaces();var container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
AreaRegistration.RegisterAllAreas(); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); }
Link: http://docs.autofac.org/en/latest/register/scanning.html
Skanowanie w bibliotece "Services", gdzie interfejs dziedziczy po IDependency
protected void Application_Start() {var builder = new ContainerBuilder();builder.RegisterControllers(typeof(MvcApplication).Assembly); builder.RegisterAssemblyTypes(Assembly.Load("Services")) .Where(t => t.GetInterfaces().Contains(typeof(IDependency))) .AsImplementedInterfaces();var container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
AreaRegistration.RegisterAllAreas(); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); }
Brak komentarzy:
Prześlij komentarz