LastTribunal
LastTribunal

Reputation: 1

Resolve IoC interface to class with same name implicitily

Given a set of interfaces where for each interface there is a corresponding implementing class with the same name (e.g.: ISomeThing : SomeThing), is there a way to automatically resolve all without creating explicit mappings?

There is probably an IoC container that has this already..

Upvotes: 0

Views: 58

Answers (1)

fatherOfWine
fatherOfWine

Reputation: 1251

This is default convention for StructureMap. It tries to connect concrete classes to interfaces using the I[Something]/[Something] naming convention.

    public interface ISpaceship { }

    public class Spaceship : ISpaceship { }

    public interface IRocket { }

    public class Rocket : IRocket { }

    [Fact]
    public void default_scanning_in_action()
    {
       var container = new Container(_ =>
       {
           _.Scan(x =>
           {
            x.Assembly("<AssemblyNameWhereClassesAreDefined>");
            x.WithDefaultConventions();
           });
       });

       var spacesfip = container.GetInstance<ISpaceship>().ShouldBeOfType<Spaceship>();
       var rocket = container.GetInstance<IRocket>().ShouldBeOfType<Rocket>();
    }

Upvotes: 1

Related Questions