构造映射构造函数参数应用于属性
本文关键字:应用于 属性 参数 构造函数 映射 | 更新日期: 2023-09-27 18:33:19
我正在向StructureMap注册一个类,该类在构造函数参数中包含一个TimeSpan,另一个TimeSpan作为该类的属性。 当我在 StructureMap 中使用命名构造函数参数时,构造函数参数的值将应用于我的构造函数参数和任何作为 TimeSpan 的公共类属性。 我还尝试将类切换到日期时间而不是时间跨度,并得到了相同的结果。 所以我的问题是,我是否正确使用了 StructureMap,还是应该以另一种方式注册这个类?谢谢!
下面是一个简单的接口和类来演示这个问题:
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;
}
}
这是结构图注册码
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 容器的输出。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"名称似乎被忽略了。 超时属性设置为 00:05:00,而它应该是 00:00:01。 我正在使用结构图 3.1.6.186。
我在这里没有得到答案,但我发布了StructureMap Google Group。 杰里米·米勒的答案是
https://groups.google.com/forum/#!topic/structuremap-users/4wA737fnRdw
基本上,这是一个已知问题,可能不会修复,因为通过将 Timeout 设置为只读很容易解决。 这可以通过删除 Timeout 属性中的集来完成。 例如
public interface ITimeSpanTest
{
TimeSpan Timeout { get; }
TimeSpan LogTimeout { get; set; }
}
public class TimeSpanTest : ITimeSpanTest
{
public TimeSpan LogTimeout { get; set; }
public TimeSpan Timeout { get; }
public string Process { get; set; }
public TimeSpanTest(TimeSpan logTimeout, string process)
{
this.Timeout = TimeSpan.FromSeconds(1);
this.LogTimeout = logTimeout;
this.Process = process;
}
}
在这种特殊情况下,工作正常。 最终,这个问题似乎是 StructureMap setter 注入器将 logTimeout 设置应用于所有公共 TimeSpan 属性。 我还使用表现出相同行为的日期时间进行了测试。 但是,浮点型和整型则不然。 这发生在 StructureMap 3 的最后几个版本和最新版本 4 中。