gpt4 book ai didi

asp.net-mvc - 使用 CaSTLe Windsor 在 ASP.NET MVC 中设置控制反转 (IoC)

转载 作者:行者123 更新时间:2023-12-04 07:28:36 24 4
gpt4 key购买 nike

我正在阅读 Sanderson 的 Pro ASP.NET MVC 框架,并在第 4 章中讨论了 Creating a Custom Controller Factory看来原来的方法,AddComponentLifeStyleAddComponentWithLifeStyle ,用于注册 Controller 现在已弃用:

public class WindsorControllerFactory : DefaultControllerFactory
{
IWindsorContainer container;

public WindsorControllerFactory()
{
container = new WindsorContainer(new XmlInterpreter(new ConfigResource("castle")));

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

//[Obsolete("Use Register(Component.For<I>().ImplementedBy<T>().Named(key).Lifestyle.Is(lifestyle)) instead.")]
//IWindsorContainer AddComponentLifeStyle<I, T>(string key, LifestyleType lifestyle) where T : class;
foreach (Type t in controllerTypes)
{
container.Register(Component.For<IController>().ImplementedBy<???>().Named(t.FullName).LifeStyle.Is(LifestyleType.Transient));
}
}

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

新的建议是使用 Register(Component.For<I>().ImplementedBy<T>().Named(key).Lifestyle.Is(lifestyle)) ,但我不知道如何在 ImplementedBy<???>() 中显示实现 Controller 类型方法。我试过 ImplementedBy<t>()ImplementedBy<typeof(t)>() ,但我找不到合适的方式来传递实现类型。有任何想法吗?

最佳答案

我正在使用 ControllerBuilder.SetControllerFactory 执行此操作以及您可以在开源项目 MvcContrib 中找到的代码:

Global.asax.cs

protected void Application_Start()
{
...

IWindsorContainer windsorContainer = new WindsorContainer();
windsorContainer.RegisterControllers(Assembly.GetExecutingAssembly());
ControllerBuilder.Current.SetControllerFactory(new WindsorControllerFactory(windsorContainer));

...
}

WindsorControllerFactory
public class WindsorControllerFactory : DefaultControllerFactory
{
private readonly IWindsorContainer _container;

public WindsorControllerFactory(IWindsorContainer container)
{
if (container == null)
{
throw new ArgumentNullException();
}

_container = container;
}

protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
{
if (controllerType == null)
{
throw new HttpException();
}

if (!typeof(IController).IsAssignableFrom(controllerType))
{
throw new ArgumentException();
}

try
{
return (IController)_container.Resolve(controllerType);
}
catch (Exception ex)
{
throw new InvalidOperationException();
}
}

public override void ReleaseController(IController controller)
{
IDisposable disposable = controller as IDisposable;

if (disposable != null)
{
disposable.Dispose();
}

_container.Release(controller);
}
}

WindsorExtensions (见 MvcContrib)
public static class WindsorExtensions
{
public static IWindsorContainer RegisterController<T>(this IWindsorContainer container) where T : IController
{
container.RegisterControllers(typeof(T));

return container;
}

public static IWindsorContainer RegisterControllers(this IWindsorContainer container, params Type[] controllerTypes)
{
foreach (Type type in controllerTypes)
{
if (ControllerExtensions.IsController(type))
{
container.Register(Component.For(type).Named(type.FullName).LifeStyle.Is(LifestyleType.Transient));
}
}

return container;
}

public static IWindsorContainer RegisterControllers(this IWindsorContainer container, params Assembly[] assemblies)
{
foreach (Assembly assembly in assemblies)
{
container.RegisterControllers(assembly.GetExportedTypes());
}

return container;
}
}

Controller 扩展 (见 MvcContrib)
public static class ControllerExtensions
{
public static bool IsController(Type type)
{
return type != null
&& type.Name.EndsWith("Controller", StringComparison.OrdinalIgnoreCase)
&& !type.IsAbstract
&& typeof(IController).IsAssignableFrom(type);
}
}

关于asp.net-mvc - 使用 CaSTLe Windsor 在 ASP.NET MVC 中设置控制反转 (IoC),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5124393/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com