Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

9
  • 3
    Why do you "usually use a factory"? In most cases you can simply inject an instance instead of a factory. And even if you need a factory, Func<IFoo> is already a factory, no need to wrap it in another class+interface. Commented Apr 29, 2016 at 13:42
  • 1
    @CodesInChaos good question. It's really just a way for me to abstract the creation of models in order to be able to create really testable code with 100% code-coverage. I find it easier to mock the objects if I abstract the creation of them. Commented Apr 29, 2016 at 13:44
  • Injecting an instance already removes the creation of the object from your class. I'd only inject a factory if the class needs to create multiple instances with different parameters. Commented Apr 29, 2016 at 13:45
  • @CodesInChaos I've added some examples to when I use a factory. Commented Apr 29, 2016 at 14:18
  • 1
    @RobertHarvey Good point. I added an actual factory that I use. But I really want to find a balance and know what's "good practice". But I guess it's really depends on the particular case... Commented Apr 29, 2016 at 15:27