Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 依赖项注入和默认构造函数的使用_C#_Dependency Injection_Code Injection - Fatal编程技术网

C# 依赖项注入和默认构造函数的使用

C# 依赖项注入和默认构造函数的使用,c#,dependency-injection,code-injection,C#,Dependency Injection,Code Injection,我试图理解依赖注入的概念。 下面是我试图调试的示例。 在这里,我创建了Customer类,我将其依赖项注入到他的构造函数中 现在当我调用this.Iorder.GetOrderDetails()时在Index方法中,它给出了NullReferenceException错误,并要求使用new关键字创建一个对象来调用该方法。 当我移动这个调用时,this.Iorder.GetOrderDetails()调用另一个方法GetCutomerDetails(),并在有效的索引方法中调用该方法 问题:我无法

我试图理解依赖注入的概念。 下面是我试图调试的示例。 在这里,我创建了Customer类,我将其依赖项注入到他的构造函数中 现在当我调用
this.Iorder.GetOrderDetails()时
Index
方法中,它给出了
NullReferenceException
错误,并要求使用
new
关键字创建一个对象来调用该方法。 当我移动这个调用时,
this.Iorder.GetOrderDetails()
调用另一个方法
GetCutomerDetails()
,并在有效的索引方法中调用该方法

问题:我无法理解为什么
这个.Iorder.GetOrderDetails()方法调用在
索引
方法中不起作用,以及为什么它在
GetCutomerDetails()中起作用。

控制器:

public class CustomerController: Controller
{
    private IorderDetails Iorder;

    //DI constructor  Injecting OrderDetails object 
    CustomerController(IorderDetails iorderObj)
    {
        if (iorderObj == null)
            throw new ArgumentNullException("orderObj should not be null");

        this.Iorder = iorderObj;
    }
    //Default constructor  
    public CustomerController() { }

    public ActionResult Index()
    {            
       CustomerController objCustomer = new CustomerController(new CustomerModel());
       objCustomer.GetCutomerDetails();

       //Commented GetOrderDetails() method
       //this.Iorder.GetOrderDetails();            
       return View();
    }

    public ActionResult GetCutomerDetails()
    {
        this.Iorder.GetOrderDetails();
        return View();
    }
}

您拥有
CustomerController
的默认构造函数。调用它时,您不会将任何内容分配给
Iorder
。在这种情况下,它是
null
。之后,在方法
Index()
中,您尝试使用
Iorder
执行方法
GetOrderDetails()
,该方法为空。这是失败的。在方法
Index()
内部创建
CustomerController
的另一个实例时,可以对该实例调用
GetOrderDetails()


通常,当与依赖项注入结合使用时,不建议使用多个构造函数。但在必要时,您应该将所有实例字段初始化为有效的字段。这称为本地默认值。这是一本关于依赖注入模式和反模式的好书。还可以看看作者的博客。

你是说依赖注入?你用什么将IOrderDetails注入CustomerController类?删除默认构造函数,你就会明白为什么你的代码不使用索引方法。
CustomerController
创建了另一个
CustomerController
实例,很可能这只是一个错误或糟糕的设计。
public class CustomerController: Controller
{
    private IorderDetails Iorder;

    //DI constructor  Injecting OrderDetails object 
    CustomerController(IorderDetails iorderObj)
    {
        if (iorderObj == null)
            throw new ArgumentNullException("orderObj should not be null");

        this.Iorder = iorderObj;
    }
    //Default constructor  
    public CustomerController() { }

    public ActionResult Index()
    {            
       CustomerController objCustomer = new CustomerController(new CustomerModel());
       objCustomer.GetCutomerDetails();

       //Commented GetOrderDetails() method
       //this.Iorder.GetOrderDetails();            
       return View();
    }

    public ActionResult GetCutomerDetails()
    {
        this.Iorder.GetOrderDetails();
        return View();
    }
}