Is it correct to have 2 constructors, one for dependency injection, and the other an injection resolution?

I have 2 constructors in my class:

public class VuelingCacheWebServices : IVuelingCacheWebService { public IVuelingCache apiConnector { get; set; } public VuelingCacheWebServices(IVuelingCache ApiConnector) { apiConnector = ApiConnector; } public VuelingCacheWebServices() : this(new VuelingCache()) { } } 

As you can see, I have one constructor depending on IVuelingCache and the default constructor, which creates an instance to go to the first constructor. Is it correct? this way I get rid of the Factory class.

+4
source share
4 answers

This is one way to make sure that you have a valid instance of IVuelingCache - this template has a name - an injection of dependency of poor people.

Some see this as an anti-pattern, as you hardcode the default implementation into your code.

+4
source

There is some discussion that this model is a good idea:

http://lostechies.com/chadmyers/2009/07/14/just-say-no-to-poor-man-s-dependency-injection/

In my opinion, if you have a good default that is not logically an external dependency, then you should specify this default value. If your "default" really should not be the default, or will mix too much parts too much apart, do not use this template.

You connect your code by doing this, so I really think long and hard about what will be used as the default convenience for locally accessible default implementations, or if you really just use it as a crutch, and as a result strong related dependencies.

In addition, many people recommend using constructor injection only for necessary dependencies and using property nesting for optional dependencies. When you use the wrong DI human template, you make an optional (or not applicable) dependency. You might want to consider injecting properties for these cases with an additional dependency, as this is the pattern that many expect.

+1
source

No, this is not a good idea, because your class will learn about the implementation of IVuelingCache ( new VuelingCache() ).

+1
source

There are several problems with your approach:

  • This makes your code less clear. This is not only code for writing, but also harder to find out which dependencies it takes. When you have one single constructor, it immediately becomes clear what its dependencies are.
  • This makes the class dependent on specific instances. This makes separation of abstraction and implementation difficult and may be architecturally poor.
  • This forces you to make changes to this class when changing implementations, which is bad for maintainability. Say, for example, your application uses ICommandHandler<T> interfaces as an abstraction for executing business commands, and that your class depends on ICommandHandler<UpdateUserCommand> and points to a specific UpdateUserCommandHandler . When you later want to wrap any ICommandHandler<T> inside DurationLoggingCommandHandler<T> , DeadLockRetryCommandHandler` or not, you need to go through your full application to replace all the dependencies, otherwise it is just a matter of reconfiguring the configuration in the process of launching your application path.
0
source

Source: https://habr.com/ru/post/971279/


All Articles