简体   繁体   English

如何从ASP.NET MVC 5应用程序中的“区域”在IoC容器中注册类型?

[英]How to register type in the IoC container from an “Area” in ASP.NET MVC 5 app?

I am trying to setup dependency injection in my ASP.NET MVC 5 app using Unity.Mvc. 我正在尝试使用Unity.Mvc在我的ASP.NET MVC 5应用程序中设置依赖项注入 I followed this tutorial which explains that the class UnityConfig has a method called RegisterTypes which allows me to register my type into the container. 我按照本教程的说明进行了解释, 该教程说明UnityConfig类具有一个称为RegisterTypes的方法,该方法使我可以将我的类型注册到容器中。

However, I am using Area in my project. 但是,我在项目中使用Area。 some of my Areas need to be able to also register types into the container. 我的某些区域还需要能够将类型注册到容器中。 These types in my area will only be used by my area but still need to be registered. 我所在地区的这些类型只会在我所在地区使用,但仍需要注册。

How can I register-types into my IoC container directly from my area? 如何直接从我的区域将类型注册到IoC容器中?

You can create a separate class that inherits from UnityContainerExtension and place it in your area, like this: 您可以创建一个继承自UnityContainerExtension的单独的类,并将其放置在您的区域中,如下所示:

public class MyAreaContainerExtension : UnityContainerExtension
{
    protected override void Initialize()
    {
        Container.RegisterType<IDoesSomething, DoesSomething>();
    }
}

Then in your startup, 然后在您的创业公司,

container.AddNewExtension<MyAreaContainerExtension>();

Or if your extension has constructor parameters you pass to it, like configuration data it needs or instances it should use, you can create an instance of the extension and then do 或者,如果您的扩展具有传递给它的构造函数参数(例如其需要的配置数据或应使用的实例),则可以创建扩展的实例,然后执行

container.AddExtension(theExtensionInstanceICreated);

It's a nice practice because it allows you to keep component registration closer to the classes being registered instead of having one gigantic file with dozens or even a hundred or more registrations. 这是一个很好的做法,因为它使您可以使组件注册更接近要注册的类,而不是拥有一个包含数十个甚至一百个甚至更多注册的巨大文件。

Another benefit of this approach is that as component registrations become complicated you can write unit tests for these extensions that add the extension to a container and then resolve objects from the container. 这种方法的另一个好处是,随着组件注册变得越来越复杂,您可以为这些扩展编写单元测试,以将扩展添加到容器中,然后从容器中解析对象。

You could have other components which must be registered in order for the types registered in this extension to work, but you don't want to duplicate the registrations in your extension. 您可能必须注册其他组件才能使用此扩展程序中注册的类型,但是您不想在扩展程序中重复这些注册。 In that case you could make it explicit by specifying what types must already be registered, something like this: 在这种情况下,您可以通过指定必须已经注册的类型来使其明确,如下所示:

public class MyAreaContainerExtension : UnityContainerExtension
{
    protected override void Initialize()
    {
        AssertRequiredRegistrations();
        Container.RegisterType<IDoesSomething, DoesSomething>();
    }

    private void AssertRequiredRegistrations()
    {
        AssertRequiredRegistration(typeof(ISomeOtherType));
    }

    private void AssertRequiredRegistration(Type type)
    {
        if(!Container.IsRegistered(type)) 
            throw new Exception($"Type {type.FullName} must be registered with the container.");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM