【发布时间】:2013-01-24 17:45:35
【问题描述】:
我有一个使用ServiceStack 创建的服务。我将 Funq 用于我的依赖注入,因为它默认附带 ServiceStack,但这可能是其他 DI 容器表现出的行为。
我在启动时注册我的类型:
this.AppContainer.RegisterAs<ConcreteDownloadServices, IDownloadServices>();`
我在一个类中有这个构造函数:
private readonly IDownloadServices downloadServices;
public ExampleService(IDownloadServices downloadServices)
{
this.downloadServices = downloadServices;
}
而我的具体实现的构造函数是:
public ConcreteDownloadServices()
{
this.ArbitraryProperty = "ArbitraryString";
}
一切都解决得很好,我可以进入代码并查看事情是否按预期工作。但是,AbritraryProperty 没有正确设置。我可以单步执行并看到它设置为字符串值,但是一旦代码返回到调用代码(在此示例中,它将是 ExampleService 的构造函数)ArbitraryProperty 现在为 null。
- 这是设计使然吗?
- 如果是设计使然,那么使用 DI 是否意味着我不应该在具体实现的构造函数中做任何事情?
- 如果是设计使然,为自动属性设置默认值的正确方法是什么?
- 如果不是设计使然,那是怎么回事?
【问题讨论】:
标签: c# dependency-injection servicestack funq