在SharpArchitecture的VB.Net转换中实现StructureMap的问题

Mon*_*n69 8 vb.net structuremap asp.net-mvc ioc-container s#arp-architecture

我在VB.Net环境中工作,最近的任务是创建一个MVC环境,作为工作的基础.我决定将最新的SharpArchitecture版本(2009年第3季度)转换为VB,总体来说,经过一些头发拉动后,它已经很好了.我遇到了Castle Windsor的一个问题,我的自定义存储库接口(位于核心/域项目中)在我的测试控制器的构造函数中引用了没有注入具体实现(来自数据项目).我用这个打了一堵砖墙,所以基本上决定把Castle Windsor换成StructureMap.

我认为我已经实现了这一点,因为一切都编译并运行,并且我的控制器在引用自定义存储库接口时运行正常.现在看起来我已经/或者现在无法正确设置我的通用接口(我希望这对我所有这些都是新的有意义).当我在控制器构造函数中使用IRepository(Of T)(希望它注入Repository(Of Type)的具体实现)时,我收到以下运行时错误:

"StructureMap异常代码:202没有为PluginFamily定义的默认实例SharpArch.Core.PersistenceSupport.IRepository`1 [[DebtRemedy.Core.Page,DebtRemedy.Core,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null]],SharpArch .Core,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = b5f559ae0ac4e006"

以下是我正在使用的代码摘录(我的项目名为DebtRemedy).

我的structuremap注册表类

Public Class DefaultRegistry
    Inherits Registry

    Public Sub New()
        ''//Generic Repositories
        AddGenericRepositories()
        ''//Custom Repositories
        AddCustomRepositories()
        ''//Application Services
        AddApplicationServices()
        ''//Validator
        [For](GetType(IValidator)).Use(GetType(Validator))
    End Sub

    Private Sub AddGenericRepositories()
        ''//ForRequestedType(GetType(IRepository(Of ))).TheDefaultIsConcreteType(GetType(Repository(Of )))
        [For](GetType(IEntityDuplicateChecker)).Use(GetType(EntityDuplicateChecker))
        [For](GetType(IRepository(Of ))).Use(GetType(Repository(Of )))
        [For](GetType(INHibernateRepository(Of ))).Use(GetType(NHibernateRepository(Of )))
        [For](GetType(IRepositoryWithTypedId(Of ,))).Use(GetType(RepositoryWithTypedId(Of ,)))
        [For](GetType(INHibernateRepositoryWithTypedId(Of ,))).Use(GetType(NHibernateRepositoryWithTypedId(Of ,)))
    End Sub

    Private Sub AddCustomRepositories()
        Scan(AddressOf SetupCustomRepositories)
    End Sub

    Private Shared Sub SetupCustomRepositories(ByVal y As IAssemblyScanner)
        y.Assembly("DebtRemedy.Core")
        y.Assembly("DebtRemedy.Data")
        y.WithDefaultConventions()
    End Sub

    Private Sub AddApplicationServices()
        Scan(AddressOf SetupApplicationServices)
    End Sub

    Private Shared Sub SetupApplicationServices(ByVal y As IAssemblyScanner)
        y.Assembly("DebtRemedy.ApplicationServices")
        y.With(New FirstInterfaceConvention)
    End Sub

End Class

Public Class FirstInterfaceConvention
    Implements ITypeScanner

    Public Sub Process(ByVal type As Type, ByVal graph As PluginGraph) Implements ITypeScanner.Process
        If Not IsConcrete(type) Then
            Exit Sub
        End If
        ''//only works on concrete types
        Dim firstinterface = type.GetInterfaces().FirstOrDefault()
        ''//grabs first interface
        If firstinterface IsNot Nothing Then
            graph.AddType(firstinterface, type)
        Else
            ''//registers type
            ''//adds concrete types with no interfaces
            graph.AddType(type)
        End If
    End Sub
End Class
Run Code Online (Sandbox Code Playgroud)

我已经尝试了ForRequestedType(我认为现在已弃用)和For.IRepository(Of T)生活在SharpArch.Core.PersistenceSupport中.Repository(Of T)生活在SharpArch.Data.NHibernate中.

我的servicelocator课程

    Public Class StructureMapServiceLocator
    Inherits ServiceLocatorImplBase
    Private container As IContainer

    Public Sub New(ByVal container As IContainer)
        Me.container = container
    End Sub

    Protected Overloads Overrides Function DoGetInstance(ByVal serviceType As Type, ByVal key As String) As Object
        Return If(String.IsNullOrEmpty(key), container.GetInstance(serviceType), container.GetInstance(serviceType, key))
    End Function

    Protected Overloads Overrides Function DoGetAllInstances(ByVal serviceType As Type) As IEnumerable(Of Object)
        Dim objList As New List(Of Object)
        For Each obj As Object In container.GetAllInstances(serviceType)
            objList.Add(obj)
        Next
        Return objList
    End Function
End Class
Run Code Online (Sandbox Code Playgroud)

我的控制器类

    Public Class ServiceLocatorControllerFactory
    Inherits DefaultControllerFactory

    Protected Overloads Overrides Function GetControllerInstance(ByVal requestContext As RequestContext, ByVal controllerType As Type) As IController
        If controllerType Is Nothing Then
            Return Nothing
        End If

        Try
            Return TryCast(ObjectFactory.GetInstance(controllerType), Controller)
        Catch generatedExceptionName As StructureMapException
            System.Diagnostics.Debug.WriteLine(ObjectFactory.WhatDoIHave())
            Throw
        End Try
    End Function

End Class
Run Code Online (Sandbox Code Playgroud)

我的global.asax中的初始化内容

Dim container As IContainer = New Container(New DefaultRegistry)
ControllerBuilder.Current.SetControllerFactory(New ServiceLocatorControllerFactory())

ServiceLocator.SetLocatorProvider(Function() New StructureMapServiceLocator(container))
Run Code Online (Sandbox Code Playgroud)

我的测试控制器

Public Class DataCaptureController
Inherits BaseController

Private ReadOnly clientRepository As IClientRepository()
Private ReadOnly pageRepository As IRepository(Of Page)

Public Sub New(ByVal clientRepository As IClientRepository(), ByVal pageRepository As IRepository(Of Page))
    Check.Require(clientRepository IsNot Nothing, "clientRepository may not be null")
    Check.Require(pageRepository IsNot Nothing, "pageRepository may not be null")
    Me.clientRepository = clientRepository
    Me.pageRepository = pageRepository
End Sub

Function Index() As ActionResult
    Return View()
End Function
Run Code Online (Sandbox Code Playgroud)

当我拿出与pageepository(IR)相关的一切时,上面的工作正常.

任何有关这方面的帮助将不胜感激.

Per*_*sen 1

昨天我在实例化 IRepository(Of MyEntity) 时遇到了类似的问题。

我必须y.ConnectImplementationsToTypesClosing(GetType(IRepository(Of )))在我的 Scan 委托中声明以使 StructureMap 将泛型类型映射到其实现。

像这样:

Private Shared Sub SetupCustomRepositories(ByVal y As IAssemblyScanner)
    y.Assembly("DebtRemedy.Core")
    y.Assembly("DebtRemedy.Data")
    y.WithDefaultConventions()
    y.ConnectImplementationsToTypesClosing(GetType(Of ));
End Sub
Run Code Online (Sandbox Code Playgroud)