使用无参数控制器使用MVC4进行依赖注入

时间:2021-04-03 15:48:25

I am going through the following tutorial. I'm at the part

我正在阅读以下教程。我是那个角色

IUnityContainer BuildUnityContainer()
{
    var container = new UnityContainer();

    container.RegisterType<IStoreService, StoreService>();
    container.RegisterType<IController, StoreController>("Store");        

    return container;
}

The question I have is how do I do multiple controllers? For instance if I have Store, Music, and Owner controllers, each with their own I[Name]Service

我的问题是如何做多个控制器?例如,如果我有Store,Music和Owner控制器,每个控制器都有自己的I [Name]服务

4 个解决方案

#1


2  

The new Unity 3 provides an integration point for MVC 4 out of the box. All the details on how to use it are here: http://msdn.microsoft.com/en-us/library/dn178463(v=pandp.30).aspx#sec17

新的Unity 3提供了开箱即用的MVC 4集成点。有关如何使用它的所有详细信息,请访问:http://msdn.microsoft.com/en-us/library/dn178463(v = pandp.30).aspx#sec17

You can also combine this with the registration by convention feature to register all your mappings like IFoo -> Foo: http://msdn.microsoft.com/en-us/library/dn178463(v=pandp.30).aspx#sec23

您还可以将其与按惯例注册功能相结合,以注册所有映射,如IFoo - > Foo:http://msdn.microsoft.com/en-us/library/dn178463(v = pandp.30)。aspx#sec23

Regarding controllers, you do not need to register them explicitly in Unity, because if you ask Unity to resolve an instance of a concrete type that was not previously registered, it will build one up (injecting the dependencies using the longest constructor). MVC will ask Unity using the concrete types of the controllers, so they will work automatically.

关于控制器,您不需要在Unity中明确注册它们,因为如果您要求Unity解析之前未注册的具体类型的实例,它将构建一个实例(使用最长的构造函数注入依赖项)。 MVC将使用具体类型的控制器来询问Unity,因此它们将自动运行。

#2


1  

you have to register each of those in the same way. Or you might have to write a logic where you can get all possible types using reflaction and register those within loop.

你必须以同样的方式注册每一个。或者您可能必须编写一个逻辑,您可以使用reflaction获取所有可能的类型并在循环内注册它们。

#3


0  

Take a look at http://autoregistration.codeplex.com/. It does autoregistration of types for unity. Most other IOC containers does this as well.

看看http://autoregistration.codeplex.com/。它为统一进行类型的自动注册。大多数其他IOC容器也是如此。

#4


0  

Take a look at http://weblogs.asp.net/shijuvarghese/archive/2008/10/24/asp-net-mvc-tip-dependency-injection-with-unity-application-block.aspx.

请查看http://weblogs.asp.net/shijuvarghese/archive/2008/10/24/asp-net-mvc-tip-dependency-injection-with-unity-application-block.aspx。

Step 3 contains code for injecting dependencies to controller Also include the following code from Step 4:

步骤3包含用于向控制器注入依赖项的代码还包括步骤4中的以下代码:

ControllerBuilder.Current.SetControllerFactory(new myFinance.Web.Controllers.UnityControllerFactory(container));

#1


2  

The new Unity 3 provides an integration point for MVC 4 out of the box. All the details on how to use it are here: http://msdn.microsoft.com/en-us/library/dn178463(v=pandp.30).aspx#sec17

新的Unity 3提供了开箱即用的MVC 4集成点。有关如何使用它的所有详细信息,请访问:http://msdn.microsoft.com/en-us/library/dn178463(v = pandp.30).aspx#sec17

You can also combine this with the registration by convention feature to register all your mappings like IFoo -> Foo: http://msdn.microsoft.com/en-us/library/dn178463(v=pandp.30).aspx#sec23

您还可以将其与按惯例注册功能相结合,以注册所有映射,如IFoo - > Foo:http://msdn.microsoft.com/en-us/library/dn178463(v = pandp.30)。aspx#sec23

Regarding controllers, you do not need to register them explicitly in Unity, because if you ask Unity to resolve an instance of a concrete type that was not previously registered, it will build one up (injecting the dependencies using the longest constructor). MVC will ask Unity using the concrete types of the controllers, so they will work automatically.

关于控制器,您不需要在Unity中明确注册它们,因为如果您要求Unity解析之前未注册的具体类型的实例,它将构建一个实例(使用最长的构造函数注入依赖项)。 MVC将使用具体类型的控制器来询问Unity,因此它们将自动运行。

#2


1  

you have to register each of those in the same way. Or you might have to write a logic where you can get all possible types using reflaction and register those within loop.

你必须以同样的方式注册每一个。或者您可能必须编写一个逻辑,您可以使用reflaction获取所有可能的类型并在循环内注册它们。

#3


0  

Take a look at http://autoregistration.codeplex.com/. It does autoregistration of types for unity. Most other IOC containers does this as well.

看看http://autoregistration.codeplex.com/。它为统一进行类型的自动注册。大多数其他IOC容器也是如此。

#4


0  

Take a look at http://weblogs.asp.net/shijuvarghese/archive/2008/10/24/asp-net-mvc-tip-dependency-injection-with-unity-application-block.aspx.

请查看http://weblogs.asp.net/shijuvarghese/archive/2008/10/24/asp-net-mvc-tip-dependency-injection-with-unity-application-block.aspx。

Step 3 contains code for injecting dependencies to controller Also include the following code from Step 4:

步骤3包含用于向控制器注入依赖项的代码还包括步骤4中的以下代码:

ControllerBuilder.Current.SetControllerFactory(new myFinance.Web.Controllers.UnityControllerFactory(container));