Castle Windsor Fluent Registration API
一对一注册
直接注册组件
container.Register(
Component.For<MyServiceImpl>()
);
注册接口并提供组件
container.Register(
Component.For<IMyService>()
.ImplementedBy<MyServiceImpl>()
);
//非泛型重载方法,和上面的效果是一样的
container.Register(
Component.For(typeof(IMyService))
.ImplementedBy(typeof(MyServiceImpl))
);
注册泛型类
container.Register(
Component.For(typeof(IRepository<>)
.ImplementedBy(typeof(NHRepository<>)
);
配置组件的声明周期
container.Register(
Component.For<IMyService>()
.ImplementedBy<MyServiceImpl>()
.LifeStyle.Transient
);
如果配置LifeStyle,默认的声明周期是Singleton
同一个接口注册多个组件
container.Register(
Component.For<IMyService>().ImplementedBy<MyServiceImpl>(),
Component.For<IMyService>().ImplementedBy<OtherServiceImpl>()
);
这种情况下,当容器解析获得组件的时候默认得到的是第一个被注册的,现在是MyServiceImpl,但是我们可以通过IsDefault方法来设置默认组件,
container.Register(
Component.For<IMyService>().ImplementedBy<MyServiceImpl>(),
Component.For<IMyService>().ImplementedBy<OtherServiceImpl>().IsDefault()
);
这样Resolve将会得到OtherServiceImpl组件,还有一种方法就是给每个组件起名字,然后通过名字Resolve
container.Register(
Component.For<IService>().ImplementedBy<Service1>().Named("service1"),
Component.For<IService>().ImplementedBy<Service2>().Named("service2"));
var service = container.Resolve<IService>("service2");
使用委托作为组件工厂
container
.Register(
Component.For<IMyService>()
.UsingFactoryMethod(
() => MyLegacyServiceFactory.CreateMyService())
);
UsingFactoryMethod方法使注册更灵活,还有一个UsingFactory方法,文档建议不用
注册组件给多个服务
container.Register(
Component.For<IUserRepository, IRepository>()
.ImplementedBy<MyRepository>()
);
按约定注册组件
BasedOn
container.Register(
Classes.FromThisAssembly()
.BasedOn<SmartDispatcherController>()
.Configure(c => c.Lifestyle.Transient)
);
BasedOn 注册所有继承它的组件,解析的时候不能用BasedOn基于的类型,也就是说
container.Resolve<SmartDispatcherController>(); 将会抛异常,使用继承它的组件的类型
WithService.Base()
把最末端的组件注册进去,B继承A,C继承B,把C作为组件注册进去
WithService.DefaultInterfaces()
把最末端并且名字包含接口名字的组件注册进去
WithService.FromInterface()
container.Register(
Classes.FromThisAssembly()
.BasedOn().WithService.FromInterface()
);
举例说明
public interface IService {}
public interface ICalculatorService : IService
{
float Add(float op1, float op2);
}
public class CalculatorService : ICalculatorService
{
public float Add(float op1, float op2)
{
return op1 + op2;
}
}
IService实际上作为一个标记接口来达到某种效果(没想到有什么用呢),等价于
container.Register(
Component.For<ICalculatorService>().ImplementedBy<CalculatorService>()
);
Resolve的时候也是通过类型ICalculatorService获得组件,通过IService会抛异常
WithService.Self()
container.Register(
Classes.FromThisAssembly()
.BasedOn().WithService.Self()
);
举例说明
public interface IService {}
public interface ICalculatorService : IService
{
float Add(float op1, float op2);
}
public class CalculatorService : ICalculatorService
{
public float Add(float op1, float op2)
{
return op1 + op2;
}
}
public class CalculatorService2 : ICalculatorService
{
public float Add(float op1, float op2)
{
return op1 + op2;
}
}
等价于(实践出真知)
container.Register(
Component.For<CalculatorService>(),
Component.For<CalculatorService2>()
);
叠加使用
Classes.FromThisAssembly()
.BasedOn<IFoo>()
.WithService.Self()
.WithService.Base()
等价于
Component.For<IFoo, FooImpl>().ImplementedBy<FooImpl>();
IncludeNonPublicTypes
把abstract、internal、sealed类都给注册进去了,默认是不注册这些类的,使用时请注意
Configure method
container.Register(
Classes.FromAssembly(Assembly.GetExecutingAssembly())
.BasedOn<ICommon>()
.LifestyleTransient()
.Configure(component => component.Named(component.Implementation.FullName + "XYZ"))
);
ConfigureFor method
container.Register(
Classes.FromThisAssembly()
.BasedOn<ICommon>()
.LifestyleTransient()
.Configure(
component => component.Named(component.Implementation.FullName + "XYZ")
)
.ConfigureFor<CommonImpl1>(
component => component.DependsOn(Property.ForKey("key1").Eq(1))
)
.ConfigureFor<CommonImpl2>(
component => component.DependsOn(Property.ForKey("key2").Eq(2))
)
);
还没搞明白
ConfigureIf method
container.Register(
Classes.FromThisAssembly()
.BasedOn<ICommon>()
.LifestyleTransient()
.Configure(
component => component.Named(component.Implementation.FullName + "XYZ")
)
.ConfigureIf(
x => x.Implementation.Name.StarsWith("Common"),
component => component.DependsOn(Property.ForKey("key1").Eq(1))
)
);
Castle Windsor Fluent Registration API的更多相关文章
- castle windsor学习-----Fluent Registration API 注册
使用xml配置和fluent注册两种搭配使用需要注意的是: 如果先在WindsorContainer构造函数指明用xml配置进行注册,如下设置 IWindsorContainer container ...
- ASP.NET Web API - 使用 Castle Windsor 依赖注入
示例代码 项目启动时,创建依赖注入容器 定义一静态容器 IWindsorContainer private static IWindsorContainer _container; 在 Applica ...
- Castle Windsor 学习-----Installer的几种安装方式
翻译 当使用依赖注入容器时,你首先要向容器中注册你的组件,Windsor使用installers(该类型实现IWindsorInstaller接口)来封装和隔离注册的逻辑,可以使用Configurat ...
- Castle Windsor
让我们从Web API的集成点开始,它们是IDependencyResolver和IDependencyScope接口.IDependencyResolver和其他接口的名称可能与MVC中的接口相同, ...
- Castle Windsor常用介绍以及其在ABP项目的应用介绍
最近在研究ABP项目,有关ABP的介绍请看阳光铭睿 博客,ABP的DI和AOP框架用的是Castle Windsor下面就对Castle Windsor项目常用方法介绍和关于ABP的使用总结 1.下载 ...
- Aspect Oriented Programming using Interceptors within Castle Windsor and ABP Framework AOP
http://www.codeproject.com/Articles/1080517/Aspect-Oriented-Programming-using-Interceptors-wit Downl ...
- [Castle Windsor]学习依赖注入
初次尝试使用Castle Windsor实现依赖注入DI,或者叫做控制反转IOC. 参考: https://github.com/castleproject/Windsor/blob/master/d ...
- 在ABP项目的应用Castle Windsor
Castle Windsor常用介绍以及其在ABP项目的应用介绍 最近在研究ABP项目,有关ABP的介绍请看阳光铭睿 博客,ABP的DI和AOP框架用的是Castle Windsor下面就对Castl ...
- 在ASP.NET MVC中使用Castle Windsor
平常用Inject比较多,今天接触到了Castle Windsor.本篇就来体验其在ASP.NET MVC中的应用过程. Visual Studio 2012创建一个ASP.NET MVC 4网站. ...
随机推荐
- HTTP-304 NOT Modified
http://www.douban.com/note/161120791/ http://blog.sina.com.cn/s/blog_4c98b9600100jd4z.html http://ww ...
- 关于C51内的code,idata,xdata
从数据存储类型来说,8051系列有片内.片外程序存储器,片内.片外数据存储器,片内程序存储器还分直接寻址区和间接寻址类型,分别对应code.data.xdata.idata以及根据51系列特点而设定的 ...
- SCP和SFTP(都使用SSH。但SCP上传不能中断,而SFTP可以续传,这是最大区别)
不管SCP还是SFTP,都是SSH的功能之一.都是使用SSH协议来传输文件的. 不用说文件内容,就是登录时的用户信息都是经过SSH加密后才传输的,所以说SCP和SFTP实现了安全的文件传输. SCP和 ...
- 【HDOJ】1892 See you~
wa了十次,原来变量名写错.二维树状数组. #include <cstdio> #include <cstring> #define MAXN 1002 int nums[MA ...
- rootkit的检测工具使用(chkrootkit和rootkit hunter)
信息安全 这两天突然发现我们的服务器产生大量DNS解析连线.为了查明问题,就下载网上找工具检查问题所在.用了两个工具,一个chkrootkit,另外一个rootkit huntur. ...
- C++11并发内存模型学习
C++11标准已发布多年,编译器支持也逐渐完善,例如ms平台上从vc2008 tr1到vc2013.新标准对C++改进体现在三方面:1.语言特性(auto,右值,lambda,foreach):2.标 ...
- senrty 配置Email
测试页面在这里 右上角头像->管理->邮件 配置如下:(注意一点:465是SSL的 587是TLS的) 其他django email 1.3 文献在这里 现在都1.8了貌似 如 ...
- P1082丛林探险
P1082丛林探险 描述 东非大裂谷中有一片神秘的丛林,是全世界探险家的乐园,著名黄皮肤探险家BB一直想去试试.正好我国科学家2005年4月将首次对东非大裂谷进行科考,BB决定随科考队去神秘丛林探险. ...
- JavaScript高级程序设计16.pdf
第8章 BOM BOM的核心对象就是window,它表示浏览器的一个实例,在浏览器中window对象有双重角色,它既是JavaScript访问浏览器的一个接口,又是规定的Global对象,因此所有在全 ...
- .Net remoting 的文件传输
http://www.codeproject.com/Articles/14100/Dot-Net-Remoting-Handling-Remote-Events-using-Dele