コード例 #1
0
        public static IServiceCollection AddAbpDbContext <TDbContext>(
            this IServiceCollection services,
            Action <IAbpDbContextRegistrationOptionsBuilder> optionsBuilder = null)
            where TDbContext : AbpDbContext <TDbContext>
        {
            services.AddMemoryCache();

            var options = new AbpDbContextRegistrationOptions(typeof(TDbContext), services);

            optionsBuilder?.Invoke(options);

            services.TryAddTransient(DbContextOptionsFactory.Create <TDbContext>);

            foreach (var dbContextType in options.ReplacedDbContextTypes)
            {
                services.Replace(
                    ServiceDescriptor.Transient(
                        dbContextType,
                        sp => sp.GetRequiredService(typeof(TDbContext))
                        )
                    );

                services.Configure <AbpDbContextOptions>(opts =>
                {
                    opts.DbContextReplacements[dbContextType] = typeof(TDbContext);
                });
            }

            new EfCoreRepositoryRegistrar(options).AddRepositories();

            return(services);
        }
コード例 #2
0
        public static IServiceCollection AddAbpDbContext <TDbContext>(
            this IServiceCollection services,
            Action <IAbpDbContextRegistrationOptionsBuilder> optionsBuilder = null)
            where TDbContext : AbpDbContext <TDbContext>
        {
            services.AddMemoryCache();

            var options = new AbpDbContextRegistrationOptions(typeof(TDbContext));

            optionsBuilder?.Invoke(options);

            services.TryAddTransient <TDbContext>();
            services.TryAddTransient(DbContextOptionsFactory.Create <TDbContext>);

            if (options.DefaultRepositoryDbContextType != typeof(TDbContext))
            {
                services.TryAddTransient(options.DefaultRepositoryDbContextType, typeof(TDbContext));
            }

            foreach (var dbContextType in options.ReplacedDbContextTypes)
            {
                services.Replace(ServiceDescriptor.Transient(dbContextType, typeof(TDbContext)));
            }

            new EfCoreRepositoryRegistrar(options)
            .AddRepositories(services);

            return(services);
        }
コード例 #3
0
        public static IServiceCollection AddAbpDbContext <TDbContext>(
            this IServiceCollection services,
            Action <IAbpDbContextRegistrationOptionsBuilder> optionsBuilder = null)
            where TDbContext : AbpDbContext <TDbContext>
        {
            services.AddMemoryCache();

            var options = new AbpDbContextRegistrationOptions(typeof(TDbContext), services);

            var replacedDbContextTypes = typeof(TDbContext).GetCustomAttributes <ReplaceDbContextAttribute>(true)
                                         .SelectMany(x => x.ReplacedDbContextTypes).ToList();

            foreach (var dbContextType in replacedDbContextTypes)
            {
                options.ReplaceDbContext(dbContextType);
            }

            optionsBuilder?.Invoke(options);

            services.TryAddTransient(DbContextOptionsFactory.Create <TDbContext>);

            foreach (var entry in options.ReplacedDbContextTypes)
            {
                var originalDbContextType = entry.Key;
                var targetDbContextType   = entry.Value ?? typeof(TDbContext);

                services.Replace(
                    ServiceDescriptor.Transient(
                        originalDbContextType,
                        sp => sp.GetRequiredService(targetDbContextType)
                        )
                    );

                services.Configure <AbpDbContextOptions>(opts =>
                {
                    opts.DbContextReplacements[originalDbContextType] = targetDbContextType;
                });
            }

            new EfCoreRepositoryRegistrar(options).AddRepositories();

            return(services);
        }
コード例 #4
0
 public AbpEfCoreRepositoryRegistrar(AbpDbContextRegistrationOptions options) : base(options)
 {
 }