public RedisCacheManager(IHttpContextAccessor httpContextAccessor,
                                 IRedisConnectionWrapper connectionWrapper,
                                 BopConfig config)
        {
            if (string.IsNullOrEmpty(config.RedisConnectionString))
            {
                throw new Exception("Redis connection string is empty");
            }

            _config = config;

            // ConnectionMultiplexer.Connect should only be called once and shared between callers
            _connectionWrapper = connectionWrapper;

            _db = _connectionWrapper.GetDatabase(config.RedisDatabaseId ?? (int)RedisDatabaseNumber.Cache);

            _perRequestCache = new PerRequestCache(httpContextAccessor);
        }
Пример #2
0
        /// <summary>
        /// Register dependencies
        /// </summary>
        /// <param name="services">Collection of service descriptors</param>
        /// <param name="typeFinder">Type finder</param>
        /// <param name="bopConfig">Bop configuration parameters</param>
        protected virtual IServiceProvider RegisterDependencies(IServiceCollection services, ITypeFinder typeFinder, BopConfig bopConfig)
        {
            var containerBuilder = new ContainerBuilder();

            //register engine
            containerBuilder.RegisterInstance(this).As <IEngine>().SingleInstance();

            //register type finder
            containerBuilder.RegisterInstance(typeFinder).As <ITypeFinder>().SingleInstance();

            //populate Autofac container builder with the set of registered service descriptors
            containerBuilder.Populate(services);

            //find dependency registrars provided by other assemblies
            var dependencyRegistrars = typeFinder.FindClassesOfType <IDependencyRegistrar>();

            //create and sort instances of dependency registrars
            var instances = dependencyRegistrars
                            .Select(dependencyRegistrar => (IDependencyRegistrar)Activator.CreateInstance(dependencyRegistrar))
                            .OrderBy(dependencyRegistrar => dependencyRegistrar.Order);

            //register all provided dependencies
            foreach (var dependencyRegistrar in instances)
            {
                dependencyRegistrar.Register(containerBuilder, typeFinder, bopConfig);
            }

            //create service provider
            _serviceProvider = new AutofacServiceProvider(containerBuilder.Build());

            return(_serviceProvider);
        }
Пример #3
0
        /// <summary>
        /// Add and configure services
        /// </summary>
        /// <param name="services">Collection of service descriptors</param>
        /// <param name="configuration">Configuration of the application</param>
        /// <param name="bopConfig">Bop configuration parameters</param>
        /// <returns>Service provider</returns>
        public IServiceProvider ConfigureServices(IServiceCollection services, IConfiguration configuration, BopConfig bopConfig)
        {
            //find startup configurations provided by other assemblies
            var typeFinder            = new WebAppTypeFinder();
            var startupConfigurations = typeFinder.FindClassesOfType <IBopStartup>();

            //create and sort instances of startup configurations
            var instances = startupConfigurations
                            .Select(startup => (IBopStartup)Activator.CreateInstance(startup))
                            .OrderBy(startup => startup.Order);

            //configure services
            foreach (var instance in instances)
            {
                instance.ConfigureServices(services, configuration);
            }

            //register mapper configurations
            AddAutoMapper(services, typeFinder);

            //register dependencies
            RegisterDependencies(services, typeFinder, bopConfig);


            //run startup tasks
            RunStartupTasks(typeFinder);


            return(_serviceProvider);
        }
