This project is read-only.

Quick conversion method from Windsor Installer to simple container

Jul 2, 2013 at 3:35 PM
Hi,
is there a quick correspondent form for the following installing method below for simple injector?

Many thanks for your help.
   public void Install(IWindsorContainer container, IConfigurationStore store)
        {
            container.Register(
                Classes.FromAssemblyNamed("Domain.Data.Dapper")
                    .Where(type => type.Name.EndsWith("Services"))
                    .WithService.DefaultInterfaces()
                    .LifestyleTransient());

        }_
Jul 2, 2013 at 7:10 PM
Edited Jul 2, 2013 at 8:26 PM
An IPackage is the equivalent of a Windsor IWindsorInstaller interface. This interface and extension methods are located in the SimpleInjector.Packaging NuGet package. With Simple Injector there is no fluent API, and this is deliberate.

The following code would be the equivalent of your example:
public void RegisterServices(Container container)
{
    var assembly = typeof(Domain.Data.Dapper.SomeType).Assembly;
    
    var registrations =
        from implementation in assembly.GetExportedTypes()
        where implementation.Name.EndsWith("Services")
        from abstraction in implementation.GetInterfaces()
        where implementation.Name.Contains(abstraction.Name.SubString(1))
        select { abstraction, implementation };
    
    foreach (var reg in registrations)
    {
        container.Register(reg.abstraction, reg.implementation, 
            Lifestyle.Transient);
    }
}
Note that the DefaultInterfaces smells of an over-abundance of 1:1 interfaces. Instead, if you place all 'services' behind the same generic abstraction, registration becomes much easier:
container.RegisterManyForOpenGeneric(typeof(IRepository<>),
    this.GetType().Assembly);
But besides that, adding cross-cutting concerns becomes much easier as well, since you can define a single generic decorator that wraps all implementations of that generic interface:
container.RegisterDecorator(typeof(IRepository<>),
    typeof(LoggingRepositoryDecorator<>);
If you haven't already I can recommend the following articles:
Marked as answer by dot_NET_Junkie on 2/26/2014 at 1:38 PM
Jul 4, 2013 at 11:14 AM
Great it works perfectly.
Thank you so much.