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 注册类型的更多相关文章

  1. castle windsor学习-----Registering components by conventions

    注册多个组件 1.one-by-one注册组件可能是一项非常重复的工作,可以通过Classes或Types注册一组组件(你可以指定一些特定的特征) 三个步骤 注册多个类型通常采取以下结构 contai ...

  2. castle windsor学习-----How components are created

  3. [Castle Windsor]学习依赖注入

    初次尝试使用Castle Windsor实现依赖注入DI,或者叫做控制反转IOC. 参考: https://github.com/castleproject/Windsor/blob/master/d ...

  4. Castle Windsor 学习-----Installer的几种安装方式

    翻译 当使用依赖注入容器时,你首先要向容器中注册你的组件,Windsor使用installers(该类型实现IWindsorInstaller接口)来封装和隔离注册的逻辑,可以使用Configurat ...

  5. castle windsor学习----- Services and Components 两者的定义

  6. castle windsor学习-----XML Inline Parameters 内联参数

    当使用XML配置的时候,可能要给组件指定各种各样的依赖 1.简单的参数 参数名称不区分大小写 <component id="ping" type="Acme.Crm ...

  7. castle windsor学习----- Referencing types in XML 在xm文件中引用类型

    当从xml引用installer的语法如下 <install type="Acme.Crm.Infrastructure.ServicesInstaller, Acme.Crm.Inf ...

  8. castle windsor学习-----Inline dependencies 依赖

    应用程序中的很多组件都会依赖其他的服务组件,很多依赖一些不合法的组件或者容器中没有的组件,例如int类型.string类型.TimeSpan类型 Windsor支持以上的场景,注册API有Depend ...

  9. castle windsor学习-------Container Events 容器的事件

    所有的事件是实现IKernelEvents 接口,已容器的Kernel属性暴露出来 1. AddedAsChildKernel 当前的容器添加子容器或其他容器时触发 2. RemovedAsChild ...

随机推荐

  1. ros之串口通信---imu

    1.sudo apt-get install ros-kinetic-rosserial 或者sudo git clonegit://github.com/wjwwood/serial.git  (开 ...

  2. Git--Bug解决篇

    Git--公司bug解决篇 作为程序员,我们时常遇到这样的场景,公司的产品上线了,程序员们美滋滋的开始开发新功能希望得到更多的流量.这时候,公司上线的产品发生了很严重的bug,可是我们已经在这个bug ...

  3. JSTL JSP页面推断某个cookie的值和读取值....

    <c:if test="${cookie['woshop'].value eq '1'}">                 <div>           ...

  4. nodeJS 中关于 promise 的使用

    var readInfo = function (fileName) { var defer = q.defer(); fs.readFile(fileName, {encoding:'utf-8'} ...

  5. AccessibilityService 官网介绍

    AccessibilityService extends Service java.lang.Object    ↳ android.content.Context      ↳ android.co ...

  6. springmvc上传方法

    /** * * @param file 上传的文件 * @param filePath 上传到那个目录 * @return 上传后的文件名字 * @throws IOException */ publ ...

  7. 请描述Java中的时间监听机制?

    1.时间监听涉及到三个组件:事件源.事件对象.事件监听器 2.当事件源上发生某个动作时,它会调用事件监听器的一个方法,并将事件对象穿进去,开发人员在监听器中通过事件对象,拿到事件源,从而对事件源进行操 ...

  8. linux下LAMP环境搭建

    ++++++++++++++++++++++++++++++++++++++++++++++ linux下LAMP环境搭建 ++++++++++++++++++++++++++++++++++++++ ...

  9. 关于函数的return

    function add(x, y) { var total = x + y; return total; } add(5,10); 关于函数的return 我一开始是认为没有什么用的  后来在项目中 ...

  10. Myecplise Tomcat 启动很慢

    今天突然遇到一个问题,tomcat在Myecplse启动非常慢,直接用tomcat自带的start.bat启动很快,如果通过Myeclipse启动会发现项目一直在实例化,最后发现是因为加了断点调试,断 ...