如何在 VB.NET 或 C# 中将此 WindsorControllerFactory 转换为 UnityControllerFactory

发布于 2024-08-21 09:21:21 字数 4745 浏览 8 评论 0原文

我一直在阅读 Steven Sanderson 所著的《Pro ASP.NET MVC Framework》一书。我正在尝试重写一个应用程序以使用 Unity IoC 而不是 Castle Windsor IoC。我希望 Unity 实现能够像 Windsor 那样注册所有控制器。

代码:

public class WindsorControllerFactory : DefaultControllerFactory

这是

WindorControllerFactory

// The constructor
// 1. Sets up a new IoC container
// 2. Registers all components specified in web.config
// 3. Registers all controller types as components
public WindsorControllerFactory()
{ 
    // Instantiate a container, taking configuration from web.config
    container = new WindsorContainer(
                    new XmlInterpreter(new ConfigResource("castle"))
                );

    // Also register all the controller types as transient
    var controllerTypes = from t in Assembly.GetExecutingAssembly().GetTypes()
                          where typeof(IController).IsAssignableFrom(t)
                          select t;

    foreach (Type t in controllerTypes)
        container.AddComponentWithLifestyle(t.FullName, t, Castle.Core.LifestyleType.Transient);
}

// Constructs the controller instance needed to service each request
protected override IController GetControllerInstance(Type controllerType)
{
    return (IController)container.Resolve(controllerType);
}

是我在 Unity 实现方面的微弱尝试:

Public Class UnityControllerFactory
Inherits DefaultControllerFactory


Private _container As New UnityContainer()

Public Property Container() As UnityContainer
    Get
        Return _container
    End Get
    Set(ByVal value As UnityContainer)
        _container = value
    End Set
End Property

Public Sub New()
    Dim section As UnityConfigurationSection = DirectCast(ConfigurationManager.GetSection("unity"), UnityConfigurationSection)
    section.Containers("SupportSiteContainer").Configure(_container)
    '_container.RegisterType(GetType(IRepository(Of )), "IRepository", New TransientLifetimeManager(), Nothing)

    Dim controllerTypes = From t In Assembly.GetExecutingAssembly().GetTypes() _
                         Where GetType(IController).IsAssignableFrom(t) _
                         Select t

    For Each t As Type In controllerTypes
        _container.RegisterType(t, t.Name, New TransientLifetimeManager())

        '    ' _container.AddComponentWithLifestyle(t.FullName, t, LifestyleType.Transient)
    Next
End Sub


Public Overloads Function GetControllerInstance(ByVal controllerType As Type) As IController
    If controllerType Is Nothing Then
        Return Nothing
    End If

    Return DirectCast(_container.Resolve(controllerType), IController)
End Function

End Class

这是我的 Unity 配置:

<unity>
    <typeAliases>
        <!-- Lifetime manager types -->
        <typeAlias alias="singleton" type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager, Microsoft.Practices.Unity"/>
        <typeAlias alias="external" type="Microsoft.Practices.Unity.ExternallyControlledLifetimeManager, Microsoft.Practices.Unity"/>
        <typeAlias alias="transient" type="Microsoft.Practices.Unity.TransientLifetimeManager, Microsoft.Practices.Unity"/>
        <typeAlias alias="perThread" type="Microsoft.Practices.Unity.PerThreadLifetimeManager, Microsoft.Practices.Unity" />
        <!-- Custom object types -->
        <typeAlias alias="IRepository" type="SupportSite.Repository.IRepository`1, SupportSite.Repository"/>
        <typeAlias alias="EFRepository" type="SupportSite.Data.EFRepository`1, SupportSite.Data"/>
        <typeAlias alias="IUnitOfWork" type="SupportSite.Repository.IUnitOfWork, SupportSite.Repository"/>
        <typeAlias alias="EFUnitOfWork" type="SupportSite.Data.EFUnitOfWork, SupportSite.Data"/>
        <typeAlias alias="IUnitOfWorkFactory" type="SupportSite.Repository.IUnitOfWorkFactory, SupportSite.Repository"/>
        <typeAlias alias="EFUnitOfWorkFactory" type="SupportSite.Data.EFUnitOfWorkFactory, SupportSite.Data"/>
    </typeAliases>

    <containers>
      <container name="SupportSiteContainer">
        <types>
            <type type="IRepository" mapTo="EFRepository">
                <lifetime type="transient" />
            </type>

            <type type="IUnitOfWork" mapTo="EFUnitOfWork">
                <lifetime type="transient" />
            </type>

            <type type="IUnitOfWorkFactory" mapTo="EFUnitOfWorkFactory">
                <lifetime type="transient" />
            </type>
        </types>
      </container>
    </containers>

