如何在组件的 class 中注入 DateTimeProvider / wrapper 依赖项但不使用构造函数?
how can I inject DateTimeProvider / wrapper dependency in a component's class but not using the constructor?
在 webforms 应用程序中,假设我们有一个 class,它有一个使用 DateTime.Now 完成一些工作的方法,如果我想测试这个方法,我可以注入一个 DateTimeWrapper使用构造函数,但在我的场景中,我不希望 IoC 容器为我构造 class,而是我想使用它 "new it" 并使其可测试,我认为每个都没有意义当我们要使用这个 class 时,应该注入 DateTimeWrapper "which returns the real DateTime.Now" 的依赖项!
注意:没有在 WebForms 页面中注入任何内容 class。
public class EmailNotification
{
public IDateTimeWrapper DateTimeWrapper { get; set; }
public void SetBody(string body)
{
...
Body = body + DateTimeWrapper.Now.ToString();
}
}
我尝试使用 "Autofac" 来使用 属性 注入,并且以下工作非常完美:
builder.RegisterType<DateTimeWrapper>()
.As<IDateTimeWrapper>();
builder.RegisterType<EmailNotification>().PropertiesAutowired();
// in this case, it works
var email = container.Resolve<EmailNotification>();
email.SetBody("hello world!");
但是我需要这样使用组件:
// somehow I want the DateTimeProvider "the real one" to be injected or used at this point
var email= new EmailNotification();
email.SetBody("hello world!");
// I'm getting a NullReferenceException at the last line because the dependency has been never injected
你有什么想法吗?
提前致谢
解决方案很简单 "i think",我更改了这个:
public class EmailNotification
{
public IDateTimeWrapper DateTimeWrapper { get; set; }
public void SetBody(string body)
{
...
Body = body + DateTimeWrapper.Now.ToString();
}
}
以下内容:
public class EmailNotification
{
private IDateTimeWrapper _dateTimeWrapper = new DateTimeWrapper();
public IDateTimeWrapper DateTimeWrapper
{
get
{
return _dateTimeWrapper;
}
set
{
_dateTimeWrapper = value ?? throw new ArgumentNullException(nameof(DateTimeWrapper));
}
}
public void SetBody(string body)
{
...
Body = body + DateTimeWrapper.Now.ToString();
}
}
所以我仍然将 IDateTimeWrapper 作为单元测试的接缝,同时在构造 EmailNotification 的实例时默认初始化它 class。
在 webforms 应用程序中,假设我们有一个 class,它有一个使用 DateTime.Now 完成一些工作的方法,如果我想测试这个方法,我可以注入一个 DateTimeWrapper使用构造函数,但在我的场景中,我不希望 IoC 容器为我构造 class,而是我想使用它 "new it" 并使其可测试,我认为每个都没有意义当我们要使用这个 class 时,应该注入 DateTimeWrapper "which returns the real DateTime.Now" 的依赖项!
注意:没有在 WebForms 页面中注入任何内容 class。
public class EmailNotification
{
public IDateTimeWrapper DateTimeWrapper { get; set; }
public void SetBody(string body)
{
...
Body = body + DateTimeWrapper.Now.ToString();
}
}
我尝试使用 "Autofac" 来使用 属性 注入,并且以下工作非常完美:
builder.RegisterType<DateTimeWrapper>()
.As<IDateTimeWrapper>();
builder.RegisterType<EmailNotification>().PropertiesAutowired();
// in this case, it works
var email = container.Resolve<EmailNotification>();
email.SetBody("hello world!");
但是我需要这样使用组件:
// somehow I want the DateTimeProvider "the real one" to be injected or used at this point
var email= new EmailNotification();
email.SetBody("hello world!");
// I'm getting a NullReferenceException at the last line because the dependency has been never injected
你有什么想法吗? 提前致谢
解决方案很简单 "i think",我更改了这个:
public class EmailNotification
{
public IDateTimeWrapper DateTimeWrapper { get; set; }
public void SetBody(string body)
{
...
Body = body + DateTimeWrapper.Now.ToString();
}
}
以下内容:
public class EmailNotification
{
private IDateTimeWrapper _dateTimeWrapper = new DateTimeWrapper();
public IDateTimeWrapper DateTimeWrapper
{
get
{
return _dateTimeWrapper;
}
set
{
_dateTimeWrapper = value ?? throw new ArgumentNullException(nameof(DateTimeWrapper));
}
}
public void SetBody(string body)
{
...
Body = body + DateTimeWrapper.Now.ToString();
}
}
所以我仍然将 IDateTimeWrapper 作为单元测试的接缝,同时在构造 EmailNotification 的实例时默认初始化它 class。