During a code review I have started having a bit of a dilemma as to whether use dependency injection or not. I would like to hear your thoughts, because this is kind of an ongoing theme and would help in the future code reviews as well.Before I start I want to say that from my knowledge DI is mostly used for dependency management and better, much easier unit testing (correct me if I am wrong please).Now here is the scenario:This is the class that will be used as a dependency only once and probably for good. Meaning it will stay that way for a while and no other class will be using it.Reason being is because it is a refactoring of some legacy code and there was not much to be done but to separate it into another class, at least as a first good step, for following the SRP.Also, another thing is that the hypothetical doSomethingImportant() hits the database.public SomeClass{   public Object doSomethingImportant()   {      ...   }}Based on that information do you think it is okay to new up the dependency in the other class as opposed to using DI since:Dependency management argument kind of falls off since it is only going to be used once.&Unit testing also falls off since I would rather do an integration or acceptance test to see how the method is interacting with the database in a real-life application.public SomeOtherClass{   private readonly SomeClass _someClass = new SomeClass();   public Object doSomethingImportantUsingDependency()   {      ...   }}I was personally inclined towards doing DI because it is considered good practice, but for a second there it felt like I was blindingly following the rules and not thinking it over since there are always exceptions to the rule.What are your thoughts on this? I would love to hear.PS: I don't think this is a generic when should I use DI question because it is very specific to this particular situation when unit tests are of no use and the class is going to be used only once so there is no need to centralize it for dependency management (even though it is good practice in general).
Should one use Dependency Injection even if the class is used only once?
c#;.net;unit testing;dependency injection;dependency management
In C# it is trivial to provide optional dependency injection without coupling yourself to your dependency too tightly:public class SomeOtherClass {    private readonly ISomeClass _someClass;    public SomeOtherClass(ISomeClass dependency = null) {        _someClass = dependency ?? new SomeClass();    }}(or you can make the constructors explicit if your company dislikes default params)In my experience, oh we'll never need another one is naive. Business changes. Technology changes. Make dependencies flexible. And this sort of thing strikes the right balance between usability (yes, you'll almost always use the common/default one) and flexibility (but the ctor is there if you need it) - all with a nice simple line of code, while also providing some semblance of error correcting robustness. It's so trivial and clearly beneficial, there's no reason not to do it for the simple/straight-forward case.