谢谢, 马特

I have been following along in the book Pro ASP.NET MVC Framework by Steven Sanderson. I am trying to rewrite an application to use Unity IoC instead of Castle Windsor IoC. I want the Unity implementation to register all of the controllers like the Windsor one does.

Here is the WindorControllerFactory code:

public class WindsorControllerFactory : DefaultControllerFactory

{

WindsorContainer container;

// The constructor
// 1. Sets up a new IoC container
// 2. Registers all components specified in web.config
// 3. Registers all controller types as components
public WindsorControllerFactory()
{ 
    // Instantiate a container, taking configuration from web.config
    container = new WindsorContainer(
                    new XmlInterpreter(new ConfigResource("castle"))
                );

    // Also register all the controller types as transient
    var controllerTypes = from t in Assembly.GetExecutingAssembly().GetTypes()
                          where typeof(IController).IsAssignableFrom(t)
                          select t;

    foreach (Type t in controllerTypes)
        container.AddComponentWithLifestyle(t.FullName, t, Castle.Core.LifestyleType.Transient);
}

// Constructs the controller instance needed to service each request
protected override IController GetControllerInstance(Type controllerType)
{
    return (IController)container.Resolve(controllerType);
}

}

Here is my feeble attempt at the Unity implementation:

Public Class UnityControllerFactory
Inherits DefaultControllerFactory


Private _container As New UnityContainer()

Public Property Container() As UnityContainer
    Get
        Return _container
    End Get
    Set(ByVal value As UnityContainer)
        _container = value
    End Set
End Property

Public Sub New()
    Dim section As UnityConfigurationSection = DirectCast(ConfigurationManager.GetSection("unity"), UnityConfigurationSection)
    section.Containers("SupportSiteContainer").Configure(_container)
    '_container.RegisterType(GetType(IRepository(Of )), "IRepository", New TransientLifetimeManager(), Nothing)

    Dim controllerTypes = From t In Assembly.GetExecutingAssembly().GetTypes() _
                         Where GetType(IController).IsAssignableFrom(t) _
                         Select t

    For Each t As Type In controllerTypes
        _container.RegisterType(t, t.Name, New TransientLifetimeManager())

        '    ' _container.AddComponentWithLifestyle(t.FullName, t, LifestyleType.Transient)
    Next
End Sub


Public Overloads Function GetControllerInstance(ByVal controllerType As Type) As IController
    If controllerType Is Nothing Then
        Return Nothing
    End If

    Return DirectCast(_container.Resolve(controllerType), IController)
End Function

End Class

Here is my Unity Config:

<unity>
    <typeAliases>
        <!-- Lifetime manager types -->
        <typeAlias alias="singleton" type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager, Microsoft.Practices.Unity"/>
        <typeAlias alias="external" type="Microsoft.Practices.Unity.ExternallyControlledLifetimeManager, Microsoft.Practices.Unity"/>
        <typeAlias alias="transient" type="Microsoft.Practices.Unity.TransientLifetimeManager, Microsoft.Practices.Unity"/>
        <typeAlias alias="perThread" type="Microsoft.Practices.Unity.PerThreadLifetimeManager, Microsoft.Practices.Unity" />
        <!-- Custom object types -->
        <typeAlias alias="IRepository" type="SupportSite.Repository.IRepository`1, SupportSite.Repository"/>
        <typeAlias alias="EFRepository" type="SupportSite.Data.EFRepository`1, SupportSite.Data"/>
        <typeAlias alias="IUnitOfWork" type="SupportSite.Repository.IUnitOfWork, SupportSite.Repository"/>
        <typeAlias alias="EFUnitOfWork" type="SupportSite.Data.EFUnitOfWork, SupportSite.Data"/>
        <typeAlias alias="IUnitOfWorkFactory" type="SupportSite.Repository.IUnitOfWorkFactory, SupportSite.Repository"/>
        <typeAlias alias="EFUnitOfWorkFactory" type="SupportSite.Data.EFUnitOfWorkFactory, SupportSite.Data"/>
    </typeAliases>

    <containers>
      <container name="SupportSiteContainer">
        <types>
            <type type="IRepository" mapTo="EFRepository">
                <lifetime type="transient" />
            </type>

            <type type="IUnitOfWork" mapTo="EFUnitOfWork">
                <lifetime type="transient" />
            </type>

            <type type="IUnitOfWorkFactory" mapTo="EFUnitOfWorkFactory">
                <lifetime type="transient" />
            </type>
        </types>
      </container>
    </containers>

Thanks,
Matt

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文