Browse Source

Auto configure BsonClassMap for IHasExtraProperties

pull/696/head
Halil ibrahim Kalkan 8 years ago
parent
commit
037d1a062a
  1. 60
      framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/AbpGlobalBsonClassMap.cs
  2. 2
      framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/MongoModelSource.cs

60
framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/AbpGlobalBsonClassMap.cs

@ -0,0 +1,60 @@
using System;
using System.Collections.Generic;
using MongoDB.Bson.Serialization;
using Volo.Abp.Data;
namespace Volo.Abp.MongoDB
{
public static class AbpGlobalBsonClassMap
{
private static readonly HashSet<Type> PreConfiguredTypes = new HashSet<Type>();
/// <summary>
/// Configure default/base properties for the entity using <see cref="BsonClassMap"/>.
/// This method runs single time for an <typeparamref name="TEntity"/> for the application lifetime.
/// Subsequent calls has no effect for the same <typeparamref name="TEntity"/>.
/// </summary>
public static void ConfigureDefaults<TEntity>()
{
ConfigureDefaults(typeof(TEntity));
}
/// <summary>
/// Configure default/base properties for the entity using <see cref="BsonClassMap"/>.
/// This method runs single time for an <paramref name="entityType"/> for the application lifetime.
/// Subsequent calls has no effect for the same <paramref name="entityType"/>.
/// </summary>
public static void ConfigureDefaults(Type entityType)
{
lock (PreConfiguredTypes)
{
if (PreConfiguredTypes.Contains(entityType))
{
return;
}
ConfigureDefaultsInternal(entityType);
PreConfiguredTypes.Add(entityType);
}
}
private static void ConfigureDefaultsInternal(Type entityType)
{
var map = new BsonClassMap(entityType);
map.AutoMap();
if (entityType.IsAssignableTo<IHasExtraProperties>())
{
map.SetExtraElementsMember(
new BsonMemberMap(
map,
entityType.GetMember(nameof(IHasExtraProperties.ExtraProperties))[0]
)
);
}
BsonClassMap.RegisterClassMap(map);
}
}
}

2
framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/MongoModelSource.cs

@ -58,6 +58,8 @@ namespace Volo.Abp.MongoDB
{
b.CollectionName = collectionAttribute?.CollectionName ?? collectionProperty.Name;
});
AbpGlobalBsonClassMap.ConfigureDefaults(entityType);
}
protected virtual void BuildModelFromDbContextInstance(IMongoModelBuilder modelBuilder, AbpMongoDbContext dbContext)

Loading…
Cancel
Save