How to avoid Dependency Injection constructor madness?

前端 未结 9 1833
别跟我提以往
别跟我提以往 2020-11-22 03:59

I find that my constructors are starting to look like this:

public MyClass(Container con, SomeClass1 obj1, SomeClass2, obj2.... )

with ever

相关标签:
9条回答
  • 2020-11-22 04:31

    I read this whole thread, twice, and I think people are responding by what they know, not by what is asked.

    JP's original question looks like he's constructing objects by sending a resolver, and then a bunch of classes, but we're assuming that those classes/objects are themselves services, ripe for injection. What if they are not?

    JP, if you're looking to leverage DI and desire the glory of mixing injection with contextual data, none of these patterns (or supposed "anti-patterns") specifically address that. It actually boils down to using a package which will support you in such an endeavor.

    Container.GetSevice<MyClass>(someObject1, someObject2)
    

    ... this format is rarely supported. I believe the difficulty of programming such support, added to the miserable performance that would be associated with the implementation, makes it unattractive for opensource developers.

    But it should be done, because I should be able to create and register a factory for MyClass'es, and that factory should be able to receive data/input that aren't pushed into being a "service" just for the sake of passing data. If "anti-pattern" is about negative consequences, then forcing the existence of artificial service types for passing data/models is certainly negative (on par with your feeling about wrapping up your classes into a container. Same instinct applies).

    There are framework that may help, though, even if they look a bit ugly. For example, Ninject:

    Creating an instance using Ninject with additional parameters in the constructor

    That's for .NET, is popular, and is still nowhere as clean as it should be, but I'm sure there's something in whatever language you choose to employ.

    0 讨论(0)
  • 2020-11-22 04:32

    Injecting the container is a shortcut that you will ultimately regret.

    Over injection is not the problem, it is usually a symptom of other structural flaws, most notably separation of concerns. This is not one problem but can have many sources and what makes this so difficult to fix is that you are going to have to deal with all of them, sometimes at the same time (think of untangling spaghetti).

    Here is an incomplete list of the things to look out for

    Poor Domain Design (Aggregate root’s …. etc)

    Poor separation of concerns (Service composition, Commands, queries) See CQRS and Event Sourcing.

    OR Mappers (be careful, these things can lead you into trouble)

    View Models and other DTO’s (Never reuse one, and try to keep them to a minimal !!!!)

    0 讨论(0)
  • 2020-11-22 04:35

    I came across a similar question about constructor based dependency Injection and how complex it was getting to pass in all the dependencies.

    One of the approach, I have used in past is to use the application facade pattern using a service layer. This would have a coarse API. If this service depends on repositories, It would use a setter injection of the private properties. This requires creating an abstract factory and moving the logic of creating the repositories into a factory.

    Detailed code with explanation can be found here

    Best practices for IoC in complex service layer

    0 讨论(0)
  • 2020-11-22 04:36

    The difficulty of passing in the parameters is not the problem. The problem is that your class is doing too much, and should be broken down more.

    Dependency Injection can act as an early warning for classes getting too big, specifically because of the increasing pain of passing in all of the dependencies.

    0 讨论(0)
  • 2020-11-22 04:40

    You are right that if you use the container as a Service Locator, it's more or less a glorified static factory. For lots of reasons I consider this an anti-pattern.

    One of the wonderful benefits of Constructor Injection is that it makes violations of the Single Responsibility Principle glaringly obvious.

    When that happens, it's time to refactor to Facade Services. In short, create a new, more coarse-grained interface that hides the interaction between some or all of the fine-grained dependencies you currently require.

    0 讨论(0)
  • 2020-11-22 04:40

    I don't think your class constructors should have a reference to your IOC container period. This represents an unnecessary dependency between your class and the container (the type of dependency IOC is trying to avoid!).

    0 讨论(0)
提交回复
热议问题