castle windsor学习-----Conditional component registration 条件注册

有时候需要根据一些条件进行组件的注册

1.注册一个组件当这个组件之前没被注册过

container.Register(
    Component.For(typeof(IRepository<>))
        .ImplementedBy(typeof(Repository<>))
        .OnlyNewServices()
);

2.过滤一些组件

container.Register(
    Classes.Of<ICustomer>()
        .FromAssembly(Assembly.GetExecutingAssembly())
        .Unless(t => typeof(SpecificCustomer).IsAssignableFrom(t))
);

注册实现ICustomer接口的类型(除了SpecificCustomer类和它的子类)

container.Register(
   AllTypes.Of<ICustomer>()
      .FromAssembly(Assembly.GetExecutingAssembly())
      .If(t => t.FullName.Contains("Chain"))
);
container.Register(
    AllTypes.FromAssembly(Assembly.GetExecutingAssembly())
        .Where(Component.IsInSameNamespaceAs<FooRepository>())
        .WithService.FirstInterface()
);
container.Register(
    Classes.Of<CustomerChain1>()
        .Pick(from type in Assembly.GetExecutingAssembly().GetExportedTypes()
              where type.IsDefined(typeof(SerializableAttribute), true)
              select type
        )
);

 

posted @ 2017-03-11 10:45  蓝平凡  阅读(193)  评论(0编辑  收藏  举报