2009-09-05 20 views
0

这里是我的情况: 我有一个ISampleProvider接口:如何在解析WindsorContainer服务表单时注入依赖关系?

public interface ISampleProvider<TEntity> 
{ 
TEntity Entity{get;} 
} 

这里是这个接口的实现:

public class SampleProvider<TEntity>:ISampleProvider<TEntity> 
{ 
public SampleProvider(TEntity entity) 
{ 
Entity=entity; 
} 
public TEntity Entity 
{ 
get;private set; 
} 
} 

我想注入实体到SampleProvider当我解决这个问题WindsorContainer 所以我写了这一点:

var container=new WindsorContainer(); 
container.AddComponent("smaple_provider",typeof(ISampleProvider<Person>),typeof(SampleProvider<Person>)); 
var arguments=new Hashtable{{"entity",new Person()}}; 
var sampleProvider=container.Resolve<ISampleProvider<Person>>(arguments); 

但它不干活g和依赖关系解析器异常抛出,说“在配置中检测到周期”

显然我做错了什么。

回答

2

请确保您有温莎2.0 ... 这个测试工作正常,我:

[TestFixture] 
public class SampleProviderTests { 
    public interface ISampleProvider<TEntity> { 
     TEntity Entity { get; } 
    } 

    public class SampleProvider<TEntity> : ISampleProvider<TEntity> { 
     public SampleProvider(TEntity entity) { 
      Entity = entity; 
     } 

     public TEntity Entity { get; private set; } 
    } 

    public class Person {} 

    [Test] 
    public void test() { 
     var container = new WindsorContainer(); 
     container.AddComponent("smaple_provider", typeof(ISampleProvider<Person>), typeof(SampleProvider<Person>)); 
     var arguments = new Hashtable { { "entity", new Person() } }; 
     var sampleProvider = container.Resolve<ISampleProvider<Person>>(arguments);    
    } 
}