Пример #4
0
 /// <summary>
 /// Register services and interfaces
 /// </summary>
 /// <param name="builder">Container builder</param>
 /// <param name="typeFinder">Type finder</param>
 /// <param name="config">Config</param>
 public void Register(ContainerBuilder builder, ITypeFinder typeFinder, BopConfig config)
 {
     //factories
     builder.RegisterType <CustomerModelFactory>().As <ICustomerModelFactory>().InstancePerLifetimeScope();
     builder.RegisterType <Factories.CustomerModelFactory>().As <Factories.ICustomerModelFactory>().InstancePerLifetimeScope();
 }
 public RedisConnectionWrapper(BopConfig config)
 {
     _config           = config;
     _connectionString = new Lazy <string>(GetConnectionString);
     _redisLockFactory = CreateRedisLockFactory();
 }
        /// <summary>
        /// Register services and interfaces
        /// </summary>
        /// <param name="builder">Container builder</param>
        /// <param name="typeFinder">Type finder</param>
        /// <param name="config">Config</param>
        public virtual void Register(ContainerBuilder builder, ITypeFinder typeFinder, BopConfig config)
        {
            //file provider
            builder.RegisterType <BopFileProvider>().As <IBopFileProvider>().InstancePerLifetimeScope();

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

            //data layer
            builder.RegisterType <DataProviderManager>().As <IDataProviderManager>().InstancePerDependency();
            builder.Register(context => context.Resolve <IDataProviderManager>().DataProvider).As <IBopDataProvider>().InstancePerDependency();

            //repositories
            builder.RegisterGeneric(typeof(EntityRepository <>)).As(typeof(IRepository <>)).InstancePerLifetimeScope();


            //redis connection wrapper
            if (config.RedisEnabled)
            {
                builder.RegisterType <RedisConnectionWrapper>()
                .As <ILocker>()
                .As <IRedisConnectionWrapper>()
                .SingleInstance();
            }

            //static cache manager
            if (config.RedisEnabled && config.UseRedisForCaching)
            {
                builder.RegisterType <RedisCacheManager>().As <IStaticCacheManager>().InstancePerLifetimeScope();
            }
            else
            {
                builder.RegisterType <MemoryCacheManager>()
                .As <ILocker>()
                .As <IStaticCacheManager>()
                .SingleInstance();
            }

            //work context
            builder.RegisterType <WebWorkContext>().As <IWorkContext>().InstancePerLifetimeScope();

            //store context
            builder.RegisterType <HostedSiteContext>().As <IHostedSiteContext>().InstancePerLifetimeScope();


            //services
            builder.RegisterType <CustomerService>().As <ICustomerService>().InstancePerLifetimeScope();
            builder.RegisterType <PermissionService>().As <IPermissionService>().InstancePerLifetimeScope();
            builder.RegisterType <LocalizationService>().As <ILocalizationService>().InstancePerLifetimeScope();
            builder.RegisterType <LocalizedEntityService>().As <ILocalizedEntityService>().InstancePerLifetimeScope();
            builder.RegisterType <LanguageService>().As <ILanguageService>().InstancePerLifetimeScope();
            builder.RegisterType <EncryptionService>().As <IEncryptionService>().InstancePerLifetimeScope();
            builder.RegisterType <JwtAuthenticationService>().As <IAuthenticationService>().InstancePerLifetimeScope();
            builder.RegisterType <DefaultLogger>().As <ILogger>().InstancePerLifetimeScope();
            builder.RegisterType <EventPublisher>().As <IEventPublisher>().SingleInstance();
            builder.RegisterType <SettingService>().As <ISettingService>().InstancePerLifetimeScope();
            builder.RegisterType <HostedSiteService>().As <IHostedSiteService>().InstancePerLifetimeScope();
            builder.RegisterType <ScheduleTaskService>().As <IScheduleTaskService>().InstancePerLifetimeScope();
            builder.RegisterType <CustomerRegistrationService>().As <ICustomerRegistrationService>().InstancePerLifetimeScope();
            builder.RegisterType <GenericAttributeService>().As <IGenericAttributeService>().InstancePerLifetimeScope();
            builder.RegisterType <WorkflowMessageService>().As <IWorkflowMessageService>().InstancePerLifetimeScope();
            builder.RegisterType <TokenStoreService>().As <ITokenStoreService>().InstancePerLifetimeScope();
            builder.RegisterType <TokenValidatorService>().As <ITokenValidatorService>().InstancePerLifetimeScope();
            builder.RegisterType <TokenFactoryService>().As <ITokenFactoryService>().InstancePerLifetimeScope();
            builder.RegisterType <RoutePublisher>().As <IRoutePublisher>().SingleInstance();
            builder.RegisterType <CacheKeyService>().As <ICacheKeyService>().InstancePerLifetimeScope();

            builder.RegisterType <ActionContextAccessor>().As <IActionContextAccessor>().InstancePerLifetimeScope();

            //register all settings
            builder.RegisterSource(new SettingsSource());

            //installation service
            if (!DataSettingsManager.DatabaseIsInstalled)
            {
                builder.RegisterType <CodeFirstInstallationService>().As <IInstallationService>().InstancePerLifetimeScope();
            }


            //event consumers
            var consumers = typeFinder.FindClassesOfType(typeof(IConsumer <>)).ToList();

            foreach (var consumer in consumers)
            {
                builder.RegisterType(consumer)
                .As(consumer.FindInterfaces((type, criteria) =>
                {
                    var isMatch = type.IsGenericType && ((Type)criteria).IsAssignableFrom(type.GetGenericTypeDefinition());
                    return(isMatch);
                }, typeof(IConsumer <>)))
                .InstancePerLifetimeScope();
            }
        }