castle windsor学习-----Registering components one-by-one 注册类型
1。在容器中注册一个类型
container.Register(
Component.For<IMyService>()
.ImplementedBy<MyServiceImpl>()
);
2。注册一个非默认的类型(non-default service)
container.Register(
Component.For<IMyService>()
.ImplementedBy<MyServiceImpl>()
);
不用泛型
// Same result as example above.
container.Register(
Component.For(typeof(IMyService))
.ImplementedBy(typeof(MyServiceImpl))
);
3.注册泛型类型
// Registering a repository for each entity is not needed.
container.Register(
Component.For<IRepository<Customer>>()
.ImplementedBy<NHRepository<Customer>>(),
Component.For<IRepository<Order>>()
.ImplementedBy<NHRepository<Order>>(),
// and so on...
);
// Does not work (compiler won't allow it):
container.Register(
Component.For<IRepository<>>()
.ImplementedBy<NHRepository<>>()
);
// Use typeof() and do not specify the entity:
container.Register(
Component.For(typeof(IRepository<>)
.ImplementedBy(typeof(NHRepository<>)
);
4.配置组件的生命周期
container.Register(
Component.For<IMyService>()
.ImplementedBy<MyServiceImpl>()
.LifeStyle.Transient
);
默认为单例
5.对同一个服务注册不同的组件
container.Register(
Component.For<IMyService>().ImplementedBy<MyServiceImpl>(),
Component.For<IMyService>().ImplementedBy<OtherServiceImpl>()
);
默认采取第一个组件
通过default方法设置默认组件
container.Register(
Component.For<IMyService>().ImplementedBy<MyServiceImpl>(),
Component.For<IMyService>().Named("OtherServiceImpl").ImplementedBy<OtherServiceImpl>().IsDefault()
);
6。注册已有的对象
var customer = new CustomerImpl();
container.Register(
Component.For<ICustomer>().Instance(customer)
);
用已存在的对象注册,则忽略生命周期
7.使用委托作为组件工厂
container
.Register(
Component.For<IMyService>()
.UsingFactoryMethod(
() => MyLegacyServiceFactory.CreateMyService())
);
UsingFactoryMethod 有多个重载
提供kernel和上下文
container.Register(
Component.For<IMyFactory>().ImplementedBy<MyFactory>(),
Component.For<IMyService>()
.UsingFactoryMethod(kernel => kernel.Resolve<IMyFactory>().Create())
);
container.Register(
Component.For<User>().Instance(user),
Component.For<AbstractCarProviderFactory>(),
Component.For<ICarProvider>()
.UsingFactory((AbstractCarProviderFactory f) => f.Create(container.Resolve<User>()))
);
8. OnCreate
有时候需要检查或者改变对象的创建过程,可以使用OnCreate方法实现
container.Register(
Component.For<IService>()
.ImplementedBy<MyService>()
.OnCreate((kernel, instance) => instance.Name += "a")
);
9.组件有特殊的名称
container.Register(
Component.For<IMyService>()
.ImplementedBy<MyServiceImpl>()
.Named("myservice.default")
);
10.组件的依赖
如果一个组件需要另一个组件来运行,这就是一个依赖,当注册的时候需要明确给定一个组件来重写服务
container.Register(
Component.For<IMyService>()
.ImplementedBy<MyServiceImpl>()
.Named("myservice.default"),
Component.For<IMyService>()
.ImplementedBy<OtherServiceImpl>()
.Named("myservice.alternative"), Component.For<ProductController>()
.ServiceOverrides(ServiceOverride.ForKey("myService").Eq("myservice.alternative"))
); public class ProductController
{
// Will get a OtherServiceImpl for myService.
// MyServiceImpl would be given without the service override.
public ProductController(IMyService myService)
{
}
}
11.给多个服务注册组件
container.Register(
Component.For<IUserRepository, IRepository>()
.ImplementedBy<MyRepository>()
);
用一个组件给一个或多个服务进行注册,例如你有一个类FooBar同时实现了IFoo和IBar接口,你可以配置容器,当两个接口需要时返回同一个实现类型
container.Register(
Component.For<IUserRepository, IRepository>()
.ImplementedBy<MyRepository>()
);
castle windsor学习-----Registering components one-by-one 注册类型的更多相关文章
- castle windsor学习-----Registering components by conventions
注册多个组件 1.one-by-one注册组件可能是一项非常重复的工作,可以通过Classes或Types注册一组组件(你可以指定一些特定的特征) 三个步骤 注册多个类型通常采取以下结构 contai ...
- castle windsor学习-----How components are created
- [Castle Windsor]学习依赖注入
初次尝试使用Castle Windsor实现依赖注入DI,或者叫做控制反转IOC. 参考: https://github.com/castleproject/Windsor/blob/master/d ...
- Castle Windsor 学习-----Installer的几种安装方式
翻译 当使用依赖注入容器时,你首先要向容器中注册你的组件,Windsor使用installers(该类型实现IWindsorInstaller接口)来封装和隔离注册的逻辑,可以使用Configurat ...
- castle windsor学习----- Services and Components 两者的定义
- castle windsor学习-----XML Inline Parameters 内联参数
当使用XML配置的时候,可能要给组件指定各种各样的依赖 1.简单的参数 参数名称不区分大小写 <component id="ping" type="Acme.Crm ...
- castle windsor学习----- Referencing types in XML 在xm文件中引用类型
当从xml引用installer的语法如下 <install type="Acme.Crm.Infrastructure.ServicesInstaller, Acme.Crm.Inf ...
- castle windsor学习-----Inline dependencies 依赖
应用程序中的很多组件都会依赖其他的服务组件,很多依赖一些不合法的组件或者容器中没有的组件,例如int类型.string类型.TimeSpan类型 Windsor支持以上的场景,注册API有Depend ...
- castle windsor学习-------Container Events 容器的事件
所有的事件是实现IKernelEvents 接口,已容器的Kernel属性暴露出来 1. AddedAsChildKernel 当前的容器添加子容器或其他容器时触发 2. RemovedAsChild ...
随机推荐
- css 温故而知新 1px的问题
解决方法1: //border @mixin border($pos, $color) { content: ""; position: absolute; transform-o ...
- IMSDroid问题集
1.IMSDroid切换摄像头后的接收方横屏显示.事实上非常多种情况下都会突然发现就横屏了.解决的方法就是切换摄像头时同一时候切换横竖屏显示 2.IMSDroid掉音问题:IMSDroid通话几分钟后 ...
- HTML5 2D平台游戏开发#10Wall Jump
这个术语不知道怎么翻译比较贴切,但并不妨碍对字面意思的理解,大概就是飞檐走壁.比如: 这是游戏<忍者龙剑传>中的场景,玩家可以通过操纵角色在墙面上移动并跳跃. 首先需要实现角色抓墙这一动作 ...
- Java中常用的加密算法MD5,SHA,RSA
1. MD5加密,常用于加密用户名密码,当用户验证时. protected byte[] encrypt(byte[] obj){ try { MessageDigest md5 = Messag ...
- jfreechart折线图 demo
public class ChartUtil { public static ChartUtil chartUtil; private RoomViewsDataService roomViewsDa ...
- Windows找出占用端口的进程
第一步:找出监听指定端口的进程号: C:\> netstat -ao | findstr 443 TCP 0.0.0.0:443 Sean-NotePC:0 ...
- solr原理
1.solr原理: 我本人的理解:solr是为解决高性能的全文索引而出现的,它将用户输入的关键字进行智能分解,分解成一个个词,过滤掉一些多余的停词及空格等,比如,“在”.“里面”.“也”.“的”.“它 ...
- python 深复制与浅复制------copy模块
模块解读: 浅复制: x = copy.copy(y)深复制: x = copy.deepcopy(y)(注:模块特有的异常,copy.Error) 深copy与浅copy的差别主要体现在当有混合对象 ...
- 【BZOJ4269】再见Xor 高斯消元
[BZOJ4269]再见Xor Description 给定N个数,你可以在这些数中任意选一些数出来,每个数可以选任意多次,试求出你能选出的数的异或和的最大值和严格次大值. Input 第一行一个正整 ...
- OWASP-A5-安全配置错误
1.安全配置错误 安全配置错误可以发生在一个应用程序堆栈的任何层面,包括平台.Web服务器.应用服务器.数据库.框架和自定义代码. 开发人员和系统管理员需共同努力,以确保整个堆栈的正确配置.自动扫描器 ...