Simplest explanation of how a DI container works?

后端 未结 3 615
甜味超标
甜味超标 2021-02-07 20:35

In simple terms and/or in high-level pseudo-code, how does a DI container work and how is it used?

3条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-07 21:33

    You configure a DI container so it knows about your interfaces and types - how each interface maps to a type.

    When you call Resolve on it, it looks at the mapping and returns the mapped object you have requested.

    Some DI containers use conventions over configuration, where for example, if you define an interface ISomething, it will look for a concrete Something type to instantiate and return.

提交回复
热议问题