Constructor with parameter in Controller - MVC

后端 未结 4 868
温柔的废话
温柔的废话 2021-02-03 14:21

I have read few articles on IOC and Unity and got my self confused :(

So going back to basics can some one tel me what the below code does?

private IStud         


        
4条回答
  •  北荒
    北荒 (楼主)
    2021-02-03 14:22

    In IoC you need to register the interface and a class which is implementing the interface. So once you register then whenever you have signature like above IoC will automatically create an instance of IStudent implemented class and inject it to the object while initializing the controller. It saves the time and efforts to declare the members. In above case you just have one to declare, but it could be more in numbers and those all could need few more instances to pass to the controller. Once we register those all correctly IoC do its work thereafter. Infact we can decide the Scope/Lifetime of the injected members. It can be PerInstance/Per Request/ Or Singleton.

    There are saveral IoC frameworks available its up to you which one you want to go with.

提交回复
热议问题