Dependency Injection Initialization

前端 未结 5 1753
天涯浪人
天涯浪人 2021-02-06 14:47

Please note: I have just started using AutoFac to learn about DI and IoC.

Is dependency injection supposed to be initialized in the controllers constructor?

Ho

5条回答
  •  我在风中等你
    2021-02-06 15:42

    private IMyService iMyService;
    public HomeController(IMyServices myService)
    {
        iMyService = myService;
    }  
    

    in this case,you only care about what you need(IMyServices)

    public IMyService iMyService = new MyService();  
    

    but in this case,you care about what it will be(new MyService())

    when design a class,you know what you need;
    but what it will be,that not decided by you,that decided by who use your class

提交回复
热议问题