C# 如何指定要在特定mvc控制器上使用的接口注册?
我有两个接口的实现,在普通的旧c#中,它将被实例化如下:C# 如何指定要在特定mvc控制器上使用的接口注册?,c#,asp.net-mvc,dependency-injection,inversion-of-control,unity-container,C#,Asp.net Mvc,Dependency Injection,Inversion Of Control,Unity Container,我有两个接口的实现,在普通的旧c#中,它将被实例化如下: var useCache = bool.Parse(ConfigurationManager.AppSettings["useCache"]); var oven = useCache ? new CachedCookieOven(new CookieOven()) : new CookieOven(); var controller = new CookieController(oven); // MVC Controller
var useCache = bool.Parse(ConfigurationManager.AppSettings["useCache"]);
var oven = useCache
? new CachedCookieOven(new CookieOven())
: new CookieOven();
var controller = new CookieController(oven); // MVC Controller
以下是接口和类:
public interface ICookieOven {
IEnumerable<Cookie> Bake();
}
public class CookieOven : ICookieOven {
public IEnumerable<Cookie> Bake() {
var list = new List<Cookie>();
// bake cookies and return them
return list;
}
}
public class CachedCookieOven : ICookieOven {
readonly ICookieOven _oven;
public CachedCookieOven(ICookieOven oven) { _oven = oven; }
public IEnumerable<Cookie> Bake() {
var cookies = GetFromPlate();
return cookies ?? _oven.Bake();
}
}
创建的Bootstrapper
类在注释中说,我不需要注册我的mvc控制器类
public static class Bootstrapper
{
public static IUnityContainer Initialise()
{
var container = BuildUnityContainer();
DependencyResolver.SetResolver(new UnityDependencyResolver(container));
return container;
}
private static IUnityContainer BuildUnityContainer()
{
var container = new UnityContainer();
// register all your components with the container here
// it is NOT necessary to register your controllers
// e.g. container.RegisterType<ITestService, TestService>();
RegisterTypes(container);
return container;
}
public static void RegisterTypes(IUnityContainer container)
{
}
}
公共静态类引导程序
{
公共静态IUnityContainer初始化()
{
var container=BuildUnityContainer();
SetResolver(新UnitedDependencyResolver(容器));
返回容器;
}
私有静态IUnityContainer BuildUnityContainer()
{
var container=new UnityContainer();
//在此处向容器注册所有组件
//无需注册控制器
//例如container.RegisterType();
注册类型(容器);
返回容器;
}
公共静态无效注册表类型(IUnityContainer容器)
{
}
}
在Unity中,我记录了这两种情况。也许有更好的办法,如果有,请告诉我
public static class Bootstrapper {
// ...
private static IUnityContainer BuildUnityContainer() {
var container = new UnityContainer();
var useCache = bool.Parse(ConfigurationManager.AppSettings["useCache"]);
// register
container.RegisterType<ICookieOven, CookieOven>("oven");
if (useCache) {
container.RegisterType<ICookieOven, CachedCookieOven>("cachedOven",
new InjectionConstructor(container.Resolve<ICookieOven>("oven"));
}
}
}
公共静态类引导程序{
// ...
私有静态IUnityContainer BuildUnityContainer(){
var container=new UnityContainer();
var useCache=bool.Parse(ConfigurationManager.AppSettings[“useCache”]);
//登记册
容器。登记类型(“烤箱”);
如果(使用缓存){
container.RegisterType(“cachedOven”,
新的注入构造函数(container.Resolve(“烘箱”);
}
}
}
如何确保将
ICookieOven
的正确实例发送给CookieController
mvc控制器的构造函数?您想创建一个对象,但哪个对象取决于运行时已知的值。您需要的是一个工厂(两个示例)
要实现这一点,一种方法可能是这样的:控制器可以依赖于注入控制器构造函数中的IOvenFactory
。当需要烤箱时,可以调用\u ovenFactory.Create()
在
IOvenFactory
实现中,您可以拥有如何创建的逻辑,具体取决于配置值。在Unity中注册类型而不使用名称会使其成为默认类型。如果您要注册多个类型,则必须提供名称。以下是在B中注册我的类型的正确方法ootstrapper
类:
public static void RegisterTypes(IUnityContainer container)
{
var useCache = bool.Parse(ConfigurationManager.AppSettings["useCache"]);
if (useCache) {
// named, this is not the default
container.RegisterType<ICookieOven,CookieOven>("oven");
// this one is not named and is the default
container.RegisterType<ICookieOven,CachedCookieOven>(new InjectionConstructor(
container.Resolve<ICookieOven>("oven"));
} else {
// notice it is not named, it is the default
container.RegisterType<ICookieOven,CookieOven>();
}
}
公共静态无效注册表类型(IUnityContainer容器)
{
var useCache=bool.Parse(ConfigurationManager.AppSettings[“useCache”]);
if(useCache){
//命名,这不是默认值
容器。登记类型(“烤箱”);
//此项未命名,是默认项
container.RegisterType(新注入构造函数)(
容器。解决(“烤箱”);
}否则{
//请注意,它没有命名,它是默认值
container.RegisterType();
}
}
web.config值在运行时不可更改。因此,useCache值是一个已知的启动值,不需要工厂。
public static void RegisterTypes(IUnityContainer container)
{
var useCache = bool.Parse(ConfigurationManager.AppSettings["useCache"]);
if (useCache) {
// named, this is not the default
container.RegisterType<ICookieOven,CookieOven>("oven");
// this one is not named and is the default
container.RegisterType<ICookieOven,CachedCookieOven>(new InjectionConstructor(
container.Resolve<ICookieOven>("oven"));
} else {
// notice it is not named, it is the default
container.RegisterType<ICookieOven,CookieOven>();
}
}