Dependency Injection: Turtles all the way down?

后端 未结 5 872
执笔经年
执笔经年 2020-12-01 18:11

So I\'m wondering about how unit testing works in regards to dealing external dependencies. Here and elsewhere I\'ve become familiar with dependency injection, and how that

5条回答
  •  余生分开走
    2020-12-01 19:05

    The examples you provide do not use Dependency Injection. Instead, Bar should use Constructor Injection to get a Foo instance, but there's no point in injecting a concrete class. Instead, you should extract an interface from Foo (let's call it IFoo) and inject that into Bar:

    public class Bar
    {
        private IFoo f;
    
        public Bar(IFoo f)
        {
            this.f = f;
        }
    
        public int doSomethingWithFoo
        {
            int x = this.f.doSomethingWithExternalDependency();
            // Do some more stuff ...
            return result;
        }
    }
    

    This enables you to always decouple consumers and dependencies.

    Yes, there will still be a place where you must compose the entire application's object graph. We call this place the Composition Root. It's a application infrastructure component, so you don't need to unit test it.

    In most cases you should consider using a DI Container for that part, and then apply the Register Resolve Release pattern.

提交回复
热议问题