Example #1
0
        protected virtual void RegisterDependencies(LConfig config)
        {
            var builder   = new ContainerBuilder();
            var container = builder.Build();

            //dependencies
            var typeFinder = new WebAppTypeFinder(config);

            builder = new ContainerBuilder();
            builder.RegisterInstance(config).As <LConfig>().SingleInstance();
            builder.RegisterType <LocalizationService>().As <ILocalizationService>().SingleInstance();
            builder.RegisterType <SettingService>().As <ISettingService>().SingleInstance();
            builder.RegisterGeneric(typeof(Repository <>)).As(typeof(IRepository <>)).InstancePerLifetimeScope();
            builder.RegisterType <RepositoryContext>().As <IRepositoryContext>().InstancePerLifetimeScope();

            //web helper
            builder.RegisterType <WebHelper>().As <IWebHelper>().InstancePerLifetimeScope();

            //plugins
            builder.RegisterType <PluginFinder>().As <IPluginFinder>().InstancePerLifetimeScope();

            //cache manager
            builder.RegisterType <MemoryCacheManager>().As <ICacheManager>().Named <ICacheManager>("lcl_cache_static").SingleInstance();
            builder.RegisterType <PerRequestCacheManager>().As <ICacheManager>().Named <ICacheManager>("lcl_cache_per_request").InstancePerLifetimeScope();

            //event bus
            builder.RegisterType <EventBus>().As <IEventBus>().InstancePerLifetimeScope();

            builder.RegisterType <EventAggregator>().As <IEventAggregator>().InstancePerLifetimeScope();
            builder.RegisterType <DomainEvent>().As <IDomainEvent>().InstancePerLifetimeScope();



            builder.RegisterInstance(this).As <IEngine>().SingleInstance();
            builder.RegisterInstance(typeFinder).As <ITypeFinder>().SingleInstance();
            builder.Update(container);

            //register dependencies provided by other assemblies
            builder = new ContainerBuilder();
            var drTypes     = typeFinder.FindClassesOfType <IDependencyRegistrar>();
            var drInstances = new List <IDependencyRegistrar>();

            foreach (var drType in drTypes)
            {
                drInstances.Add((IDependencyRegistrar)Activator.CreateInstance(drType));
            }
            //sort
            drInstances = drInstances.AsQueryable().OrderBy(t => t.Order).ToList();
            foreach (var dependencyRegistrar in drInstances)
            {
                dependencyRegistrar.Register(builder, typeFinder);
            }
            builder.Update(container);

            this._containerManager = new ContainerManager(container);

            //set dependency resolver
            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
        }
Example #2
0
        protected virtual void RegisterDependencies(LConfig config)
        {
            var builder   = new ContainerBuilder();
            var container = builder.Build();

            //dependencies
            var typeFinder = new WebAppTypeFinder(config);

            builder = new ContainerBuilder();
            builder.RegisterInstance(config).As <LConfig>().SingleInstance();
            builder.RegisterInstance(this).As <IEngine>().SingleInstance();
            builder.RegisterInstance(typeFinder).As <ITypeFinder>().SingleInstance();

            // DDD
            builder.RegisterGeneric(typeof(Repository <>)).As(typeof(IRepository <>)).InstancePerLifetimeScope();
            builder.RegisterGeneric(typeof(Repository <,>)).As(typeof(IRepository <,>)).InstancePerLifetimeScope();
            builder.RegisterType <RepositoryContext>().As <IRepositoryContext>().InstancePerLifetimeScope();

            builder.RegisterType <EventBus>().As <IEventBus>().InstancePerLifetimeScope();
            builder.RegisterType <EventAggregator>().As <IEventAggregator>().InstancePerLifetimeScope();
            builder.RegisterType <DomainEvent>().As <IDomainEvent>().InstancePerLifetimeScope();

            //plugins
            builder.RegisterType <PluginFinder>().As <IPluginFinder>().InstancePerLifetimeScope();

            //cache manager
            builder.RegisterType <MemoryCacheProvider>().As <ICacheProvider>().Named <ICacheProvider>("lcl_cache_static").SingleInstance();
            //config
            builder.RegisterType <DictionaryBasedConfig>().As <IDictionaryBasedConfig>().InstancePerLifetimeScope();
            builder.RegisterType <ModuleConfigurations>().As <IModuleConfigurations>().InstancePerLifetimeScope();
            builder.RegisterType <LclStartupConfiguration>().As <ILclStartupConfiguration>().InstancePerLifetimeScope();

            builder.RegisterType <LocalizationService>().As <ILocalizationService>().SingleInstance();
            builder.RegisterType <SettingService>().As <ISettingService>().SingleInstance();
            builder.RegisterType <SequentialGuidGenerator>().As <IGuidGenerator>().InstancePerLifetimeScope();


            builder.RegisterAssemblyTypes(AppDomain.CurrentDomain.GetAssemblies())
            .Where(t => t.GetCustomAttribute <DependencyRegisterAttribute>() != null)
            .AsImplementedInterfaces()
            .InstancePerRequest();

            builder.Update(container);

            //register dependencies provided by other assemblies
            builder = new ContainerBuilder();
            var drTypes     = typeFinder.FindClassesOfType <IDependencyRegistrar>();
            var drInstances = new List <IDependencyRegistrar>();

            foreach (var drType in drTypes)
            {
                drInstances.Add((IDependencyRegistrar)Activator.CreateInstance(drType));
            }
            //sort
            drInstances = drInstances.AsQueryable().OrderBy(t => t.Order).ToList();
            foreach (var dependencyRegistrar in drInstances)
            {
                dependencyRegistrar.Register(builder, typeFinder);
            }
            builder.Update(container);

            this._containerManager = new ContainerManager(container);
        }