gpt4 book ai didi

asp.net-mvc - 在 SharpArchitecture 的 VB.Net 转换中实现 StructureMap 的问题

转载 作者:行者123 更新时间:2023-12-04 08:50:34 27 4
gpt4 key购买 nike

我在 VB.Net 环境中工作,最近的任务是创建一个 MVC 环境以用作工作的基础。我决定将最新的 SharpArchitecture 版本(2009 年第 3 季度)转换为 VB,经过一番拉扯之后,总体上它已经很好了。我遇到了 CaSTLe Windsor 的一个问题,在我的测试 Controller 的构造函数中引用的自定义存储库接口(interface)(位于核心/域项目中)没有被注入(inject)具体实现(来自数据项目)。我为此碰了壁,所以基本上决定将 CaSTLe Windsor 换成 StructureMap。

我想我已经实现了这一点,因为一切都可以编译和运行,并且我的 Controller 在引用自定义存储库接口(interface)时运行正常。现在看来,我已经/或现在无法正确设置我的通用接口(interface)(我希望这是有道理的,因为我对这一切都不熟悉)。当我在 Controller 构造函数中使用 IRepository(Of T) (希望它被注入(inject) 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,版本=1.0.0.0,文化=中性,PublicKeyToken=b5f559ae0ac4e006"

这是我正在使用的代码摘录(我的项目称为 DebtRemedy)。

我的结构图注册表类

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

我已经尝试过 ForRequestedType(我认为现在已弃用)和 For.
IRepository(Of T) 位于 SharpArch.Core.PersistenceSupport 中。
Repository(Of T) 位于 SharpArch.Data.NHibernate 中。

我的服务定位器类
    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

我的 Controller 工厂类
    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

我的 global.asax 中的初始化内容
Dim container As IContainer = New Container(New DefaultRegistry)
ControllerBuilder.Current.SetControllerFactory(New ServiceLocatorControllerFactory())

ServiceLocator.SetLocatorProvider(Function() New StructureMapServiceLocator(container))

我的测试 Controller
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

当我取出与 IRepository(Of T) 的 pageRepository 相关的所有内容时,上述工作正常。

对此的任何帮助将不胜感激。

最佳答案

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

我不得不声明y.ConnectImplementationsToTypesClosing(GetType(IRepository(Of )))在我的 Scan 委托(delegate)中,使 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

关于asp.net-mvc - 在 SharpArchitecture 的 VB.Net 转换中实现 StructureMap 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2344914/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com