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 ...
随机推荐
- java游戏开发基础Swing之JRadioButton
© 版权声明:本文为博主原创文章,转载请注明出处 1.按钮(JButton) Swing中的按钮是JButton,它是javax.swing.AbstractButton类的子类,Swing中的按钮可 ...
- oracle中位图索引和B-tree索引的区别
1.适用系统的不同:位图索引适合OLAP系统,而B-tree索引适合OLTP系统. 2.占用存储空间不同:位图索引只需要很小的存储空间,而B-tree索引需要占用很大的存储空间. 3.创建需要的时间不 ...
- springboot 中使用AOP
网上关于AOP的例子好多,各种名词解释也一大堆,反正名词各种晦涩,自己写个最最最简单的例子入门mark一下,以后再深入学习. maven依赖 <dependency> <groupI ...
- Linux Apache安装加载mod_deflate模块
为了开启apache服务器中的gzip压缩功能,mod_deflate模块是必须安装加载的.现在介绍如何安装.1.进入到mod_deflate.c目录 cd /lamp/httpd-2.2.20/mo ...
- 解决Apache长时间占用内存大的问题,Apache 内存优化方法
问:为什么服务器在连续运行多天后或访问峰值后,进程中的一个Apache.exe占用内存几百兆不减少?答:用记事本打开apache2\conf\httpd.conf,我在centos5上装了kloxo, ...
- PHP性能:序——谈性能
PHP性能:序——谈性能 这里不谈PHP的性能和其他语言的性能,这里讨论PHP自身的性能问题. 性能是什么? 通俗的来讲,性能,就是在固定的环境下能做的事情的多少. 为什么要性能? 1.每一个软件或网 ...
- ansible的异步执行
ansible任务的异步执行 96 茶客furu声 关注 2016.07.12 01:40* 字数 458 阅读 1777评论 0喜欢 4 ansible方便在于能批量下发,并返回结果和呈现.简单.高 ...
- 【JMeter4.0学习(十)】之JMeter函数简单运用以及结合正则表达式提取器
下面来简单的举个栗子: 首先,把函数和正则表达式提取器放在一块来介绍,如下所示: 1.结构完整展示,下面再一步一步创建添加: 2.添加线程组: 3.首先添加HTTP请求1 4.添加结果树后,运行后查看 ...
- IntelliJ idea——》删除tag
查看git上所有tag E:\eju_IdeaProjects\house-platform>git tag --11v1. 20181107周三上线 20181120周二上线 v1.0.0 v ...
- 原创:解决 python中moviepy调用ffmpeg的错误:subprocess, PermissionError: [WinError 5] 拒绝访问
近期运行一个python程序用到了moviepy.editor.VideoFileClip() moviepy基于ffmpeg,但是并不是pip安装的ffmepg, 执行 import imageio ...