Interface binding by default

Jun 12, 2014 at 5:26 AM
Hello there! :)

I get to use DI container that is called "StructureMap". There I can tell my container to use default binding. For example, if I have IService interface and an implementation Service. StructureMap will throw "I" letter and will use "Service" implementation.

Can I do such things using Simple Injector?

Sincerely,
Anton.
Coordinator
Jun 12, 2014 at 6:24 AM
Can you show the StructureMap configuration you are referring to?
Jun 12, 2014 at 1:26 PM
Hello, thanks for your response.

This feature is called Autoregistration. Please take a look at this article - http://anthonysteele.co.uk/ioc-comparison-autoregistration-in-structuremap

You can use something like this

x.Scan(y =>
{
y.WithDefaultConventions();
y.AssemblyContainingType(typeof(BusinessProcess));
}

Thank you!
Coordinator
Jun 12, 2014 at 1:51 PM
There's no comparable feature in Simple Injector. The reasons for this are explained here.

To do this in Simple Injector, you will have to query the type system yourself. Depending on your needs, such query might look like this:
var defaultConventionsRegistrations =
    from implementation in typeof(BusinessProcess).Assembly.GetExportedTypes()
    where !implementation.IsGenericType
    where !implementation.IsAbstract
    let serviceType = (
        from interfaceType in implementation.GetInterfaces()
        where interfaceType.Name == "I" + implementation.Name
        select interfaceType)
        .SingleOrDefault()
    where serviceType != null
    select new { serviceType, implementation };

foreach (var registration in defaultConventionsRegistrations)
{
    container.Register(registration.serviceType, registration.implementation);
}