Beispiel #1
0
        public static IServiceCollection AddMongoDbContext <TMongoDbContext>(this IServiceCollection services, Action <IAbpMongoDbContextRegistrationOptionsBuilder> optionsBuilder = null) //Created overload instead of default parameter
            where TMongoDbContext : AbpMongoDbContext
        {
            var options = new AbpMongoDbContextRegistrationOptions(typeof(TMongoDbContext), services);

            optionsBuilder?.Invoke(options);

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

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

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

            new MongoDbRepositoryRegistrar(options).AddRepositories();

            return(services);
        }
Beispiel #2
0
        public static IServiceCollection AddMongoDbContext <TMongoDbContext>(this IServiceCollection services, Action <IAbpMongoDbContextRegistrationOptionsBuilder> optionsBuilder = null) //Created overload instead of default parameter
            where TMongoDbContext : AbpMongoDbContext
        {
            var options = new AbpMongoDbContextRegistrationOptions(typeof(TMongoDbContext), services);

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

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

            optionsBuilder?.Invoke(options);

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

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

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

            new MongoDbRepositoryRegistrar(options).AddRepositories();

            return(services);
        }