在.net核心中,如果我使用依赖注入,所有构造函数参数必须由DI提供吗?
让我们说:
public Person CreateClient()
{
string phone = "12345678";
return new Person(phoneNumber: phone);
}
public class Person
{
private readonly ISomeService _service;
private readonly string _phoneNumber;
public Person (ISomeService service, string phoneNumber)
{
_service = service;
_phoneNumber = phoneNumber;
}
public string PhoneNumber {get { return _phoneNumber; } }
public string Gender {get { return _service.GetGenderFromDb(); } }
}
public interface ISomeService
{
String GetGenderFromDb();
}
public class FooService : ISomeService
{
public String GetGenderFromDb() { return "Male"; }
}
这可能是客户提供的DI和值保持在同一个构造函数中吗?
在您调用"new"来创建对象的任何地方都不适合从上到下进行构造函数DI.当您想要将参数传递给构造函数时,DI不适用.
正如其他人所提到的,最好的方法是创建一个工厂.它可能看起来像这样.
public class Person
{
private readonly ISomeService _service;
private readonly string _phoneNumber;
public Person (ISomeService service, string phoneNumber)
{
_service = service;
_phoneNumber = phoneNumber;
}
public string PhoneNumber {get { return _phoneNumber; } }
public string Gender {get { return _service.GetGenderFromDb(); } }
}
public class PersonFactory : IPersonFactory
{
private readonly ISomeService _someService;
public PersonFactory(ISomeService someService)
{
_someService = someService;
}
public GetPerson(string phoneNumber)
{
return new Person(_someService, phoneNumber);
}
}
现在,当你想创建一个人时,你会注入一个实例IPersonFactory
,然后调用GetPerson
它.
此外,您可能会发现您希望您的模型更加简单,工厂也可以完成大部分繁重的工作.我发现此时性别来自数据库,因此您可以将其更改为更像以下内容:
public class Person
{
public Person (string gender, string phoneNumber)
{
Gender = gender;
PhoneNumber = phoneNumber;
}
public string PhoneNumber {get; private set; }
public string Gender {get; private set;}
}
public class PersonFactory : IPersonFactory
{
private readonly ISomeService _someService;
public PersonFactory(ISomeService someService)
{
_someService = someService;
}
public GetPerson(string phoneNumber)
{
var gender = _someService.GetGenderFromDb();
return new Person(gender, phoneNumber);
}
}
现在你的Person类没有关于从哪里获取Gender的任何细节,工厂每次都会研究如何创建Person模型.