Esempio n. 1
0
 public static void AddMongoRepository <T>(this IServiceCollection services, string collection,
                                           Action <BsonClassMap <T> > mapSetup = null, Action <IMongoIndexManager <T> > indexSetup = null)
     where T : IIdentifiable
 {
     DataAccessClassMap.RegisterClass(mapSetup);
     services.AddSingleton <IRepository <T> >(sp => CreateMongoRepository(sp, collection, indexSetup));
 }
Esempio n. 2
0
 public static void AddMongoRepository <T>(this IServiceCollection services, string collectionName,
                                           Action <BsonClassMap <T> > mapSetup = null, Action <IMongoIndexManager <T> > indexSetup = null) where T : Entity
 {
     DataAccessClassMap.RegisterClass(mapSetup);
     services.AddSingleton <IRepository <T> >(sp => new MongoRepository <T>(
                                                  sp.GetService <IMongoDatabase>().GetCollection <T>(collectionName), c => indexSetup?.Invoke(c.Indexes)));
 }
Esempio n. 3
0
        public static IServiceCollection AddDataAccess(this IServiceCollection services,
                                                       IConfiguration configuration)
        {
            var    options         = configuration.GetOptions <DataAccessOptions>();
            string jobDatabaseName = options.JobDatabaseName ?? options.Prefix + "_jobs";

            services.AddHangfire(x => x.UseMongoStorage($"{options.ConnectionString}/{jobDatabaseName}",
                                                        new MongoStorageOptions
            {
                MigrationOptions = new MongoMigrationOptions
                {
                    Strategy = MongoMigrationStrategy.Migrate
                }
            }));

            DataAccessClassMap.RegisterConventions("SIL.XForge",
                                                   new CamelCaseElementNameConvention(),
                                                   new EnumRepresentationConvention(BsonType.String),
                                                   new IgnoreIfNullConvention(true));

            DataAccessClassMap.RegisterClass <Json0Snapshot>(cm => cm.MapIdProperty(e => e.Id));
            DataAccessClassMap.RegisterClass <ProjectSecret>(cm => cm.MapIdProperty(e => e.Id));

            services.AddSingleton <IMongoClient>(sp => new MongoClient(options.ConnectionString));
            services.AddSingleton(sp => sp.GetService <IMongoClient>().GetDatabase(options.MongoDatabaseName));

            services.AddMongoRepository <UserSecret>("user_secrets", cm => cm.MapIdProperty(us => us.Id));
            services.AddMongoRepository <BetaMigration>("users_beta_migration");

            return(services);
        }
Esempio n. 4
0
        public static IServiceCollection AddSFDataAccess(this IServiceCollection services,
                                                         IConfiguration configuration)
        {
            services.AddDataAccess(configuration);

            DataAccessClassMap.RegisterClass <SyncUser>(cm => cm.SetIdMember(null));

            services.AddMongoRepository <TranslateMetrics>("translate_metrics", cm => cm.MapIdProperty(tm => tm.Id));
            services.AddMongoRepository <SFProjectSecret>("sf_project_secrets");

            return(services);
        }
Esempio n. 5
0
        public static IServiceCollection AddDataAccess(this IServiceCollection services,
                                                       IConfiguration configuration)
        {
            var options = configuration.GetOptions <DataAccessOptions>();

            services.AddHangfire(x => x.UseMongoStorage(options.ConnectionString, options.JobDatabase,
                                                        new MongoStorageOptions
            {
                MigrationOptions = new MongoMigrationOptions
                {
                    Strategy = MongoMigrationStrategy.Migrate
                }
            }));

            DataAccessClassMap.RegisterConventions("SIL.XForge",
                                                   new CamelCaseElementNameConvention(),
                                                   new ObjectRefConvention(),
                                                   new EnumRepresentationConvention(BsonType.String),
                                                   new IgnoreIfNullConvention(true));

            DataAccessClassMap.RegisterClass <Entity>(cm =>
            {
                cm.MapIdProperty(e => e.Id)
                .SetIdGenerator(StringObjectIdGenerator.Instance)
                .SetSerializer(new StringSerializer(BsonType.ObjectId));
            });

            DataAccessClassMap.RegisterClass <ProjectUserEntity>(cm =>
            {
                cm.SetIdMember(null);
                cm.MapProperty(u => u.Id).SetSerializer(new StringSerializer(BsonType.ObjectId));
                cm.UnmapProperty(u => u.ProjectRef);
            });

            services.AddSingleton <IMongoClient>(sp => new MongoClient(options.ConnectionString));
            services.AddSingleton <IMongoDatabase>(
                sp => sp.GetService <IMongoClient>().GetDatabase(options.MongoDatabaseName));

            services.AddMongoRepository <UserEntity>("users",
                                                     mapSetup: cm =>
            {
                var customSitesSerializer =
                    new DictionaryInterfaceImplementerSerializer <Dictionary <string, Site> >(
                        DictionaryRepresentation.Document, new StringSerializer(),
                        BsonSerializer.SerializerRegistry.GetSerializer <Site>());
                cm.GetMemberMap(u => u.Sites).SetSerializer(customSitesSerializer);
            });

            return(services);
        }