我正在使用 Autofac 并希望解决当前程序集的正确实现
我有一个 DataContextFactory 接口和类:
Public Interface IDataContextFactory
Inherits IDisposable
Function GetDataContext() As IDataContext
End Interface
和接口的实现
Public Class CDataContextFactory
Implements IDataContextFactory
Private m_oDbContext As IDataContext
Public Sub New(ByVal i_oDbContext As IDataContext)
m_oDbContext = i_oDbContext
End Sub
Public Function GetDataContext() As CoreData.IDataContext Implements CoreData.IDataContextFactory.GetDataContext
Return m_oDbContext
End Function
End Class
所以现在我在每个注册的程序集中都有不同的 IDataContext 实现。例如,我有一个名为 ReportData 的程序集,其中包含数据上下文
Public Class CReportDataContext
Inherits DbContext
Implements IDataContext
---
End Class
以及另一个程序集 CommonData 中的一个实现
Public Class CFacadeDataContext
Implements IDataContext
---
End Class
然后我在每个程序集中都有我的 IRepository 的实现。例如
Public MustInherit Class CBaseReadRepository(Of T As {IEntity, Class})
Implements IReadRepository(Of T)
Private m_oDataContextFactory As IDataContextFactory
Private m_oDataContext As IDataContext
Protected ReadOnly m_oObjectDataSet As CQuery(Of T)
Public Sub New(ByVal i_oDataContextFactory As IDataContextFactory)
m_oDataContextFactory = i_oDataContextFactory
m_oObjectDataSet = DataContext.ObjectDataSet(Of T)()
End Sub
----
End Class
那么我该如何解决DataContextFactory将解析Assembly ReportData中的CReportDataContext和Assembly CommonData中的CFacadeDataContext
这是我的 ContainerBuilder 注册:
Dim builder As New ContainerBuilder()
Dim oData = Assembly.Load("ReportData")
builder.RegisterAssemblyTypes(oData).Where(Function(t) t.Name.EndsWith("DataContext")).As(Of IDataContext) _
.AsImplementedInterfaces.SingleInstance
oData = Assembly.Load("CommonData")
builder.RegisterAssemblyTypes(oData).Where(Function(t) t.Name.EndsWith("DataContext")) _
.AsImplementedInterfaces().SingleInstance
builder.RegisterAdapter(Of IDataContext, IDataContextFactory)(Function(x) New CDataContextFactory(x))
谢谢