Constructor Injection in C#/Unity?

前端 未结 3 1172
醉梦人生
醉梦人生 2020-12-05 03:21

I\'m using C# with Microsoft\'s Unity framework. I\'m not quite sure how to solve this problem. It probably has something to do with my lack of understanding DI with Unity.<

3条回答
  •  庸人自扰
    2020-12-05 04:02

    Unless you register respectively "joe" and "timmy" as named dependencies, you can't be sure that "timmy" is injected into Schoolbus. In fact, if you attempt to register two instances of the same class as unnamed dependencies, you will have an ambiguous setup, and you will not be able to resolve Person at all.

    In general, if you have to register a lot of named instances you are probably going about DI in the wrong way. The main idea of DI is to resolve Domain Services more than Domain Objects.

    The primary idea of DI is to provide a mechanism that allows you to resolve abstract types (interfaces or abstract classes) into concrete types. Your example has no abstract types, so it doesn't really make a lot of sense.

提交回复
热议问题