/// <summary>
        /// Adds the dendency injection.
        /// </summary>
        /// <param name="services">The services.</param>
        /// <param name="systemSettings">The systemSettings.</param>
        /// <param name="cacheDecoratorSettings">The cacheDecoratorSettings.</param>

        /// <returns>IServiceCollection.</returns>
        public static IServiceCollection AddDendencyInjection(this IServiceCollection services,
                                                              SystemSettings systemSettings,
                                                              CacheDecoratorSettings cacheDecoratorSettings
                                                              )
        {
            // SystemSettings
            services.AddSingleton(x => systemSettings);

            // ISystemSettingsProvider
            services.AddSingleton <ISystemSettingsProvider>(x => new SystemSettingsProvider(systemSettings));

            // ICacheDecoratorSettingsProvider
            services.AddSingleton <ICacheDecoratorSettingsProvider>(x => new CacheDecoratorSettingsProvider(cacheDecoratorSettings));

            // MemoryCache
            services.AddSingleton <IMemoryCache>(x => new MemoryCache(new MemoryCacheOptions()));

            // CacheProviderResolver
            services.AddSingleton <ICacheProviderResolver, CacheProviderResolver>();

            // IHttpContextAccessor
            services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>();

            // Repositories
            services.AddScoped <IFooRepository, FooRepository>()
            .Decorate <IFooRepository, RedisFooRepository>()
            .Decorate <IFooRepository, CachedFooRepository>();

            // Services
            services.AddScoped <IFooService, FooService>();

            return(services);
        }
Beispiel #2
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();

            // database connection - Wldo
            this.WLDOonnection = this.Configuration.GetConnectionString("WLDO");

            // database connection - Mysql
            this.MySQLConnection = this.Configuration.GetConnectionString("MySQL");

            //DB
            services.AddScoped <IDatabaseHelper>(x => new DatabaseHelper(WLDOonnection, MySQLConnection));

            //Dendency Injection

            // 系統設定
            var systemSettings = new SystemSettings();

            this.Configuration.GetSection("SystemSettings").Bind(systemSettings);
            this.SystemSettings = systemSettings;

            // 快取裝飾者設定
            var cacheDecoratorSettings = new CacheDecoratorSettings();

            this.Configuration.GetSection("CacheDecoratorSettings").Bind(cacheDecoratorSettings);
            this.CacheDecoratorSettings = cacheDecoratorSettings;

            services.AddMemoryCache();

            services.AddStackExchangeRedisCache(options =>
            {
                options.Configuration = "127.0.0.1:6379";
                options.InstanceName  = "CacheDecorator_Sample";
            });

            services.AddAutoMapper
            (
                typeof(ServiceMappingProfile).Assembly,
                typeof(WebApplicationMappingProfile).Assembly
            );

            services.Configure <CookiePolicyOptions>(options =>
            {
                // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                options.CheckConsentNeeded    = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });

            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

            // DendencyInjection
            services.AddDendencyInjection(this.SystemSettings, this.CacheDecoratorSettings);
        }