public ServiceLocator(IContainerConfiguration containerConfiguration)
        {
            var assembliesProvider      = new AssembliesProvider(containerConfiguration);
            var compositionRootProvider = new CompositionRootProvider(assembliesProvider);
            var registry      = new Registry();
            var typesProvider = new TypesProvider();
            var objectInstantiatingGenerator            = new ObjectInstantiatingGenerator();
            var intantiationProvidingGeneratingStrategy = new InstantiationProvidingGeneratingStrategy(objectInstantiatingGenerator);
            var scopeKeeper = new ScopeKeeper();
            var perRequestProvidingGeneratingStrategy   = new PerRequestProvidingGeneratingStrategy(scopeKeeper, objectInstantiatingGenerator);
            var perContainerProvidingGeneratingStrategy = new PerContainerProvidingGeneratingStrategy(scopeKeeper, objectInstantiatingGenerator);
            var arrayInstantiatingGenerator             = new ArrayInstantiatingGenerator();
            var enumerableProvidingGeneratingStrategy   = new EnumerableProvidingGeneratingStrategy(arrayInstantiatingGenerator);
            var generatingStrategyProvider           = new GeneratingStrategyProvider(intantiationProvidingGeneratingStrategy, perRequestProvidingGeneratingStrategy, perContainerProvidingGeneratingStrategy, enumerableProvidingGeneratingStrategy);
            var objectGraphProvider                  = new ObjectGraphProvider(registry, generatingStrategyProvider);
            var instanceProvidingMethodBodyGenerator = new InstanceProvidingMethodBodyGenerator();
            var instanceProvidingMethodGenerator     = new InstanceProvidingMethodGenerator(objectGraphProvider, instanceProvidingMethodBodyGenerator);
            var registrationFactory                  = new RegistrationFactory(typesProvider, instanceProvidingMethodGenerator);
            var assemblyBoundedRegistrator           = new AssemblyBoundedRegistrator(registry, registrationFactory);
            var registratorProvider                  = new RegistratorProvider(assemblyBoundedRegistrator);
            var dependenciesCompositor               = new DependenciesCompositor(compositionRootProvider, registratorProvider);
            var registrySaturator   = new RegistrySaturator(registry, scopeKeeper);
            var registryInitializer = new RegistryInitializer(dependenciesCompositor, registrySaturator);

            _map.Add(typeof(IRegistryInitializer), registryInitializer);
            _map.Add(typeof(IScopeKeeper), scopeKeeper);
            var resolver = new Resolver(registry, scopeKeeper);

            _map.Add(typeof(IResolver), resolver);
        }
Exemple #2
0
        private IEnumerable <Type> GetAutoMapperProfileTypes()
        {
            ReadOnlyCollection <Type> types =
                AssembliesProvider
                .GetAssemblies()
                .SelectMany(assembly => assembly.ExportedTypes)
                .Where(type => AutoMapperProfileBaseType.IsAssignableFrom(type))
                .ToList()
                .AsReadOnly();

            return(types);
        }
        public static void ConfigureAutofac(this HttpConfiguration httpConfiguration)
        {
            // TODO : later implement plugin module...
            Assembly[] aztAssemblies = AssembliesProvider.GetAssemblies();

            // Hack... plugin mode solve this problem
            var assemblyNames   = Assembly.GetExecutingAssembly().GetReferencedAssemblies();
            var assembliesTypes = assemblyNames
                                  .Where(a => a.FullName.Contains("Azure.TestProject"))
                                  .SelectMany(an => Assembly.Load(an).GetTypes())
                                  .Where(p => typeof(Profile).IsAssignableFrom(p) && p.IsPublic && !p.IsAbstract)
                                  .Distinct();

            var autoMapperProfiles = assembliesTypes.Select(p => (Profile)Activator.CreateInstance(p)).ToList();

            ApplicationKindProvider.Initialize(ApplicationKind.AspNetWebApi2);

            var builder = new ContainerBuilder();

            builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
            builder.RegisterWebApiFilterProvider(httpConfiguration);
            //builder.RegisterAssemblyModules(aztAssemblies);

            builder.Register(ctx => new MapperConfiguration(cfg =>
            {
                foreach (var profile in autoMapperProfiles)
                {
                    cfg.AddProfile(profile);
                }
            }));
            builder.Register(ctx => ctx.Resolve <MapperConfiguration>().CreateMapper()).As <IMapper>().InstancePerLifetimeScope();


            builder.RegisterModule <RepositoryDependencyModule>();
            builder.RegisterModule <ServiceDependencyModule>();
            //builder.RegisterModule<AutoMapperDependencyModule>();
            builder.RegisterModule <DataContextDependencyModule>();


            IContainer container = builder.Build();

            GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container);
        }