zoukankan      html  css  js  c++  java
  • 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>()
    );
  • 相关阅读:
    为什么多线程读写 shared_ptr 要加锁?
    iOS开发那些事iOS6 UI状态保持和恢复
    Hook钩子程序(续)
    JavaScript 项目构建工具 Grunt 实践:任务和指令
    浏览器插件之ActiveX开发(五)
    mongodb副本集架构搭建
    Hadoop 0.23.3版本
    Grails 中使用 grailseventspush 实现 ajax/comet/websocket 消息推送
    razor中@相对于<% %>的优势
    Chrome developer tool介绍(javascript调试)
  • 原文地址:https://www.cnblogs.com/lanpingwang/p/6533817.html
Copyright © 2011-2022 走看看