MVC中使用Castle.Windsor
我在MVC中使用Castle.Windsor是这样用的。
首先在UI层安装Install Castle.Windsor
在App_Start中增加一个类WindsorActivator,用于注册和销毁Containter。注意,这里是在PreApplicationStartMethod中注册的,是在ApplicationShutdownMethod中销毁的。
using Castle.Windsor;
using Castle.Windsor.Installer;
using System;
using WebActivatorEx; [assembly: PreApplicationStartMethod(typeof(TaskManagement.UI.App_Start.WindsorActivator), "PreStart")]
[assembly: ApplicationShutdownMethodAttribute(typeof(TaskManagement.UI.App_Start.WindsorActivator), "Shutdown")] namespace TaskManagement.UI.App_Start
{
public static class WindsorActivator
{
public static IWindsorContainer Container; public static void PreStart()
{
//将这个Assembly中所有实现IWindsorInstaller接口的类都注册
Container = new WindsorContainer().Install(FromAssembly.This());
} public static void Shutdown()
{
if (Container != null)
Container.Dispose();
}
}
}
新建一个Installers文件夹,在该文件夹中分别添加多个Installer文件,用于注册DA、Service、Infrastructure层的内容,举例ServiceInstaller.cs文件:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Castle.MicroKernel.Registration;
using Castle.MicroKernel.SubSystems.Configuration;
using Castle.Windsor;
using TaskManagement.Service.Implementation; namespace TaskManagement.UI.Installers
{
public class ServiceInstaller : IWindsorInstaller
{
public void Install(IWindsorContainer container, IConfigurationStore store)
{
//container.Register(Classes.FromThisAssembly()
// .IncludeNonPublicTypes()
// .BasedOn<ITransient>()
// .WithService.DefaultInterfaces()
// .LifestyleTransient()); container.Register(Classes.FromAssemblyNamed("TaskManagement.Service")
//.IncludeNonPublicTypes()
.BasedOn<BaseService>()
.WithService
.DefaultInterfaces() //使用默认的I+ServiceName的方式来取Service
.LifestylePerWebRequest());
//.LifestyleTransient());
}
}
}
其中ControllerInstaller比较特殊:
using System.Web.Mvc;
using Castle.MicroKernel.Registration;
using Castle.MicroKernel.SubSystems.Configuration;
using Castle.Windsor; namespace TaskManagement.UI.Installers
{
using Plumbing; public class ControllersInstaller : IWindsorInstaller
{
public void Install(IWindsorContainer container, IConfigurationStore store)
{
//container.Register(
// Classes.
// FromThisAssembly().
// BasedOn<IController>().
// If(c => c.Name.EndsWith("Controller")).
// LifestyleTransient()); //ControllerBuilder.Current.SetControllerFactory(new WindsorControllerFactory(container)); container.Register(Classes.FromThisAssembly().
BasedOn<IController>().
If(c => c.Name.EndsWith("Controller"))
.LifestyleTransient()); container.Register(Classes.FromThisAssembly()
.BasedOn<Controller>()
.LifestyleTransient()
);
//设置指定的Controller的工厂,以替代系统默认的工厂
ControllerBuilder.Current.SetControllerFactory(new WindsorControllerFactory(container));
}
}
}
需要额外的一个工厂类来取代默认的DefaultControllerFactory:
using System;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Castle.Windsor; namespace TaskManagement.UI.Plumbing
{
public class WindsorControllerFactory : DefaultControllerFactory
{
readonly IWindsorContainer container; public WindsorControllerFactory(IWindsorContainer container)
{
this.container = container;
} protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
{
if (controllerType != null && container.Kernel.HasComponent(controllerType))
return (IController)container.Resolve(controllerType); return base.GetControllerInstance(requestContext, controllerType);
} public override void ReleaseController(IController controller)
{
container.Release(controller);
}
}
}
DefaultControllerFactory
使用:
1、属性注册
public class RoleS : BaseService, IRoleS
{
public IRoleR _IRoleR { get; set; }
public IViewR _IViewR { get; set; }
public IViewActionR _IViewActionR { get; set; }
直接使用即可,注意接口要申明为Public的。
2、构造函数注册。可能在测试、外部调用、Windows服务等情况下用到。
public class ChangeLogS : BaseService, IChangeLogS
{
public IChangeLogR _IChangeLogR { get; set; }
public ChangeLogS(IChangeLogR iChangeLogR)
{
_IChangeLogR = iChangeLogR;
}
3、UI层的Help 类中使用
var _IDepartmentR = WindsorActivator.Container.Kernel.Resolve<IDepartmentR>();
MVC中使用Castle.Windsor的更多相关文章
- 在ASP.NET MVC中使用Castle Windsor
平常用Inject比较多,今天接触到了Castle Windsor.本篇就来体验其在ASP.NET MVC中的应用过程. Visual Studio 2012创建一个ASP.NET MVC 4网站. ...
- Castle Windsor 使MVC Controller能够使用依赖注入
以在MVC中使用Castle Windsor为例 1.第一步要想使我们的Controller能够使用依赖注入容器,先定义个WindsorControllerFactory类, using System ...
- ASP.NET MVC Castle Windsor 教程
一.[转]ASP.NET MVC中使用Castle Windsor 二.[转]Castle Windsor之组件注册 平常用Inject比较多,今天接触到了Castle Windsor.本篇就来体验其 ...
- 依赖注入容器之Castle Windsor
一.Windsor的使用 Windsor的作为依赖注入的容器的一种,使用起来比较方便,我们直接在Nuget中添加Castle Windsor,将会自动引入Castle.Core 和 Castle.Wi ...
- 说说ABP项目中的AutoMapper,Castle Windsor(痛并快乐着)
这篇博客要说的东西跟ABP,AutoMapper和Castle Windsor都有关系,而且也是我在项目中遇到的问题,最终解决了,现在的感受就是“痛并快乐着”. 首先,这篇博客不是讲什么新的知识点,而 ...
- Castle Windsor 项目中快速使用
Castle Windsor 项目中快速使用 新建项目如下: 一个模型类,一个接口,一个实现方法.我的目的很明确就是在UI层通过Castle 调用数据访问层的方法. 添加项目引用 CastleDemo ...
- Castle Windsor常用介绍以及其在ABP项目的应用介绍
最近在研究ABP项目,有关ABP的介绍请看阳光铭睿 博客,ABP的DI和AOP框架用的是Castle Windsor下面就对Castle Windsor项目常用方法介绍和关于ABP的使用总结 1.下载 ...
- 在ABP项目的应用Castle Windsor
Castle Windsor常用介绍以及其在ABP项目的应用介绍 最近在研究ABP项目,有关ABP的介绍请看阳光铭睿 博客,ABP的DI和AOP框架用的是Castle Windsor下面就对Castl ...
- IOC 容器在 ASP.NET MVC 中的应用
IOC 容器在 ASP.NET MVC 中的应用 IOC:Inversion Of Control 翻译为控制反转,我们在面向对象软件开发过程中,一个应用程序它的底层结构可能由N种不同的构件来相互协作 ...
随机推荐
- Media Queries简单案例一
案例一: 1 <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" ...
- L-BFGS
L-BFGS算法比较适合在大规模的数值计算中,具备牛顿法收敛速度快的特点,但不需要牛顿法那样存储Hesse矩阵,因此节省了大量的空间以及计算资源.本文主要通过对于无约束最优化问题的一些常用算法总结,一 ...
- paxos ---学习笔记
摘自维基百科:分布式系统中的节点通信存在两种模型:共享内存(Shared memory)和消息传递(Messages passing).基于消息传递通信模型的分布式系统,不可避免的会发生以下错误:进程 ...
- SQL Server 紧急状态下的数据库恢复
背景:由于服务器硬盘损坏,服务器异常关机.重新进入后,数据库为质疑状态.(数据库名字上面有个感叹号,连接不了) 经过无数次的百度以及大佬们的指点下,终于成功恢复,下面来说一下方法. 第一种: 1.在服 ...
- Python常用第三方模块(长期更新)
1.keyboard #监控键盘 2.PIL#处理图片 3.operator #操作列表 4.shelve #数据存储方案 保存dat文件 5.optparse #处理命令行参数 6.configpa ...
- 国际化SEO优化的最佳实践
作者:Kristopher Jones 翻译 :吴祺深 欢迎访问网易云社区,了解更多网易技术产品运营经验. 让我们来说一下hreflang属性.如果你还没有关掉这个页面,那么你已经完成了这个教程最重要 ...
- akka开发(一)HelloWorld
package com.hfi.helloakka; import akka.actor.ActorRef; import akka.actor.Props; import akka.actor.Un ...
- Mac OS 10.12 - 安装Homebrew,像Ubuntu里面的apt一样简单地安装和删除软件!
Homebrew — macOS 不可或缺的套件管理器,Homebrew官方网站如此介绍自己!!! 中文官网:https://brew.sh/index_zh-cn.html 一,安装 打开shell ...
- about BFC
https://www.cnblogs.com/lhb25/p/inside-block-formatting-ontext.html Box.Formatting Context(BFC)
- 对Spring 及SpringMVC的理解
spring是一个轻型容器(light-weight Container),其核心是Bean工厂(Bean Factory),用以构造我们所需要的M(Model).在此基础之上,Spring提供了AO ...