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); }
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); }