Cle*_*man 0 castle-windsor windsor-3.0
我是Windsor的新手,我正在尝试实现最基本的构造函数注入.显然,API在最近的版本中发生了很大的变化,当前版本的文档似乎假设您已经知道如何操作,旧版本的文档已经过时.
我有一个简单的测试组件:
public class ConstructorInjectedComponent
{
public IMyComponent Component { get; set; }
public ConstructorInjectedComponent(IMyComponent component)
{
Component = component;
}
}
Run Code Online (Sandbox Code Playgroud)
有一个简单的IMyComponent实现:
public class AMyComponent : IMyComponent
{
public string Name { get; set; }
public AMyComponent()
{
Name = Guid.NewGuid().ToString("N");
}
}
Run Code Online (Sandbox Code Playgroud)
我想以某种方式在Windsor中注册我的类型,这样我就可以获得一个包含其依赖项实例的ConstructorInjectedComponent实例:IMyComponent.
我像这样注册AMyComponent:
_container.Register(Component.For(typeof(AMyComponent)));
Run Code Online (Sandbox Code Playgroud)
我像这样注册ConstructorInjectedComponent:
_container.Register(Component.For(typeof(ConstructorInjectedComponent)));
Run Code Online (Sandbox Code Playgroud)
并尝试解决它
_container.Resolve(typeof(ConstructorInjectedComponent));
Run Code Online (Sandbox Code Playgroud)
但是失败了"无法创建组件ConstructorInjectedComponent,因为它具有需要满足的依赖性.
所以我尝试为ConstructorInjectedComponent传递一个依赖项IDictionary ......这就是文档失败的地方.
我不知道如何定义该字典.我找不到解释它的文档.我试过这个:
var d = new Dictionary<string, string>() {{"IMyComponent", "AMyComponent"}};
_container.Register(Component.For(typeof(ConstructorInjectedComponent))
.DependsOn(dependencies));
Run Code Online (Sandbox Code Playgroud)
但是失败了同样的"具有需要解决的依赖性"错误.
我究竟做错了什么?
首先,确保您了解基本概念至关重要,即组件是什么,服务是什么以及依赖是什么.
有关如何使用注册API的文档应该可以帮助您开始使用.
tl; dr asnwer是:因为ConstructorInjectedComponent取决于IMyComponent确保您注册AMyComponent以IMyComponent作为服务公开 .
_container.Register(Component.For<IMyComponent>().ImplementedBy<AMyComponent>());
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2882 次 |
| 最近记录: |