mirror of https://github.com/abpframework/abp.git
31 changed files with 206 additions and 441 deletions
@ -1,17 +1,42 @@ |
|||
using MongoDB.Bson.Serialization; |
|||
using System; |
|||
using MongoDB.Bson.Serialization; |
|||
using Volo.Abp.Data; |
|||
|
|||
namespace Volo.Abp.MongoDB |
|||
{ |
|||
public static class AbpBsonClassMapExtensions |
|||
{ |
|||
public static void ConfigureExtraProperties<T>(this BsonClassMap<T> map) |
|||
where T : class, IHasExtraProperties |
|||
public static void ConfigureAbpConventions(this BsonClassMap map) |
|||
{ |
|||
map.AutoMap(); |
|||
map.ConfigureExtraProperties(); |
|||
} |
|||
|
|||
public static void ConfigureExtraProperties<TEntity>(this BsonClassMap<TEntity> map) |
|||
where TEntity : class, IHasExtraProperties |
|||
{ |
|||
map.SetExtraElementsMember(new BsonMemberMap( |
|||
map, |
|||
typeof(T).GetMember(nameof(IHasExtraProperties.ExtraProperties))[0]) |
|||
typeof(TEntity).GetMember(nameof(IHasExtraProperties.ExtraProperties))[0]) |
|||
); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Configures SetExtraElementsMember if the <see cref="BsonClassMap.ClassType"/>
|
|||
/// implements the <see cref="IHasExtraProperties"/> interface.
|
|||
/// Otherwise, does nothing
|
|||
/// </summary>
|
|||
public static void ConfigureExtraProperties(this BsonClassMap map) |
|||
{ |
|||
if (map.ClassType.IsAssignableTo<IHasExtraProperties>()) |
|||
{ |
|||
map.SetExtraElementsMember( |
|||
new BsonMemberMap( |
|||
map, |
|||
map.ClassType.GetMember(nameof(IHasExtraProperties.ExtraProperties))[0] |
|||
) |
|||
); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
|
|||
@ -1,60 +0,0 @@ |
|||
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); |
|||
} |
|||
} |
|||
} |
|||
@ -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.AuditLogging.MongoDB |
|||
{ |
|||
public static class AbpAuditLoggingBsonClassMap |
|||
{ |
|||
private static readonly OneTimeRunner OneTimeRunner = new OneTimeRunner(); |
|||
|
|||
public static void Configure() |
|||
{ |
|||
OneTimeRunner.Run(() => |
|||
{ |
|||
BsonClassMap.RegisterClassMap<AuditLog>(map => |
|||
{ |
|||
map.AutoMap(); |
|||
}); |
|||
}); |
|||
} |
|||
} |
|||
} |
|||
@ -1,23 +0,0 @@ |
|||
using MongoDB.Bson.Serialization; |
|||
using Volo.Abp.MongoDB; |
|||
using Volo.Abp.Threading; |
|||
|
|||
namespace Volo.Abp.BackgroundJobs.MongoDB |
|||
{ |
|||
public static class BackgroundJobsBsonClassMap |
|||
{ |
|||
private static readonly OneTimeRunner OneTimeRunner = new OneTimeRunner(); |
|||
|
|||
public static void Configure() |
|||
{ |
|||
OneTimeRunner.Run(() => |
|||
{ |
|||
BsonClassMap.RegisterClassMap<BackgroundJobRecord>(map => |
|||
{ |
|||
map.AutoMap(); |
|||
map.ConfigureExtraProperties(); |
|||
}); |
|||
}); |
|||
} |
|||
} |
|||
} |
|||
@ -1,48 +0,0 @@ |
|||
using MongoDB.Bson.Serialization; |
|||
using Volo.Abp.MongoDB; |
|||
using Volo.Abp.Threading; |
|||
using Volo.Blogging.Blogs; |
|||
using Volo.Blogging.Comments; |
|||
using Volo.Blogging.Posts; |
|||
using Volo.Blogging.Tagging; |
|||
using Volo.Blogging.Users; |
|||
|
|||
namespace Volo.Blogging.MongoDB |
|||
{ |
|||
public static class AbpBloggingBsonClassMap |
|||
{ |
|||
private static readonly OneTimeRunner OneTimeRunner = new OneTimeRunner(); |
|||
|
|||
public static void Configure() |
|||
{ |
|||
OneTimeRunner.Run(() => |
|||
{ |
|||
BsonClassMap.RegisterClassMap<BlogUser>(map => |
|||
{ |
|||
map.AutoMap(); |
|||
map.ConfigureExtraProperties(); |
|||
}); |
|||
BsonClassMap.RegisterClassMap<Blog>(map => |
|||
{ |
|||
map.AutoMap(); |
|||
map.ConfigureExtraProperties(); |
|||
}); |
|||
BsonClassMap.RegisterClassMap<Post>(map => |
|||
{ |
|||
map.AutoMap(); |
|||
map.ConfigureExtraProperties(); |
|||
}); |
|||
BsonClassMap.RegisterClassMap<Comment>(map => |
|||
{ |
|||
map.AutoMap(); |
|||
map.ConfigureExtraProperties(); |
|||
}); |
|||
BsonClassMap.RegisterClassMap<Tag>(map => |
|||
{ |
|||
map.AutoMap(); |
|||
map.ConfigureExtraProperties(); |
|||
}); |
|||
}); |
|||
} |
|||
} |
|||
} |
|||
@ -1,35 +0,0 @@ |
|||
using MongoDB.Bson.Serialization; |
|||
using Volo.Abp.MongoDB; |
|||
using Volo.Abp.Threading; |
|||
|
|||
namespace Volo.Abp.Identity.MongoDB |
|||
{ |
|||
public static class AbpIdentityBsonClassMap |
|||
{ |
|||
private static readonly OneTimeRunner OneTimeRunner = new OneTimeRunner(); |
|||
|
|||
public static void Configure() |
|||
{ |
|||
OneTimeRunner.Run(() => |
|||
{ |
|||
BsonClassMap.RegisterClassMap<IdentityUser>(map => |
|||
{ |
|||
map.AutoMap(); |
|||
map.ConfigureExtraProperties(); |
|||
}); |
|||
|
|||
BsonClassMap.RegisterClassMap<IdentityRole>(map => |
|||
{ |
|||
map.AutoMap(); |
|||
map.ConfigureExtraProperties(); |
|||
}); |
|||
|
|||
BsonClassMap.RegisterClassMap<IdentityClaimType>(map => |
|||
{ |
|||
map.AutoMap(); |
|||
map.ConfigureExtraProperties(); |
|||
}); |
|||
}); |
|||
} |
|||
} |
|||
} |
|||
@ -1,42 +0,0 @@ |
|||
using MongoDB.Bson.Serialization; |
|||
using Volo.Abp.IdentityServer.ApiResources; |
|||
using Volo.Abp.IdentityServer.Clients; |
|||
using Volo.Abp.IdentityServer.Grants; |
|||
using Volo.Abp.IdentityServer.IdentityResources; |
|||
using Volo.Abp.MongoDB; |
|||
using Volo.Abp.Threading; |
|||
|
|||
namespace Volo.Abp.IdentityServer.MongoDB |
|||
{ |
|||
public class AbpIdentityServerBsonClassMap |
|||
{ |
|||
private static readonly OneTimeRunner OneTimeRunner = new OneTimeRunner(); |
|||
|
|||
public static void Configure() |
|||
{ |
|||
OneTimeRunner.Run(() => |
|||
{ |
|||
BsonClassMap.RegisterClassMap<ApiResource>(map => |
|||
{ |
|||
map.AutoMap(); |
|||
map.ConfigureExtraProperties(); |
|||
}); |
|||
BsonClassMap.RegisterClassMap<Client>(map => |
|||
{ |
|||
map.AutoMap(); |
|||
map.ConfigureExtraProperties(); |
|||
}); |
|||
BsonClassMap.RegisterClassMap<IdentityResource>(map => |
|||
{ |
|||
map.AutoMap(); |
|||
map.ConfigureExtraProperties(); |
|||
}); |
|||
BsonClassMap.RegisterClassMap<PersistedGrant>(map => |
|||
{ |
|||
map.AutoMap(); |
|||
map.ConfigureExtraProperties(); |
|||
}); |
|||
}); |
|||
} |
|||
} |
|||
} |
|||
@ -1,21 +0,0 @@ |
|||
using MongoDB.Bson.Serialization; |
|||
using Volo.Abp.Threading; |
|||
|
|||
namespace Volo.Abp.PermissionManagement.MongoDB |
|||
{ |
|||
public static class AbpPermissionManagementBsonClassMap |
|||
{ |
|||
private static readonly OneTimeRunner OneTimeRunner = new OneTimeRunner(); |
|||
|
|||
public static void Configure() |
|||
{ |
|||
OneTimeRunner.Run(() => |
|||
{ |
|||
BsonClassMap.RegisterClassMap<PermissionGrant>(map => |
|||
{ |
|||
map.AutoMap(); |
|||
}); |
|||
}); |
|||
} |
|||
} |
|||
} |
|||
@ -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(); |
|||
}); |
|||
}); |
|||
} |
|||
} |
|||
} |
|||
@ -1,23 +0,0 @@ |
|||
using MongoDB.Bson.Serialization; |
|||
using Volo.Abp.MongoDB; |
|||
using Volo.Abp.Threading; |
|||
|
|||
namespace Volo.Abp.TenantManagement.MongoDb |
|||
{ |
|||
public static class AbpTenantManagementBsonClassMap |
|||
{ |
|||
private static readonly OneTimeRunner OneTimeRunner = new OneTimeRunner(); |
|||
|
|||
public static void Configure() |
|||
{ |
|||
OneTimeRunner.Run(() => |
|||
{ |
|||
BsonClassMap.RegisterClassMap<Tenant>(map => |
|||
{ |
|||
map.AutoMap(); |
|||
map.ConfigureExtraProperties(); |
|||
}); |
|||
}); |
|||
} |
|||
} |
|||
} |
|||
@ -1,21 +0,0 @@ |
|||
using Volo.Abp.Threading; |
|||
|
|||
namespace MyCompanyName.MyProjectName.MongoDB |
|||
{ |
|||
public static class MyProjectNameBsonClassMap |
|||
{ |
|||
private static readonly OneTimeRunner OneTimeRunner = new OneTimeRunner(); |
|||
|
|||
public static void Configure() |
|||
{ |
|||
OneTimeRunner.Run(() => |
|||
{ |
|||
//Register mappings here. Example:
|
|||
//BsonClassMap.RegisterClassMap<Question>(map =>
|
|||
//{
|
|||
// map.AutoMap();
|
|||
//});
|
|||
}); |
|||
} |
|||
} |
|||
} |
|||
@ -1,21 +0,0 @@ |
|||
using Volo.Abp.Threading; |
|||
|
|||
namespace MyCompanyName.MyProjectName.MongoDB |
|||
{ |
|||
public static class MyProjectNameBsonClassMap |
|||
{ |
|||
private static readonly OneTimeRunner OneTimeRunner = new OneTimeRunner(); |
|||
|
|||
public static void Configure() |
|||
{ |
|||
OneTimeRunner.Run(() => |
|||
{ |
|||
//Register mappings here. Example:
|
|||
//BsonClassMap.RegisterClassMap<Question>(map =>
|
|||
//{
|
|||
// map.AutoMap();
|
|||
//});
|
|||
}); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue