我在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. Delphi事件的广播 转

    http://blog.sina.com.cn/s/blog_44fa172f0102wgs2.html 原文地址:Delphi事件的广播 转作者:MondaySoftware 明天就是五一节了,辛苦 ...

  2. Jersey构建Restful风格的webservices

    最近一直在搞老项目的开发工作,很少写博文了.听了两位阿里巴巴大牛的讨论,决定试试用restful风格的webservices看看. 这里用的是Jersey这个框架,刚开始弄,有点麻烦,只能到处查资料. ...

  3. Replication--对发布修改的一些小总结

    --==================================================== --在华丽分割线下,是我对肖磊--大菠萝的崇高地敬意和婶婶地感谢,本文乃肖兄表述我执笔而来 ...

  4. Angular6 学习笔记——内容投影, ViewChild和ContentChild

    angular6.x系列的学习笔记记录,仍在不断完善中,学习地址: https://www.angular.cn/guide/template-syntax http://www.ngfans.net ...

  5. SQL学习笔记1

    2018.10.15:周一   -- 返回前5个数据 SELECT TOP 5 * FROM Student;   -- 返回前50%的数据 SELECT TOP 50 PERCENT * FROM ...

  6. Hibernate一级缓存测试分析

    Hibernate 一级缓存测试分析 Hibernate的一级缓存就是指Session缓存,此Session非http的session会话技术,可以理解为JDBC的Connection,连接会话,Se ...

  7. 毕业回馈-89C51之数码管的使用

    7段码的数码管由7个LED等共同组成,根据公共端的不同有共阴和共阳之分.现在很多数码管在7段码的基础上加了一个.即dp,其内部结构如下图所示: 公共端为LED灯的阴极,所以为共阴极接法: 公共端为阳极 ...

  8. 【算法】Matrix - Tree 矩阵树定理 & 题目总结

    最近集中学习了一下矩阵树定理,自己其实还是没有太明白原理(证明)类的东西,但想在这里总结一下应用中的一些细节,矩阵树定理的一些引申等等. 首先,矩阵树定理用于求解一个图上的生成树个数.实现方式是:\( ...

  9. MySQL(视图、触发器、函数)

    day61 参考:http://www.cnblogs.com/wupeiqi/articles/5713323.html 视图 视图:给某个查询语句设置别名,日后方便使用               ...

  10. Ceres入门笔记

    介绍 Ceres可以解决下列形式的边界约束鲁棒非线性最小二乘问题 (1) $\min\limits_{x}\quad \frac{1}{2} \sum\limits_{i}\rho_{i}\left( ...