MVC3& Unity.Mvc

时间:2011-05-20 19:06:35

标签: asp.net-mvc dependency-injection

奇怪的问题我正在使用我的MVC3项目。我使用Unity.Mvc跟踪了一个简单的IOC示例。

如果我的对象及其界面在Web项目本身(在本例中为IMessageService和MessageService),它可以正常工作。他们显然在工作,没有问题。

但是当我尝试从外部程序集注册我的业务服务对象时,它们永远不会被设置为任何东西(总是为null)。 App-Start在注册等时没有错误

有人有什么想法吗?绝望在这里......

更新

#Region "Imports"

Imports MyProject.Services
Imports MyProject.Services.Interfaces
Imports MyProject.Web.Mvc.Bootstrap
Imports MyProject.Web.Mvc.Services
Imports Microsoft.Practices.Unity
Imports Unity.Mvc3

#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(UnityDI), "Start")> 

#End Region

Namespace MyProject.Web.Mvc.Bootstrap

    ''' <summary>
    ''' Class to setup dependency injection and register types/services.
    ''' </summary>
    ''' <remarks></remarks>
    Public NotInheritable Class UnityDI

        ''' <summary>
        ''' Method to register the Unity 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 Start()

            ' Set DI resolver
            ' NOTE: ECD - The UnityDependencyResolver below is part of the Unity.Mvc3 assembly
            DependencyResolver.SetResolver(New UnityDependencyResolver(RegisterIocServices()))

        End Sub

        ''' <summary>
        ''' Registers the IOC types/services.   
        ''' </summary>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Private Shared Function RegisterIocServices() As IUnityContainer

            ' Create Unity dependency container
            Dim dependencyContainer As IUnityContainer = New UnityContainer

            ' Register the relevant types/services for the container here through classes or configuration
            With dependencyContainer
                .RegisterType(Of IFormsAuthenticationService, FormsAuthenticationService)()
                .RegisterType(Of IContactService, ContactService)()
                .RegisterType(Of IProductItemService, ProductItemService)()
                .RegisterType(Of ICustomerProductItemService, CustomerProductItemService)()
                .RegisterType(Of ISystemTableItemService, SystemTableItemService)()
                .RegisterType(Of ICatalogCodeService, CatalogCodeService)()
                .RegisterType(Of IOrderService, OrderService)()

                ' TEST: This one is in the MVC project and works, the above are an external library
                .RegisterType(Of IMessageService, MessageService)()
            End With

            Return dependencyContainer

        End Function

    End Class

End Namespace

以上代码是我的注册流程。我也尝试将它直接放在global.asax中并具有相同的行为。

更新2

弄清楚我的问题。

在我的控制器中,我有以下属性:

<Dependency()>
Private Property CatalogCodeService As ICatalogCodeService
<Dependency()>
Private Property ContactService As IContactService
<Dependency()>
Private Property CustomerProductItemService As ICustomerProductItemService

但是,在操作方法中访问服务时总是会抛出一个错误,即没有任何这些对象的实例。所以我认为这是我发布的原始代码或者我注册的方式。

原来这不是我的注册码,这完全没问题。

问题?

属性必须是“公共”!!私人,受保护,朋友都不适合国际奥委会。一旦我将这些属性更改为“公共”,一切都开始正常工作。

我还没有证实这在C#中是一样的,所以如果有人可以加两分钱,请务必这样做。

去图......

1 个答案:

答案 0 :(得分:1)

更新II:

上面的实现使用了公共属性,它遵循依赖注入的“Property Injection”模式。

我已经切换到更合适的“构造函数注入”方法。

通过此更改,我的控制器使用的服务的属性可以是私有且只读。

“.NET中的依赖注入”作者:Mark Seemann,这本伟大的书。我强烈建议任何实现依赖注入的人或那些只想了解它的人。