Exemplo n.º 1
0
        public static void AddEntityFrameworkPersistence(this IServiceCollection services,
                                                         PersistenceOptions persistenceOptions, bool isDevelopment)
        {
            services.AddScoped <IPersistenceSeeder, EntityFrameworkSeeder>();
            var assembly = typeof(GenericEntityFrameworkRepository <,>).Assembly;
            var types    = assembly.GetTypes()
                           .Where(x => !x.IsAbstract && x.IsClass && x.Name.Contains("EntityFrameworkRepository"));

            foreach (var type in types)
            {
                services.AddScoped(type);
                var interfaces = type.GetInterfaces().Where(x => x.IsInterface);
                foreach (var inter in interfaces)
                {
                    services.AddScoped(inter, type);
                }
            }

            services.AddDbContext <ProductsContext>(x =>
                                                    x.ConfigureDefaultOptions(persistenceOptions.EntityFrameworkOptions.ProductsInfo, isDevelopment,
                                                                              GetMigrationTableName <ProductsContext>()));

            services.AddDbContext <UsersContext>(x => x.ConfigureDefaultOptions(
                                                     persistenceOptions.EntityFrameworkOptions.UsersInfo, isDevelopment,
                                                     GetMigrationTableName <UsersContext>()));

            services.AddDbContext <BlogsContext>(x => x.ConfigureDefaultOptions(
                                                     persistenceOptions.EntityFrameworkOptions.BlogsInfo, isDevelopment,
                                                     GetMigrationTableName <BlogsContext>()));
        }
Exemplo n.º 2
0
        public void ConfigureServices(IServiceCollection services)
        {
            var persistenceOptions = new PersistenceOptions();

            _configuration.GetSection(PersistenceOptions.Section).Bind(persistenceOptions);
            var externalOptions = new ExternalLoginProviderOptions();

            _configuration.GetSection(ExternalLoginProviderOptions.Section).Bind(externalOptions);
            services
            .AddControllersWithViews()
            .Services
            .AddHttpContextAccessor()
            .AddScoped <ICartService, CookiesCartService>()
            .AddAutoMapperWithMyProfiles("WebStore.EcommerceApp", "WebStore.Application")
            .AddCookieIdentity()
            .AddExternalProviders(externalOptions)
            .AddScoped <ISieveProcessor, CustomSieveProcessor>()
            .AddScoped <ISieveCustomFilterMethods, MyCustomSieveMethods>()
            .RegisterPersistence(persistenceOptions, _environmen)
            .AddScoped(typeof(IPipelineBehavior <,>), typeof(ValidationBehaviour <,>))
            .AddFluentValidation(options =>
            {
                options.RegisterValidatorsFromAssemblyContaining(typeof(FeaturedResponse <>),
                                                                 lifetime: ServiceLifetime.Scoped);
                options.AutomaticValidationEnabled          = false;
                options.LocalizationEnabled                 = false;
                options.ImplicitlyValidateChildProperties   = true;
                options.DisableDataAnnotationsValidation    = true;
                ValidatorOptions.Global.DisplayNameResolver = (type, member, expression) => member?.Name;
            })
            .AddMediatR(options => options.AsScoped(), Assembly.Load("WebStore.Application"));
        }
        public static PersistenceOptions UseMysqlEntityFramework <TContext>
            (this PersistenceOptions options, IServiceCollection services, string connectionString)
            where TContext : DbContext
        {
            MysqlOptions.DbContextType = typeof(TContext);

            services.AddDbContext <TContext>(options =>
            {
                options.UseMySQL(connectionString);
            });
            services.AddScoped(typeof(IRepository <>), typeof(MysqlEfRepository <>));
            services.AddScoped <IUnitOfWork, MysqlEfUnitOfWork>();

            return(options);
        }
Exemplo n.º 4
0
        public static IPersistenceBuilder <TContext> AddPersistence <TContext>(
            this IServiceCollection services,
            Func <TContext> contextFactory,
            PersistenceOptions options)
            where TContext : DbContext
        {
            if (services.IsContextServiceAdded <TContext>())
            {
                throw new InvalidOperationException("Persistence services already added");
            }

            if (contextFactory == null)
            {
                throw new ArgumentNullException(nameof(contextFactory));
            }

            services.AddContextServices <TContext>(options, contextFactory)
            .AddUnitOfWork <TContext>(options);

            return(new PersistenceBuilder <TContext>(services));
        }
Exemplo n.º 5
0
        private static IServiceCollection AddUnitOfWork <TContext>(
            this IServiceCollection services,
            PersistenceOptions options)
            where TContext : DbContext
        {
            if (options == null)
            {
                return(services.AddTransient <IUnitOfWork, TransactionScopeUnitOfWork <TContext> >());
            }

            switch (options.UnitOfWorkTransactionType)
            {
            case TransactionType.TransactionScope:
                return(services.AddTransient <IUnitOfWork, TransactionScopeUnitOfWork <TContext> >());

            case TransactionType.DbContextTransaction:
                return(services.AddTransient <IUnitOfWork, DbContextTransactionUnitOfWork <TContext> >());

            default:
                throw new ArgumentOutOfRangeException(nameof(options.UnitOfWorkTransactionType));
            }
        }
Exemplo n.º 6
0
        private static IServiceCollection AddContextServices <TContext>(
            this IServiceCollection services,
            PersistenceOptions options,
            Func <TContext> contextFactory)
            where TContext : DbContext
        {
            services.AddSingleton <IContextFactory <TContext> >(s => new ContextFactory <TContext>(contextFactory))
            .AddTransient <IContextFlushService <TContext>, ContextFlushService <TContext> >();

            if (options == null)
            {
                return(services.AddTransient <IContextService <TContext>, ContextService <TContext> >()
                       .AddTransient <IFlushService <TContext>, ContextFlushService <TContext> >());
            }

            if (options.UseContextPool)
            {
                services.AddSingleton <IPooledObjectPolicy <TContext>, ContextPolicy <TContext> >()
                .AddSingleton <IContextPool <TContext>, ContextPool <TContext> >()
                .AddTransient <IContextService <TContext>, PooledContextService <TContext> >();
            }
            else
            {
                services.AddTransient <IContextService <TContext>, ContextService <TContext> >();
            }

            if (options.DisableAutomaticRepositoryFlush)
            {
                services.AddTransient <IFlushService <TContext>, NoFlushService <TContext> >();
            }
            else
            {
                services.AddTransient <IFlushService <TContext>, ContextFlushService <TContext> >();
            }

            return(services);
        }
Exemplo n.º 7
0
        public static IServiceCollection RegisterPersistence(this IServiceCollection services,
                                                             PersistenceOptions persistenceOptions, IHostEnvironment environment)
        {
            services.AddTransient <ITestProductsData, TestProductsData>();
            services.AddTransient <IUsersGenerator, UserGenerator>();
            services.AddTransient <ITestBlogsData, TestBlogsData>();

            if (persistenceOptions.SeedDatabase)
            {
                services.AddHostedService <SeederHostedService>();
            }

            switch (persistenceOptions.Repository)
            {
            case RepositoryType.EntityFramework:
                services.AddEntityFrameworkPersistence(persistenceOptions, environment.IsDevelopment());
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(persistenceOptions));
            }

            return(services);
        }
 public UserDataContext(DbContextOptions options, IOptions <PersistenceOptions> config) : base(options)
 {
     _config = config.Value;
 }
        public static void AddPersistence(this IServiceCollection services, Action <PersistenceOptions> setupAction)
        {
            var options = new PersistenceOptions();

            setupAction(options);
        }