mirror of https://github.com/abpframework/abp
Resolved #694: Auto configure defaults for entities for mongodb.
parent
debd0c37fa
commit
71b573bf60
@ -0,0 +1,9 @@
|
||||
using MongoDB.Bson.Serialization;
|
||||
|
||||
namespace Volo.Abp.MongoDB
|
||||
{
|
||||
public interface IHasBsonClassMap
|
||||
{
|
||||
BsonClassMap GetMap();
|
||||
}
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
using System;
|
||||
using MongoDB.Bson.Serialization;
|
||||
|
||||
namespace Volo.Abp.MongoDB
|
||||
{
|
||||
public interface IMongoEntityModelBuilder<TEntity>
|
||||
{
|
||||
Type EntityType { get; }
|
||||
|
||||
string CollectionName { get; set; }
|
||||
|
||||
BsonClassMap<TEntity> BsonMap { get; }
|
||||
}
|
||||
|
||||
public interface IMongoEntityModelBuilder
|
||||
{
|
||||
Type EntityType { get; }
|
||||
|
||||
string CollectionName { get; set; }
|
||||
|
||||
BsonClassMap BsonMap { get; }
|
||||
}
|
||||
}
|
@ -1,18 +1,33 @@
|
||||
using MongoDB.Bson.Serialization;
|
||||
using System;
|
||||
|
||||
namespace Volo.Abp.MongoDB
|
||||
{
|
||||
public class MongoEntityModelBuilder : IMongoEntityModel
|
||||
public class MongoEntityModelBuilder<TEntity> :
|
||||
IMongoEntityModel,
|
||||
IHasBsonClassMap,
|
||||
IMongoEntityModelBuilder,
|
||||
IMongoEntityModelBuilder<TEntity>
|
||||
{
|
||||
public Type EntityType { get; }
|
||||
|
||||
public string CollectionName { get; set; }
|
||||
|
||||
public MongoEntityModelBuilder(Type entityType)
|
||||
BsonClassMap IMongoEntityModelBuilder.BsonMap => _bsonClassMap;
|
||||
BsonClassMap<TEntity> IMongoEntityModelBuilder<TEntity>.BsonMap => _bsonClassMap;
|
||||
|
||||
private readonly BsonClassMap<TEntity> _bsonClassMap;
|
||||
|
||||
public MongoEntityModelBuilder()
|
||||
{
|
||||
Check.NotNull(entityType, nameof(entityType));
|
||||
EntityType = typeof(TEntity);
|
||||
_bsonClassMap = new BsonClassMap<TEntity>();
|
||||
_bsonClassMap.ConfigureAbpConventions();
|
||||
}
|
||||
|
||||
EntityType = entityType;
|
||||
public BsonClassMap GetMap()
|
||||
{
|
||||
return _bsonClassMap;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,46 +1,71 @@
|
||||
using MongoDB.Bson.Serialization;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.Linq;
|
||||
using JetBrains.Annotations;
|
||||
|
||||
namespace Volo.Abp.MongoDB
|
||||
{
|
||||
public class MongoModelBuilder : IMongoModelBuilder
|
||||
{
|
||||
private readonly Dictionary<Type, MongoEntityModelBuilder> _entityModelBuilders;
|
||||
private readonly Dictionary<Type, object> _entityModelBuilders;
|
||||
|
||||
private static readonly object SyncObj = new object();
|
||||
|
||||
public MongoModelBuilder()
|
||||
{
|
||||
_entityModelBuilders = new Dictionary<Type, MongoEntityModelBuilder>();
|
||||
_entityModelBuilders = new Dictionary<Type, object>();
|
||||
}
|
||||
|
||||
public MongoDbContextModel Build()
|
||||
{
|
||||
var entityModels = _entityModelBuilders
|
||||
.Select(x => x.Value)
|
||||
.ToImmutableDictionary(x => x.EntityType, x => (IMongoEntityModel) x);
|
||||
.Cast<IMongoEntityModel>()
|
||||
.ToImmutableDictionary(x => x.EntityType, x => x);
|
||||
|
||||
foreach (var entityModel in entityModels.Values)
|
||||
{
|
||||
var map = entityModel.As<IHasBsonClassMap>().GetMap();
|
||||
lock (SyncObj)
|
||||
{
|
||||
if (!BsonClassMap.IsClassMapRegistered(map.ClassType))
|
||||
{
|
||||
BsonClassMap.RegisterClassMap(map);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return new MongoDbContextModel(entityModels);
|
||||
}
|
||||
|
||||
public virtual void Entity<TEntity>([NotNull] Action<MongoEntityModelBuilder> buildAction)
|
||||
public virtual void Entity<TEntity>(Action<IMongoEntityModelBuilder<TEntity>> buildAction = null)
|
||||
{
|
||||
Entity(typeof(TEntity), buildAction);
|
||||
var model = (IMongoEntityModelBuilder<TEntity>)_entityModelBuilders.GetOrAdd(
|
||||
typeof(TEntity),
|
||||
() => new MongoEntityModelBuilder<TEntity>()
|
||||
);
|
||||
|
||||
buildAction?.Invoke(model);
|
||||
}
|
||||
|
||||
public virtual void Entity([NotNull] Type entityType, [NotNull] Action<MongoEntityModelBuilder> buildAction)
|
||||
public virtual void Entity(Type entityType, Action<IMongoEntityModelBuilder> buildAction = null)
|
||||
{
|
||||
Check.NotNull(entityType, nameof(entityType));
|
||||
Check.NotNull(buildAction, nameof(buildAction));
|
||||
|
||||
var model = _entityModelBuilders.GetOrAdd(entityType, () => new MongoEntityModelBuilder(entityType));
|
||||
buildAction(model);
|
||||
var model = (IMongoEntityModelBuilder)_entityModelBuilders.GetOrAdd(
|
||||
entityType,
|
||||
() => (IMongoEntityModelBuilder)Activator.CreateInstance(
|
||||
typeof(MongoEntityModelBuilder<>).MakeGenericType(entityType)
|
||||
)
|
||||
);
|
||||
|
||||
buildAction?.Invoke(model);
|
||||
}
|
||||
|
||||
public virtual IReadOnlyList<MongoEntityModelBuilder> GetEntities()
|
||||
public virtual IReadOnlyList<IMongoEntityModel> GetEntities()
|
||||
{
|
||||
return _entityModelBuilders.Values.ToImmutableList();
|
||||
return _entityModelBuilders.Values.Cast<IMongoEntityModel>().ToImmutableList();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,21 +0,0 @@
|
||||
using MongoDB.Bson.Serialization;
|
||||
using Volo.Abp.Threading;
|
||||
|
||||
namespace Volo.Abp.SettingManagement.MongoDB
|
||||
{
|
||||
public static class AbpSettingManagementBsonClassMap
|
||||
{
|
||||
private static readonly OneTimeRunner OneTimeRunner = new OneTimeRunner();
|
||||
|
||||
public static void Configure()
|
||||
{
|
||||
OneTimeRunner.Run(() =>
|
||||
{
|
||||
BsonClassMap.RegisterClassMap<Setting>(map =>
|
||||
{
|
||||
map.AutoMap();
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue