.NET6 Dependency Injection — One Interface, Multiple Implementations

Bernardo Teixeira
Geek Culture
Published in
3 min readNov 28, 2021

--

You want to have multiple implementations with the same interface. In this article, I will show you how.

Photo by AltumCode on Unsplash

Multiple implementations with generic

I have the interface IMovivement with a method string Walk();

public interface IMoviment<T> where T : class{string Walk();}

One friendly approach could be using an Enum for the generic T. But you cant do it. Language limitation, but probably in the future, C# allows you to do it.

public interface IMoviment<T> where T : MovimentEnum{string Walk();}

This interface has three implementations, Cat, Dog, and Human class.

public class Dog : IMoviment<Dog>{public string Walk(){return “Im a Dog, walking!”;}}public class Cat : IMoviment<Cat>{public string Walk(){

--

--