public static bool RegisterTrackablePocoMap(Type trackablePocoType) { if (BsonClassMap.IsClassMapRegistered(trackablePocoType)) { return(false); } var classMap = new BsonClassMap(trackablePocoType); var pocoType = TrackableResolver.GetPocoType(trackablePocoType); // start with auto map classMap.AutoMap(); // ignore extra elements for smooth schema change classMap.SetIgnoreExtraElements(true); // unmap all members which T doesn't have var propertyNames = new HashSet <string>(pocoType.GetProperties().Select(p => p.Name)); var deletingMembers = classMap.DeclaredMemberMaps.Where(m => { var propertyInfo = m.MemberInfo as PropertyInfo; return(propertyInfo == null || propertyNames.Contains(propertyInfo.Name) == false); }).ToList(); foreach (var m in deletingMembers) { classMap.UnmapMember(m.MemberInfo); } // set default ignore for saving spaces foreach (var memberMap in classMap.DeclaredMemberMaps) { var mt = memberMap.MemberType; var defaultValue = mt.IsValueType ? Activator.CreateInstance(mt) : null; memberMap.SetDefaultValue(defaultValue); memberMap.SetIgnoreIfDefault(true); } // tell customized id to mongo-db var identityColumn = pocoType.GetProperties().FirstOrDefault( p => TrackablePropertyAttribute.GetParameter(p, "mongodb.identity") != null); if (identityColumn != null) { classMap.MapIdProperty(identityColumn.Name); } try { BsonClassMap.RegisterClassMap(classMap); } catch (ArgumentException) { // if duplicate key exists return(false); } return(true); }
public override void Map(BsonClassMap <PriorityDto> cm) { cm.MapIdProperty(x => x.Id); cm.MapProperty(x => x.Name) .SetElementName("name"); }
public void Apply(BsonClassMap classMap) { if (classMap.ClassType.IsClass && typeof(ISaga).IsAssignableFrom(classMap.ClassType)) { classMap.MapIdProperty(nameof(ISaga.CorrelationId)); } }
/// <summary> /// Configure the default mapping for the model. /// </summary> protected virtual void ConfigureDetaultTableMappings <TType>( BsonClassMap <TType> c, string idColumn) { c.AutoMap(); c.MapIdProperty(idColumn); c.SetIgnoreExtraElements(true); }
public override void Map(BsonClassMap <TicketEventDto> cm) { cm.MapIdProperty(x => x.Id); cm.MapProperty(x => x.TicketId).SetElementName("ticketId"); cm.MapProperty(x => x.Created).SetElementName("created"); cm.MapProperty(x => x.UserId).SetElementName("userId"); }
public override void Map(BsonClassMap <FileDto> cm) { cm.MapIdProperty(x => x.Id); cm.MapProperty(f => f.Created).SetElementName("created"); cm.MapProperty(f => f.Id).SetElementName("id"); cm.MapProperty(f => f.Name).SetElementName("name"); cm.MapProperty(f => f.Type).SetElementName("type"); }
public override void Map(BsonClassMap <Article> cm) { cm.AutoMap(); cm.MapIdProperty(c => c.Id); cm.MapProperty(c => c.Title).SetElementName("title"); cm.MapProperty(c => c.Author).SetElementName("author"); cm.MapProperty(c => c.Summary).SetElementName("summary"); cm.MapProperty(c => c.Content).SetElementName("content"); cm.SetIgnoreExtraElements(true); }
private void RegisterId(Type type, string propertyName = "Uid") { if (!BsonClassMap.IsClassMapRegistered(type)) { var cm = new BsonClassMap(type); cm.AutoMap(); cm.MapIdProperty(propertyName).SetIdGenerator(UidGenerator.Instance); cm.SetIgnoreExtraElements(true); cm.SetIgnoreExtraElementsIsInherited(true); BsonClassMap.RegisterClassMap(cm); } }
public override void Map(BsonClassMap <TicketDto> cm) { cm.MapIdProperty(x => x.Id); cm.MapProperty(x => x.Description) .SetElementName("description"); cm.MapProperty(x => x.Priority) .SetElementName("priority"); cm.MapProperty(x => x.Status) .SetElementName("status"); cm.MapProperty(x => x.Title) .SetElementName("title"); cm.MapProperty(x => x.Files) .SetElementName("files"); }
internal static void Build(MongoConfigurationBuilder builder, IList <Action <MongoConfigurationBuilder> > plugins) { if (_isConfigured) { return; } var allMappedClasses = builder.GlobalDatabases.SelectMany(x => x.Value.PolymorpicClasses.Select(y => new { DatabaseName = WashDatabaseName(x.Key), IsPerTenantDatabase = false, IsPolymorphic = true, Type = y.Key, Mapping = y.Value }).Concat(x.Value.SingleClasses.Select(y => new { DatabaseName = WashDatabaseName(x.Key), IsPerTenantDatabase = false, IsPolymorphic = false, Type = y.Key, Mapping = y.Value }))).Concat(builder.TenantDatabases.SelectMany(x => x.Value.PolymorpicClasses.Select(y => new { DatabaseName = WashDatabaseName(x.Key), IsPerTenantDatabase = true, IsPolymorphic = true, Type = y.Key, Mapping = y.Value }).Concat(x.Value.SingleClasses.Select(y => new { DatabaseName = WashDatabaseName(x.Key), IsPerTenantDatabase = true, IsPolymorphic = false, Type = y.Key, Mapping = y.Value })))).ToList(); _cappedCollections = allMappedClasses.Where(x => x.Mapping.Capped).ToDictionary(x => x.Type, x => x.Mapping.CappedCollectionConfig); _collections = allMappedClasses.ToDictionary(x => x.Type, x => new DatabaseCollectionDefinition { CollectionName = x.Mapping.CollectionName, DatabaseName = x.DatabaseName, IsPolymorphic = x.IsPolymorphic }); _collections[typeof(SoftDeletedEntity <>)] = new DatabaseCollectionDefinition { IsPolymorphic = true, CollectionName = "DeletedObjects", DatabaseName = null // One per database }; var baseClasses = allMappedClasses.Where(x => x.IsPolymorphic); foreach (var baseClass in baseClasses) { var subtypes = baseClass.Type.GetTypeInfo().Assembly.GetTypes().Where(st => st.GetTypeInfo().IsSubclassOf(baseClass.Type)); foreach (var subtype in subtypes) { _collections[subtype] = new DatabaseCollectionDefinition { CollectionName = baseClass.Mapping.CollectionName, IsPolymorphic = false, DatabaseName = baseClass.DatabaseName }; } } _indices = allMappedClasses .SelectMany(x => x.Mapping.Indices.Select(y => new { x.Type, Index = y }) ) .ToLookup(x => x.Type, x => x.Index); _globalTypes = new HashSet <Type>(allMappedClasses.Where(x => !x.IsPerTenantDatabase).Select(x => x.Type)); var conventionPack = new ConventionPack { new IgnoreExtraElementsConvention(true), new StringObjectIdConvention() }; ConventionRegistry.Register("Conventions", conventionPack, type => true); foreach (var t in allMappedClasses) { var bsonClassMap = new BsonClassMap(t.Type); bsonClassMap.AutoMap(); if (t.Mapping.IdMember != null) { bsonClassMap.MapIdProperty(t.Mapping.IdMember).SetSerializer(new StringSerializer(BsonType.ObjectId)).SetIdGenerator(new StringObjectIdGenerator()); } if (!BsonClassMap.IsClassMapRegistered(t.Type)) { BsonClassMap.RegisterClassMap(bsonClassMap); } if (t.IsPolymorphic) { var subtypes = t.Type.GetTypeInfo().Assembly.GetTypes().Where(st => st.GetTypeInfo().IsSubclassOf(t.Type)); foreach (var st in subtypes) { var bsonSubClassMap = new BsonClassMap(st); bsonSubClassMap.AutoMap(); if (!BsonClassMap.IsClassMapRegistered(st)) { BsonClassMap.RegisterClassMap(bsonSubClassMap); } } } } foreach (var plugin in plugins) { plugin(builder); } MongoDefaults.MaxConnectionIdleTime = TimeSpan.FromMinutes(1); _isConfigured = true; }
protected override void BeginProcessing() { // | registered by Mdbc if (ClassMap.Contains(Type)) { WriteVerbose($"Type {Type} was registered by Mdbc, doing nothing."); return; } // | registered by driver if (BsonClassMap.IsClassMapRegistered(Type)) { if (ParameterSetName != psForce) { WriteException(new PSInvalidOperationException("Class map is registered by driver. If this is expected invoke with just -Type and -Force."), Type); return; } WriteVerbose($"Type {Type} was registered by driver, registering by Mdbc."); ClassMap.Add(Type); return; } try { var cm = new BsonClassMap(Type); if (Init == null) { cm.AutoMap(); } else { var res = Actor.InvokeScript(Init, cm); if (res.Count > 0) { throw new PSArgumentException("The Init script must not return anything."); } } if (IdProperty != null) { cm.MapIdProperty(IdProperty); } if (Discriminator != null) { cm.SetDiscriminator(Discriminator); } if (DiscriminatorIsRequired) { cm.SetDiscriminatorIsRequired(true); } if (IgnoreExtraElements) { cm.SetIgnoreExtraElements(IgnoreExtraElements); } if (ExtraElementsProperty != null) { cm.MapExtraElementsProperty(ExtraElementsProperty); } // in theory, may throw if the map is registered in another thread BsonClassMap.RegisterClassMap(cm); // done ClassMap.Add(Type); } catch (ArgumentException exn) { WriteException(exn, Type); } }
public void Apply(BsonClassMap classMap) { classMap.MapIdProperty("Id") .SetIgnoreIfDefault(true) .SetIdGenerator(StringObjectIdGenerator.Instance); }
public static void BasicConfig <T>(this BsonClassMap <T> config) where T : EntityBase { config.MapIdProperty(x => x.Id); }
public static bool RegisterTrackablePocoMap(Type trackablePocoType) { if (BsonClassMap.IsClassMapRegistered(trackablePocoType)) return false; var classMap = new BsonClassMap(trackablePocoType); var pocoType = TrackableResolver.GetPocoType(trackablePocoType); // start with auto map classMap.AutoMap(); // ignore extra elements for smooth schema change classMap.SetIgnoreExtraElements(true); // unmap all members which T doesn't have var propertyNames = new HashSet<string>(pocoType.GetProperties().Select(p => p.Name)); var deletingMembers = classMap.DeclaredMemberMaps.Where(m => { var propertyInfo = m.MemberInfo as PropertyInfo; return propertyInfo == null || propertyNames.Contains(propertyInfo.Name) == false; }).ToList(); foreach (var m in deletingMembers) classMap.UnmapMember(m.MemberInfo); // set default ignore for saving spaces foreach (var memberMap in classMap.DeclaredMemberMaps) { var mt = memberMap.MemberType; var defaultValue = mt.IsValueType ? Activator.CreateInstance(mt) : null; memberMap.SetDefaultValue(defaultValue); memberMap.SetIgnoreIfDefault(true); } // tell customized id to mongo-db var identityColumn = pocoType.GetProperties().FirstOrDefault( p => TrackablePropertyAttribute.GetParameter(p, "mongodb.identity") != null); if (identityColumn != null) { classMap.MapIdProperty(identityColumn.Name); } try { BsonClassMap.RegisterClassMap(classMap); } catch (ArgumentException) { // if duplicate key exists return false; } return true; }