caloggins
12/12/2013 - 9:19 PM

ContainerFactory

ContainerFactory

namespace NAMESPACE
{
    using AutoMapper;

    using Castle.Core.Internal;
    using Castle.Facilities.Logging;
    using Castle.Facilities.TypedFactory;
    using Castle.MicroKernel.Registration;
    using Castle.Windsor;
    using Castle.Windsor.Installer;

    public static class ContainerFactory
    {
        public static IWindsorContainer Create()
        {
            var container = new WindsorContainer()
                .AddFacility<LoggingFacility>(facility => facility.LogUsing(LoggerImplementation.NLog))
                .AddFacility<TypedFactoryFacility>()
                .Install(Configuration.FromAppConfig())
                .Install(FromAssembly.Named("x"))
                .Install(FromAssembly.Named("x.y"));

            container.Kernel.Resolver.AddSubResolver(new AppSettingsConvention());

            RegisterAutoMapperProfiles(container);

            return container;
        }

        private static void RegisterAutoMapperProfiles(IWindsorContainer container)
        {
            container.Register(Component.For<IMappingEngine>().UsingFactoryMethod(() => Mapper.Engine).LifestyleSingleton());

            var list = container.ResolveAll<Profile>();

            Mapper.Initialize(configuration => list.ForEach(configuration.AddProfile));
        }
    }
}
namespace NAMESPACE
{
    using Castle.Facilities.Logging;
    using Castle.MicroKernel.Registration;
    using Castle.Windsor;
    using Castle.Windsor.Installer;

    public static class ContainerFactory
    {
        public static IWindsorContainer Create()
        {
            var container = new WindsorContainer()
                .AddFacility<LoggingFacility>(facility => facility.UseNLog())
                .AddFacility<ConnectionStringFacility>()
                .Install(
                    Configuration.FromAppConfig(),
                    FromAssembly.InDirectory(new AssemblyFilter(""))
                );

            return container;
        }
    }
}