我在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的更多相关文章

  1. 在ASP.NET MVC中使用Castle Windsor

    平常用Inject比较多,今天接触到了Castle Windsor.本篇就来体验其在ASP.NET MVC中的应用过程. Visual Studio 2012创建一个ASP.NET MVC 4网站. ...

  2. Castle Windsor 使MVC Controller能够使用依赖注入

    以在MVC中使用Castle Windsor为例 1.第一步要想使我们的Controller能够使用依赖注入容器,先定义个WindsorControllerFactory类, using System ...

  3. ASP.NET MVC Castle Windsor 教程

    一.[转]ASP.NET MVC中使用Castle Windsor 二.[转]Castle Windsor之组件注册 平常用Inject比较多,今天接触到了Castle Windsor.本篇就来体验其 ...

  4. 依赖注入容器之Castle Windsor

    一.Windsor的使用 Windsor的作为依赖注入的容器的一种,使用起来比较方便,我们直接在Nuget中添加Castle Windsor,将会自动引入Castle.Core 和 Castle.Wi ...

  5. 说说ABP项目中的AutoMapper,Castle Windsor(痛并快乐着)

    这篇博客要说的东西跟ABP,AutoMapper和Castle Windsor都有关系,而且也是我在项目中遇到的问题,最终解决了,现在的感受就是“痛并快乐着”. 首先,这篇博客不是讲什么新的知识点,而 ...

  6. Castle Windsor 项目中快速使用

    Castle Windsor 项目中快速使用 新建项目如下: 一个模型类,一个接口,一个实现方法.我的目的很明确就是在UI层通过Castle 调用数据访问层的方法. 添加项目引用 CastleDemo ...

  7. Castle Windsor常用介绍以及其在ABP项目的应用介绍

    最近在研究ABP项目,有关ABP的介绍请看阳光铭睿 博客,ABP的DI和AOP框架用的是Castle Windsor下面就对Castle Windsor项目常用方法介绍和关于ABP的使用总结 1.下载 ...

  8. 在ABP项目的应用Castle Windsor

    Castle Windsor常用介绍以及其在ABP项目的应用介绍 最近在研究ABP项目,有关ABP的介绍请看阳光铭睿 博客,ABP的DI和AOP框架用的是Castle Windsor下面就对Castl ...

  9. IOC 容器在 ASP.NET MVC 中的应用

    IOC 容器在 ASP.NET MVC 中的应用 IOC:Inversion Of Control 翻译为控制反转,我们在面向对象软件开发过程中,一个应用程序它的底层结构可能由N种不同的构件来相互协作 ...

随机推荐

  1. Spring Boot 2 实践记录之 MyBatis 集成的启动时警告信息问题

    按笔者 Spring Boot 2 实践记录之 MySQL + MyBatis 配置 中的方式,如果想正确运行,需要在 Mapper 类上添加 @Mapper 注解. 但是加入此注解之后,启动时会出现 ...

  2. Checkpoint--在Tempdb上的特殊性

    由于Checkpoint的目的是为减少数据库恢复时间,而每次实例重启都会创建新的tempdb,而不需要恢复,因此checkpoint在Tempdb上行为与其他用户数据库上略微不同. 1. 系统引发的c ...

  3. [ASP.NET]大文件无法上传排查经验分享

    最近我们标桥下载模块,在经过正常更新后,发现软件包无法上传. 临时解决方案 因为问题结点在于文件无法上传到服务器,所以我们临时手动将文件丢到服务器,通过测试服务器将数据造出来,然后再更新到正式数据库, ...

  4. Android studio项目预览的时候提示错误ActionBarOverlayLayout

    android studio打开项目(别人的demo),提示页面没法预览.截图如下 根据查询,是主题没法正常显示,需要修改样式.样式文件的路径为res\values\styles.xml,截图如下. ...

  5. C# OleDbConnection对特定部分Excel的数据读取

    最近在写winform程序,先来一个简单的. 读取特定部分Excel的数据读取,读取Excel第30行开始到H列的数据 using System;using System.Collections.Ge ...

  6. C#扩展一个现有的类

    做个记录,写个示例 using System; class Rubbish { public void Say() { Console.Write("Hello"); } } st ...

  7. jzoj5931

    根據打表可得,對於n的情況 任意一個首位!=1的排列時,則其答案-1可以與首位為1的情況對應 當n=4時 排列 答案 1 2 3 4 ------ 0 1 2 4 3 ------ 1 1 3 2 4 ...

  8. 五,session数据写入memcached

    1,session数据通常保存在服务器端的文件中,它的默认过期时间是1440s.我们可以将session数据保存到memcached中,设定memcached的过期时间大于session过期时间即可. ...

  9. 深入理解 js this 绑定机制

    函数调用位置 与词法作用域相反的是,this的指向由函数运行时决定,它是动态的,随着函数调用位置变化而变化. 要理解 this,首先要理解调用位置:调用位置就是函数在代码中被调用的位置(而 不是声明的 ...

  10. Redis 的 Sentinel

    Redis 的 Sentinel 系统用于管理多个 Redis 服务器(instance), 该系统执行以下三个任务: 监控(Monitoring): Sentinel 会不断地检查你的主服务器和从服 ...