MVC Castle依赖注入实现代码

1、MVc 实现依赖注入

 public class WindsorControllerFactory : DefaultControllerFactory
{ private readonly IKernel _kernel; public WindsorControllerFactory(IKernel kernel)
{
_kernel = kernel; }
protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, System.Type controllerType)
{
if (controllerType == null)
{
throw new HttpException(, string.Format("当前对{0}的请求不存在", requestContext.HttpContext.Request.Path));
}
return (IController)_kernel.Resolve(controllerType); }
public override void ReleaseController(IController controller)
{
_kernel.ReleaseComponent(controller);
base.ReleaseController(controller); }
}
 public class ControllerInstaller : IWindsorInstaller
{ public void Install(Castle.Windsor.IWindsorContainer container, Castle.MicroKernel.SubSystems.Configuration.IConfigurationStore store)
{ container.Register(Classes.FromThisAssembly() //在哪里找寻接口或类 .BasedOn<IController>() //实现IController接口 .If(Component.IsInSameNamespaceAs<HomeController>()) //与HomeController在同一个命名空间 .If(t => t.Name.EndsWith("Controller")) //以"Controller"结尾 .Configure(c => c.LifestylePerWebRequest()));//每次请求创建一个Controller实例 container.Register(
Component.For<IUserDal>().ImplementedBy<UserDal>().LifestylePerWebRequest()); } }

Global.asax 文件进行注册

  public class MvcApplication : System.Web.HttpApplication
{ private IWindsorContainer _container;
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes); //初始化一个IOC容器
_container = new WindsorContainer().Install(FromAssembly.This()); //完成IWindsorInstaller接口中的注册
ControllerBuilder.Current.SetControllerFactory(new WindsorControllerFactory(_container.Kernel)); } protected void Application_End()
{
_container.Dispose(); } }
2、web api 依赖注入实现
一、创建 WindsorActivator 继承 IHttpControllerActivator
 
public class WindsorCompositionRoot : IHttpControllerActivator
{
private readonly IWindsorContainer _container; public WindsorCompositionRoot(IWindsorContainer container)
{
_container = container;
} public IHttpController Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)
{
var controller = (IHttpController)_container.Resolve(controllerType); request.RegisterForDispose(
new Release(() => _container.Release(controller))); return controller;
}
private sealed class Release : IDisposable
{
private readonly Action _release; public Release(Action release)
{
_release = release;
} public void Dispose()
{
_release();
}
}
}
 public class RepositoriesInstaller : IWindsorInstaller
{
public void Install(IWindsorContainer container, IConfigurationStore store)
{
container.Register(
Component.For<ITitemService>().ImplementedBy<TitemService>().LifestylePerWebRequest()); } } Global.asax 文件注册
 public class WebApiApplication : System.Web.HttpApplication
{
private static IWindsorContainer _container;
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
//api 初始化
ConfigureWindsor(GlobalConfiguration.Configuration);
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles); } public static void ConfigureWindsor(HttpConfiguration configuration)
{
_container = new WindsorContainer();
_container.Install(FromAssembly.This());
_container.Kernel.Resolver.AddSubResolver(new CollectionResolver(_container.Kernel, true));
             RegisterControllerActivator(_container);
}
}
      private static void RegisterControllerActivator(IWindsorContainer container)
{
GlobalConfiguration.Configuration.Services.Replace(typeof(IHttpControllerActivator),
new WindsorCompositionRoot(container));
}


    }

 




上一篇:iOS开发——网络编程OC篇&(一)XMPP简单介绍与准备


下一篇:spring得到实例和new一个实例,哪个快?