我正在使用StructureMap注册一个类,该类的构造函数参数包括一个TimeSpan,另一个TimeSpan是该类的属性。当我在StructureMap中使用命名构造函数参数时,构造函数参数的值会应用于我的构造函数参数和任何公共类属性,这些属性都是TimeSpans。我还尝试将类切换为DateTimes而不是TimeSpans,并获得了相同的结果。因此,我的问题是,我是否正确地使用了StructureMap,或者应该以另一种方式注册这个类?谢谢!
以下是一个简单的接口和类,用于演示这个问题:
这是StructureMap容器的container.Model.For().Default.DescribeBuildPlan()函数的输出结果。
以下是一个简单的接口和类,用于演示这个问题:
public interface ITimeSpanTest
{
TimeSpan Timeout { get; set; }
TimeSpan LogTimeout { get; set; }
}
public class TimeSpanTest : ITimeSpanTest
{
public TimeSpan LogTimeout { get; set; }
public TimeSpan Timeout { get; set; }
public string Process { get; set; }
public TimeSpanTest(TimeSpan logTimeout, string process)
{
this.Timeout = TimeSpan.FromSeconds(1);
this.LogTimeout = logTimeout;
this.Process = process;
}
}
以下是StructureMap的注册代码
Container container = new Container();
container.Configure(c =>
{
c.Scan(x =>
{
x.TheCallingAssembly();
});
c.For<ITimeSpanTest>().Use<TimeSpanTest>()
.Ctor<TimeSpan>("logTimeout").Is(TimeSpan.FromMinutes(5))
.Ctor<string>("process").Is("Process")
.Singleton();
});
这是StructureMap容器的container.Model.For().Default.DescribeBuildPlan()函数的输出结果。
PluginType: SMTest.ITimeSpanTest
Lifecycle: Singleton
new TimeSpanTest(TimeSpan, String process)
? TimeSpan = Value: 00:05:00
? String process = Value: Process
Set TimeSpan LogTimeout = Value: 00:05:00
Set TimeSpan Timeout = Value: 00:05:00
您可以看到,TimeSpan构造函数参数的"logTimeout"名称似乎被忽略了。Timeout属性被设置为00:05:00,而应该是00:00:01。我正在使用StructureMap 3.1.6.186。