Simple Injector:Factory classes that need to create classes with dependencies

前端 未结 4 1868
灰色年华
灰色年华 2021-02-14 15:45

I have a factory class that creates a couple of different types of class. The factory is registered with the container. What is the recommended way of creating the classes insid

4条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-14 16:28

    This is something of a classic question.

    Both of your solutions could be problematic if the number of different implementations increases, especially if the dependencies for each of the implementations have a lot of variance. You could end up with a constructor that takes 20 parameters.

    My preferred implementation is to just have the factory class reference the container, and resolve the needed instance that way.

    Some might argue that this is no better than the Service Locator anti-pattern, but I don't feel like there is a perfect solution to this problem, and doing it this way seems the most natural to me.

提交回复
热议问题