Geo*_*uer 11 .net castle-windsor
说我有以下课程
MyComponent : IMyComponent {
public MyComponent(int start_at) {...}
}
Run Code Online (Sandbox Code Playgroud)
我可以通过xml使用castle windsor注册它的实例,如下所示
<component id="sample" service="NS.IMyComponent, WindsorSample" type="NS.MyComponent, WindsorSample">
<parameters>
<start_at>1</start_at >
</parameters>
</component>
Run Code Online (Sandbox Code Playgroud)
我将如何在代码中执行完全相同的操作?(注意,构造函数参数)
Chr*_*nal 16
编辑:使用Fluent接口使用以下代码的答案:)
namespace WindsorSample
{
using Castle.MicroKernel.Registration;
using Castle.Windsor;
using NUnit.Framework;
using NUnit.Framework.SyntaxHelpers;
public class MyComponent : IMyComponent
{
public MyComponent(int start_at)
{
this.Value = start_at;
}
public int Value { get; private set; }
}
public interface IMyComponent
{
int Value { get; }
}
[TestFixture]
public class ConcreteImplFixture
{
[Test]
void ResolvingConcreteImplShouldInitialiseValue()
{
IWindsorContainer container = new WindsorContainer();
container.Register(
Component.For<IMyComponent>()
.ImplementedBy<MyComponent>()
.Parameters(Parameter.ForKey("start_at").Eq("1")));
Assert.That(container.Resolve<IMyComponent>().Value, Is.EqualTo(1));
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14097 次 |
| 最近记录: |