所以我在我的应用程序中使用Autofac,看起来如下(从自上而下的方法)。
该项目在MVC 4中,我添加了AutoFac.MVC4测试版。
我在Web App Client中包含了Autofac,其中包含以下初始化代码(暂时):
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引用核心,域和服务层。 服务层引用数据层(对于存储库)。
我的数据层中有一个名为iRepository的接口,我的所有存储库都实现了这些
我想知道,我应该在我的“RegisterContainersUsingAutofac”方法中添加什么代码,以自动(以通用方式)添加所有存储库和服务,因此我不应该每次都添加myselve。 请记住,数据层未被引用(我可以,但我认为不应该这样)。
PS。我过去使用过Unity,但我喜欢AutoFac的通用方法(在一个项目中,我有超过+/- 180行添加存储库和服务,所以这是我不想要的东西)。
答案 0 :(得分:2)
直到最近我才使用Unity.Mvc并切换到Autofac。我对Unity.Mvc的问题是,无论出于何种原因,它会工作一分钟,而不是下一个,它似乎非常不稳定。
使用和配置Autofac是一件轻而易举的事情,自从我开始使用它以来,我没有遇到任何问题。
我使用WebActivator,因此我的大多数配置都在自己的类中作为引导程序对象,而不是用一堆代码乱丢Global.asax文件。
下面是Autofac bootstrapper的(VB.NET)代码。你会看到我原来每个服务对象和每个存储库对象都在该文件中注册,但它们现在被注释掉了,我使用了自动注册功能。
该项目正在使用Mvc4。
#Region "Imports"
Imports System.Reflection
Imports Autofac
Imports Autofac.Integration.Mvc
Imports MyCompany.Data.Interfaces
Imports MyCompany.Data.Repositories
Imports MyCompany.Services
Imports MyCompany.Services.Interfaces
Imports MyCompany.Web.Mvc.Public.Bootstrap
Imports MyCompany.Web.Mvc.Public.Services
#End Region
#Region "Assembly Meta"
' This tells the app to run the "Start" method prior to running the App_Start method in Global.asax
<Assembly: WebActivator.PreApplicationStartMethod(GetType(AutofacDI), "Initialize")>
#End Region
Namespace MyCompany.Web.Mvc.Public.Bootstrap
''' <summary>
''' Class to setup dependency injection and register types/services.
''' </summary>
''' <remarks></remarks>
Public NotInheritable Class AutofacDI
''' <summary>
''' Method to register the Autofac dependency injection component.
''' </summary>
''' <remarks>
''' This line of code below could alternatively be placed in Global.asax App_Start(), doing
''' so in this manner ensures that this gets run "PreStart".
''' </remarks>
Public Shared Sub Initialize()
' Create Unity dependency container.
Dim dependencyContainer = BuildIocContainer()
' Set DI resolver
DependencyResolver.SetResolver(New AutofacDependencyResolver(dependencyContainer))
End Sub
''' <summary>
''' Registers the IOC types/services.
''' </summary>
''' <returns></returns>
''' <remarks></remarks>
Private Shared Function BuildIocContainer() As Autofac.IContainer
Dim builder = New ContainerBuilder
With builder
' Register Controllers
.RegisterControllers(Assembly.GetExecutingAssembly())
' Custom MyCompany/Mvc objects
.RegisterType(Of FormsAuthenticationService)().As(Of IFormsAuthenticationService)().InstancePerHttpRequest()
.RegisterType(Of AccountMembershipService)().As(Of IMembershipService)().InstancePerHttpRequest()
'***************************************************************
'* MyCompany service objects.
'***************************************************************
' This is auto registration, it replaces all the individual registration lines of code below.
builder.RegisterAssemblyTypes(GetType(CatalogCodeService).Assembly).
Where(Function(t) t.Name.EndsWith("Service")).AsImplementedInterfaces().InstancePerHttpRequest()
'.RegisterType(Of CatalogCodeService)().As(Of ICatalogCodeService)().InstancePerHttpRequest()
'.RegisterType(Of ChargeCodeService)().As(Of IChargeCodeService)().InstancePerHttpRequest()
'.RegisterType(Of CommentsService)().As(Of ICommentsService)().InstancePerHttpRequest()
'.RegisterType(Of CommissionService)().As(Of ICommissionService)().InstancePerHttpRequest()
'.RegisterType(Of ConfigCriteriaService)().As(Of IConfigCriteriaService)().InstancePerHttpRequest()
'.RegisterType(Of ConfigGroupService)().As(Of IConfigGroupService)().InstancePerHttpRequest()
'.RegisterType(Of ConfigQuestionService)().As(Of IConfigQuestionService)().InstancePerHttpRequest()
'.RegisterType(Of ContactService)().As(Of IContactService)().InstancePerHttpRequest()
'.RegisterType(Of CustomerProductItemService)().As(Of ICustomerProductItemService)().InstancePerHttpRequest()
'.RegisterType(Of CustomerService)().As(Of ICustomerService)().InstancePerHttpRequest()
'.RegisterType(Of OrderItemService)().As(Of IOrderItemService)().InstancePerHttpRequest()
'.RegisterType(Of OrderService)().As(Of IOrderService)().InstancePerHttpRequest()
'.RegisterType(Of PriceBreakService)().As(Of IPriceBreakService)().InstancePerHttpRequest()
'.RegisterType(Of PriceService)().As(Of IPriceService)().InstancePerHttpRequest()
'.RegisterType(Of ProductItemService)().As(Of IProductItemService)().InstancePerHttpRequest()
'.RegisterType(Of QuoteItemService)().As(Of IQuoteItemService)().InstancePerHttpRequest()
'.RegisterType(Of QuoteService)().As(Of IQuoteService)().InstancePerHttpRequest()
'.RegisterType(Of ShipmentItemService)().As(Of IShipmentItemService)().InstancePerHttpRequest()
'.RegisterType(Of ShipmentService)().As(Of IShipmentService)().InstancePerHttpRequest()
'.RegisterType(Of ShoppingCartItemService)().As(Of IShoppingCartItemService)().InstancePerHttpRequest()
'.RegisterType(Of ShoppingCartService)().As(Of IShoppingCartService)().InstancePerHttpRequest()
'.RegisterType(Of SystemTableItemService)().As(Of ISystemTableItemService)().InstancePerHttpRequest()
'***************************************************************
'* MyCompany repository objects (used by service objects above)
'***************************************************************
' This is auto registration, it replaces all the individual registration lines of code below.
builder.RegisterAssemblyTypes(GetType(CatalogCodeRepository).Assembly).
Where(Function(t) t.Name.EndsWith("Repository")).AsImplementedInterfaces().InstancePerHttpRequest()
'.RegisterType(Of CatalogCodeRepository)().As(Of ICatalogCodeRepository)().InstancePerHttpRequest()
'.RegisterType(Of ChargeCodeRepository)().As(Of IChargeCodeRepository)().InstancePerHttpRequest()
'.RegisterType(Of CommentsRepository)().As(Of ICommentsRepository)().InstancePerHttpRequest()
'.RegisterType(Of CommissionRepository)().As(Of ICommissionRepository)().InstancePerHttpRequest()
'.RegisterType(Of ConfigCriteriaRepository)().As(Of IConfigCriteriaRepository)().InstancePerHttpRequest()
'.RegisterType(Of ConfigGroupRepository)().As(Of IConfigGroupRepository)().InstancePerHttpRequest()
'.RegisterType(Of ConfigQuestionRepository)().As(Of IConfigQuestionRepository)().InstancePerHttpRequest()
'.RegisterType(Of ContactRepository)().As(Of IContactRepository)().InstancePerHttpRequest()
'.RegisterType(Of CustomerProductItemRepository)().As(Of ICustomerProductItemRepository)().InstancePerHttpRequest()
'.RegisterType(Of CustomerRepository)().As(Of ICustomerRepository)().InstancePerHttpRequest()
'.RegisterType(Of IOrderItemRepository)().As(Of IOrderItemRepository)().InstancePerHttpRequest()
'.RegisterType(Of OrderRepository)().As(Of IOrderRepository)().InstancePerHttpRequest()
'.RegisterType(Of PriceBreakRepository)().As(Of IPriceBreakRepository)().InstancePerHttpRequest()
'.RegisterType(Of PriceRepository)().As(Of IPriceRepository)().InstancePerHttpRequest()
'.RegisterType(Of ProductItemRepository)().As(Of IProductItemRepository)().InstancePerHttpRequest()
'.RegisterType(Of QuoteItemRepository)().As(Of IQuoteItemRepository)().InstancePerHttpRequest()
'.RegisterType(Of QuoteRepository)().As(Of IQuoteRepository)().InstancePerHttpRequest()
'.RegisterType(Of ShipmentItemRepository)().As(Of IShipmentItemRepository)().InstancePerHttpRequest()
'.RegisterType(Of ShipmentRepository)().As(Of IShipmentRepository)().InstancePerHttpRequest()
'.RegisterType(Of ShoppingCartItemRepository)().As(Of IShoppingCartItemRepository)().InstancePerHttpRequest()
'.RegisterType(Of ShoppingCartRepository)().As(Of IShoppingCartRepository)().InstancePerHttpRequest()
'.RegisterType(Of SystemTableItemRepository)().As(Of ISystemTableItemRepository)().InstancePerHttpRequest()
End With
Return builder.Build()
End Function
End Class
End Namespace
正如您所看到的,Autofac自动注册功能取代了使用Unity.Mvc所需的40多行代码(我将这些行留下并对其进行评论以突出显示)!!
享受!!
答案 1 :(得分:1)
您是否考虑过使用命名约定? http://code.google.com/p/autofac/wiki/Scanning
var assemblyToScan = Assembly.GetExecutingAssembly();
builder.RegisterAssemblyTypes(assemblyToScan)
.Where(t => t.Name.EndsWith("Repository"))
.AsImplementedInterfaces();
builder.RegisterAssemblyTypes(assemblyToScan)
.Where(t => t.Name.EndsWith("Service"))
.AsImplementedInterfaces();
中涵盖的基本类型进行注册