Constructor Injection - Do we inject factories as well?

前端 未结 3 1213
一整个雨季
一整个雨季 2021-02-15 07:08

After listening to the Clean Code Talks, I came to understand that we should use factories to compose objects. So, for example, if a House has a Door a

3条回答
  •  萌比男神i
    2021-02-15 07:34

    If you end up using Unity, I have recently implemented an equivalent of Castle Windsor Typed Factories for Unity. You can find the project at https://github.com/PombeirP/Unity.TypedFactories, and the NuGet package at http://nuget.org/packages/Unity.TypedFactories.

    The usage is the following:

    unityContainer
        .RegisterTypedFactory()
        .ForConcreteType();
    

    You just have to create the IFooFactory interface with a method returning IFoo, and the rest is done for you by the library. You can resolve IFooFactory and use it to create IFoo objects straight away.

提交回复
热议问题