Castle Windsor Fluent Registration API
一对一注册
直接注册组件
container.Register(
Component.For<MyServiceImpl>()
);
注册接口并提供组件
container.Register(
Component.For<IMyService>()
.ImplementedBy<MyServiceImpl>()
);
//非泛型重载方法,和上面的效果是一样的
container.Register(
Component.For(typeof(IMyService))
.ImplementedBy(typeof(MyServiceImpl))
);
注册泛型类
container.Register(
Component.For(typeof(IRepository<>)
.ImplementedBy(typeof(NHRepository<>)
);
配置组件的声明周期
container.Register(
Component.For<IMyService>()
.ImplementedBy<MyServiceImpl>()
.LifeStyle.Transient
);
如果配置LifeStyle,默认的声明周期是Singleton
同一个接口注册多个组件
container.Register(
Component.For<IMyService>().ImplementedBy<MyServiceImpl>(),
Component.For<IMyService>().ImplementedBy<OtherServiceImpl>()
);
这种情况下,当容器解析获得组件的时候默认得到的是第一个被注册的,现在是MyServiceImpl,但是我们可以通过IsDefault方法来设置默认组件,
container.Register(
Component.For<IMyService>().ImplementedBy<MyServiceImpl>(),
Component.For<IMyService>().ImplementedBy<OtherServiceImpl>().IsDefault()
);
这样Resolve将会得到OtherServiceImpl组件,还有一种方法就是给每个组件起名字,然后通过名字Resolve
container.Register(
Component.For<IService>().ImplementedBy<Service1>().Named("service1"),
Component.For<IService>().ImplementedBy<Service2>().Named("service2"));
var service = container.Resolve<IService>("service2");
使用委托作为组件工厂
container
.Register(
Component.For<IMyService>()
.UsingFactoryMethod(
() => MyLegacyServiceFactory.CreateMyService())
);
UsingFactoryMethod方法使注册更灵活,还有一个UsingFactory方法,文档建议不用
注册组件给多个服务
container.Register(
Component.For<IUserRepository, IRepository>()
.ImplementedBy<MyRepository>()
);
按约定注册组件
BasedOn
container.Register(
Classes.FromThisAssembly()
.BasedOn<SmartDispatcherController>()
.Configure(c => c.Lifestyle.Transient)
);
BasedOn 注册所有继承它的组件,解析的时候不能用BasedOn基于的类型,也就是说
container.Resolve<SmartDispatcherController>(); 将会抛异常,使用继承它的组件的类型
WithService.Base()
把最末端的组件注册进去,B继承A,C继承B,把C作为组件注册进去
WithService.DefaultInterfaces()
把最末端并且名字包含接口名字的组件注册进去
WithService.FromInterface()
container.Register(
Classes.FromThisAssembly()
.BasedOn
);
举例说明
public interface IService {}
public interface ICalculatorService : IService
{
float Add(float op1, float op2);
}
public class CalculatorService : ICalculatorService
{
public float Add(float op1, float op2)
{
return op1 + op2;
}
}
IService实际上作为一个标记接口来达到某种效果(没想到有什么用呢),等价于
container.Register(
Component.For<ICalculatorService>().ImplementedBy<CalculatorService>()
);
Resolve的时候也是通过类型ICalculatorService获得组件,通过IService会抛异常
WithService.Self()
container.Register(
Classes.FromThisAssembly()
.BasedOn
);
举例说明
public interface IService {}
public interface ICalculatorService : IService
{
float Add(float op1, float op2);
}
public class CalculatorService : ICalculatorService
{
public float Add(float op1, float op2)
{
return op1 + op2;
}
}
public class CalculatorService2 : ICalculatorService
{
public float Add(float op1, float op2)
{
return op1 + op2;
}
}
等价于(实践出真知)
container.Register(
Component.For<CalculatorService>(),
Component.For<CalculatorService2>()
);
叠加使用
Classes.FromThisAssembly()
.BasedOn<IFoo>()
.WithService.Self()
.WithService.Base()
等价于
Component.For<IFoo, FooImpl>().ImplementedBy<FooImpl>();
IncludeNonPublicTypes
把abstract、internal、sealed类都给注册进去了,默认是不注册这些类的,使用时请注意
Configure method
container.Register(
Classes.FromAssembly(Assembly.GetExecutingAssembly())
.BasedOn<ICommon>()
.LifestyleTransient()
.Configure(component => component.Named(component.Implementation.FullName + "XYZ"))
);
ConfigureFor method
container.Register(
Classes.FromThisAssembly()
.BasedOn<ICommon>()
.LifestyleTransient()
.Configure(
component => component.Named(component.Implementation.FullName + "XYZ")
)
.ConfigureFor<CommonImpl1>(
component => component.DependsOn(Property.ForKey("key1").Eq(1))
)
.ConfigureFor<CommonImpl2>(
component => component.DependsOn(Property.ForKey("key2").Eq(2))
)
);
还没搞明白
ConfigureIf method
container.Register(
Classes.FromThisAssembly()
.BasedOn<ICommon>()
.LifestyleTransient()
.Configure(
component => component.Named(component.Implementation.FullName + "XYZ")
)
.ConfigureIf(
x => x.Implementation.Name.StarsWith("Common"),
component => component.DependsOn(Property.ForKey("key1").Eq(1))
)
);