Is it bad practice or code smell to use an IoC container when installing dependencies?

Is it bad practice or code smell to use an IoC container when installing dependencies?

This is my root composition:

public void Install(IWindsorContainer container, IConfigurationStore store)
{
    Assembly modelAssembly = typeof(UserLoginModel).Assembly;
    Assembly controllerAssembly = typeof(HomeController).Assembly;

    container.Install(
        new MvcInfrastructureInstaller(modelAssembly, viewAssembly, controllerAssembly, applicationTitle, resourceAssemblyLocations),
        new MiniMembershipInstaller(),
        new ServiceInstaller(),
        new RepositoryInstaller(),
        new LibraryInstaller(),
        new AutoMapperProfileInstaller() // this installer needs to resolve dependencies such as repositories through the container itself, so it goes last.
    );
}

My AutoMapperProfileInstallerneed to allow a profile containing dependencies to initialize mapper

public class AutoMapperProfileInstaller : IWindsorInstaller
{
    public void Install(IWindsorContainer container, IConfigurationStore store)
    {
        Profile entityToViewModel = container.Resolve<EntityToViewModelProfile>();
        Profile[] profiles = new[] { entityToViewModel };

        Mapper.Initialize(config =>
        {
            config.ConstructServicesUsing(container.Resolve);

            foreach (Profile profile in profiles)
            {
                config.AddProfile(profile);
            }
        });
    }
}

This is wrong at many levels, what would be the best way to initialize profiles AutoMapper?

+5
source share
1 answer

The only thing wrong with this approach is that you manually specify the implementations IWindowsInstaller. Use reflection to find them, and Activator.CreateInstanceto create implementations.

This gives you a flexible approach to tuning, where each part / module in your system is responsible for its own registrations.

AutoMapper IMapperConfiguration (Single Responsibility). .

0

All Articles