windsor使用构造函数参数注册泛型类型

时间:2009-08-28 06:46:38

标签: castle-windsor ioc-container

我有这样的事情

 MyRepository<T> : IRepository<T> {

  public MyRepository(string cs){
   ....

}

所以我需要在winsdor中注册这个泛型类型并给他一个参数

我一直试图这样做:

Type t = typeof(IRepository<>);
Type t1 = typeof(Repository<>);
Hashtable props = new Hashtable(); 
props.Add("cs", "myconnstring");
container.AddComponentWithProperties("key1", t, t1, props);

我收到以下错误

无法创建组件'key1',因为它具有要满足的依赖性。 key1正在等待以下依赖项:

键(具有特定键的组件) - 未注册的cs。

2 个答案:

答案 0 :(得分:3)

试试这个:

container.Register(Component.For(typeof(IRepository<>))
   .ImplementedBy(typeof(MyRepository<>))
   .Parameters(Parameter.ForKey("cs").Eq("myconnstring"));

查看fluent registration wiki了解详情。

答案 1 :(得分:0)

您可以使用此

var container = new WindsorContainer();


        container.Register(Component.For(typeof(ICustomGenericRepository<>))
        .ImplementedBy(typeof(CustomGenericRepository<>))
        .LifeStyle.Transient);