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
If you inject too many factories that is a code smell called constructor over-injection that indicates your class is doing too much.
Many containers provide a feature called auto-factories. That means they generate factories of type Func
automatically if they know how to generate T
.
Castle Windsor has an advanced feature called Typed Factory facilities which generates implementations of a factory interface on-the-fly.
There is also a port of typed factories for Unity in the TecX project.