检查注入的参数中的空值是否有意义? [英] Does it make sense to check for null values in injected arguments?
问题描述
我在我的应用程序中使用Unity。在构造函数中检查注入值是否为空值?
I am using Unity in my application. Does it make sense to check for null values in the constructor for the injected values?
public class ExampleClass
{
public ExampleClass(IExampleArgument e)
{
if (e == null)
//Do something
}
}
参数是否应该已经解析并绑定到值上?而且如果发生问题,它应该在解决过程中发生,所以我在这里有值还是没有?
Shouldn't the argument be resolved already and bound to a value? And in case a problem happens, it should occur during resolving, so I either have a value here or I don't?
推荐答案
在构造函数中检查注入值是否为空值有意义吗?
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?
没有任何强制客户使用注入的方法-语法上以 null
值调用构造函数是完全有效的。我假设 null
值会在程序的后面引起问题,所以您决定-是否要在该构造函数中处理该问题,或者让它稍后自然发生在程序中?
There's nothing forcing clients to use injection - it would be perfectly valid syntactically to call your constructor with a null
value. I'm assuming that a null
value would cause a problem later in the program, so you decide - do you want to handle the problem in that constructor or let it occur naturally later in the program?
这篇关于检查注入的参数中的空值是否有意义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!