使用 DynamicParameters 时缺少依赖项
使用 Castle.Windsor 2.5.4.32 和 Castle.Core 2.5.2.0。
我有一个组件,其构造函数中需要一个 string[] 。字符串[] 来自“设置”类。我不希望组件依赖 在设置类上,因为它只需要 string[]。所以我用了 DynamicProperties 从设置类中提取数据,该数据是从容器中提取的。至少, 这就是我认为会发生的事情。温莎无法解决我的问题 组件,因为它说缺少依赖项(字符串[])。
这是一个快速 (MSTest) 测试用例。这是我第一次使用 DynamicParameters 所以我可能做错了什么。有什么想法吗?
namespace WindsorTests.DynamicParametersTest
{
using Castle.MicroKernel.Registration;
using Castle.Windsor;
using Microsoft.VisualStudio.TestTools.UnitTesting;
[TestClass]
public class DPTest
{
[TestMethod]
public void TestMethod1()
{
var container = new WindsorContainer();
container.Register(
Component.For<ISettings>().ImplementedBy<Settings>(),
Component.For<Foo>().DynamicParameters((k, p) =>
{
var settings = k.Resolve<ISettings>();
p["data"] = settings.MoreData;
})
);
var bar = container.Resolve<Foo>();
}
}
public interface ISettings
{
string[] MoreData { get; }
}
public class Settings : ISettings
{
public string[] MoreData
{
get { return new[] {"A", "B", "C"}; }
}
}
public class Foo
{
public Foo(string[] data)
{
}
}
}
Using Castle.Windsor 2.5.4.32 and Castle.Core 2.5.2.0.
I've got a component that needs a string[] in its ctor. The string[]
comes from a "settings" class. I don't want the component dependent
on the settings class since it just needs the string[]. So I used
DynamicProperties to pull the data from the settings class, which was pulled from the container. At least,
that's what I thought would happen. Windsor is not able to resolve my
component as it says there's a missing dependency (the string[]).
Here's a quick (MSTest) test case. This is the first time I've used
DynamicParameters so I might be doing something wrong. Any ideas?
namespace WindsorTests.DynamicParametersTest
{
using Castle.MicroKernel.Registration;
using Castle.Windsor;
using Microsoft.VisualStudio.TestTools.UnitTesting;
[TestClass]
public class DPTest
{
[TestMethod]
public void TestMethod1()
{
var container = new WindsorContainer();
container.Register(
Component.For<ISettings>().ImplementedBy<Settings>(),
Component.For<Foo>().DynamicParameters((k, p) =>
{
var settings = k.Resolve<ISettings>();
p["data"] = settings.MoreData;
})
);
var bar = container.Resolve<Foo>();
}
}
public interface ISettings
{
string[] MoreData { get; }
}
public class Settings : ISettings
{
public string[] MoreData
{
get { return new[] {"A", "B", "C"}; }
}
}
public class Foo
{
public Foo(string[] data)
{
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的使用情况完全没问题。
我刚刚在 Windsor 3 上运行了测试用例,它通过了。所以我想这可能是 2.5.x 中的一个错误
Your usage is perfectly fine.
I just ran the test case on Windsor 3 and it's passing. So I suppose that might be a bug in 2.5.x