我在我的应用程序中使用 Unity。在注入(inject)值的构造函数中检查空值是否有意义?
public class ExampleClass
{
public ExampleClass(IExampleArgument e)
{
if (e == null)
//Do something
}
}
参数不应该已经被解析并绑定(bind)到一个值吗?如果出现问题,它应该在解析期间发生,所以我要么在这里有一个值,要么我没有?
请您参考如下方法:
Does it make sense to check for null values in the constructor for the injected values?
当然。
Shouldn't the argument be resolved already and bound to a value?
没有任何强制客户端使用注入(inject)的东西——从语法上讲,使用 null 值调用构造函数是完全有效的。我假设 null 值会在程序的后面导致问题,因此您决定 - 您是想在该构造函数中处理问题还是让它在程序的后面自然发生?
