Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dependency Injection and Explicit Interface Implementation

Is there a benefit implementing interfaces explicitly with respect to Dependency Injection?

As far as I understand, interfaces can be implemented either explicitly or implicitly:

interface IFoo
{
    void Bar();
}

//implicit implementation
class Foo1 : IFoo
{
    public void Bar(){}
}

//explicit implementation
class Foo2 : IFoo
{
    void IFoo.Bar(){}
}

Now the explicit implementation can only be called by calling the interface method, while the implicit implementation can be called directly on an instance of the class:

class Baz
{
    void Ba()
    {
        Foo1 foo1 = new Foo1();
        foo1.Bar();

        Foo2 foo2 = new Foo2();
        foo2.Bar();    //syntax error

        IFoo foo2_explicit = new Foo2();
        foo2_explicit.Bar();
    }
}

Thus, using explicit interface implementations, one cannot accidentally call a method on a concrete class, but one has to call the interface method. Does this prevent tightly coupled code as is one purpose of DI or am I barking up the wrong tree here? After all, one cannot accidently write a constructor or method that gets a concrete class injected instead of an interface:

class Baz
{
    void Ba(Foo2 foo)
    {
        foo.Bar(); //syntax error
    }

    void Bb(IFoo foo)
    {
        foo.Bar();
    }
}
like image 347
Thaoden Avatar asked Jul 28 '15 11:07

Thaoden


People also ask

What is dependency injection and how it is implemented?

Dependency Injection (DI) is a design pattern used to implement IoC. It allows the creation of dependent objects outside of a class and provides those objects to a class through different ways. Using DI, we move the creation and binding of the dependent objects outside of the class that depends on them.

What are implicit and explicit interface implementations?

Resolving conflicts here ..... "Implicit Implementation" - means just simple implementation of a particular method having same name and same signature which belongs to the same class itself, where as "Explicit Implementation" - means implementation of a method using their Interface name having same name and signature ...

Is interface required for dependency injection?

interface injection: the dependency provides an injector method that will inject the dependency into any client passed to it. Clients must implement an interface that exposes a setter method that accepts the dependency.


2 Answers

Normally, the purpose of Dependency Injection is decoupling, which you achieve by injecting the abstraction into its client:

public class Baz
{
    private readonly IFoo foo;

    public Baz(IFoo foo)
    {
        this.foo = foo;
    }

    // Members using this.foo go here...
}

This ensures that Baz depends on IFoo, and is decoupled from any concrete implementation.

Whether or not a concrete class implements IFoo implicitly or explicitly makes no difference.

Once in a while, a class may have a Concrete Dependency, but this isn't particularly normal; and when it happens, the concrete dependency is concrete, so often will not implement an interface at all. Explicit versus implicit interface implementation is irrelevant in such cases.

like image 118
Mark Seemann Avatar answered Sep 30 '22 14:09

Mark Seemann


If your class is in container, then you use interface. So, there are no benefits.

But, if you use your class directly (in tests for example) you have to cast to access the method and it is not convenient.

Total: 0 advantages when you use class in container and bad for tests.

like image 43
omikad Avatar answered Sep 30 '22 15:09

omikad