mirror of https://github.com/abpframework/abp
parent
91725a7b90
commit
abf84ec2ce
@ -0,0 +1,11 @@
|
||||
using System;
|
||||
|
||||
namespace Volo.Abp.MongoDB
|
||||
{
|
||||
public interface IMongoEntityModel
|
||||
{
|
||||
Type EntityType { get; }
|
||||
|
||||
string CollectionName { get; }
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using JetBrains.Annotations;
|
||||
|
||||
namespace Volo.Abp.MongoDB
|
||||
{
|
||||
public interface IMongoModelBuilder
|
||||
{
|
||||
void Entity<TEntity>([NotNull] Action<MongoEntityModelBuilder> buildAction);
|
||||
|
||||
void Entity([NotNull] Type entityType, [NotNull] Action<MongoEntityModelBuilder> buildAction);
|
||||
|
||||
IReadOnlyList<MongoEntityModelBuilder> GetEntities();
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,7 @@
|
||||
namespace Volo.Abp.MongoDB
|
||||
{
|
||||
public interface IMongoModelSource
|
||||
{
|
||||
MongoDbContextModel GetModel(AbpMongoDbContext dbContext);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,23 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using MongoDB.Driver;
|
||||
using Volo.Abp.Domain.Entities;
|
||||
using Volo.Abp.Reflection;
|
||||
|
||||
namespace Volo.Abp.MongoDB
|
||||
{
|
||||
internal static class MongoDbContextHelper
|
||||
{
|
||||
public static IEnumerable<Type> GetEntityTypes(Type dbContextType)
|
||||
{
|
||||
return
|
||||
from property in dbContextType.GetTypeInfo().GetProperties(BindingFlags.Public | BindingFlags.Instance)
|
||||
where
|
||||
ReflectionHelper.IsAssignableToGenericType(property.PropertyType, typeof(IMongoCollection<>)) &&
|
||||
typeof(IEntity).IsAssignableFrom(property.PropertyType.GenericTypeArguments[0])
|
||||
select property.PropertyType.GenericTypeArguments[0];
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Volo.Abp.MongoDB
|
||||
{
|
||||
public class MongoDbContextModel
|
||||
{
|
||||
public IReadOnlyDictionary<Type, IMongoEntityModel> Entities { get; }
|
||||
|
||||
public MongoDbContextModel(IReadOnlyDictionary<Type, IMongoEntityModel> entities)
|
||||
{
|
||||
Entities = entities;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,17 +0,0 @@
|
||||
using System;
|
||||
|
||||
namespace Volo.Abp.MongoDB
|
||||
{
|
||||
public class MongoEntityMapping
|
||||
{
|
||||
public Type EntityType { get; set; }
|
||||
|
||||
public string CollectionName { get; set; }
|
||||
|
||||
public MongoEntityMapping(Type entityType, string collectionName = null)
|
||||
{
|
||||
EntityType = entityType;
|
||||
CollectionName = collectionName ?? entityType.Name;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,18 @@
|
||||
using System;
|
||||
|
||||
namespace Volo.Abp.MongoDB
|
||||
{
|
||||
public class MongoEntityModelBuilder : IMongoEntityModel
|
||||
{
|
||||
public Type EntityType { get; }
|
||||
|
||||
public string CollectionName { get; set; }
|
||||
|
||||
public MongoEntityModelBuilder(Type entityType)
|
||||
{
|
||||
Check.NotNull(entityType, nameof(entityType));
|
||||
|
||||
EntityType = entityType;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,46 @@
|
||||
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;
|
||||
|
||||
public MongoModelBuilder()
|
||||
{
|
||||
_entityModelBuilders = new Dictionary<Type, MongoEntityModelBuilder>();
|
||||
}
|
||||
|
||||
public MongoDbContextModel Build()
|
||||
{
|
||||
var entityModels = _entityModelBuilders
|
||||
.Select(x => x.Value)
|
||||
.ToImmutableDictionary(x => x.EntityType, x => (IMongoEntityModel) x);
|
||||
|
||||
return new MongoDbContextModel(entityModels);
|
||||
}
|
||||
|
||||
public virtual void Entity<TEntity>([NotNull] Action<MongoEntityModelBuilder> buildAction)
|
||||
{
|
||||
Entity(typeof(TEntity), buildAction);
|
||||
}
|
||||
|
||||
public virtual void Entity([NotNull] Type entityType, [NotNull] Action<MongoEntityModelBuilder> buildAction)
|
||||
{
|
||||
Check.NotNull(entityType, nameof(entityType));
|
||||
Check.NotNull(buildAction, nameof(buildAction));
|
||||
|
||||
var model = _entityModelBuilders.GetOrAdd(entityType, () => new MongoEntityModelBuilder(entityType));
|
||||
buildAction(model);
|
||||
}
|
||||
|
||||
public virtual IReadOnlyList<MongoEntityModelBuilder> GetEntities()
|
||||
{
|
||||
return _entityModelBuilders.Values.ToImmutableList();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,26 @@
|
||||
using JetBrains.Annotations;
|
||||
|
||||
namespace Volo.Abp.MongoDB
|
||||
{
|
||||
public class MongoModelBuilderConfigurationOptions
|
||||
{
|
||||
[NotNull]
|
||||
public string CollectionPrefix
|
||||
{
|
||||
get => _collectionPrefix;
|
||||
set
|
||||
{
|
||||
Check.NotNull(value, nameof(value), $"{nameof(CollectionPrefix)} can not be null! Set to empty string if you don't want a collection prefix.");
|
||||
_collectionPrefix = value;
|
||||
}
|
||||
}
|
||||
private string _collectionPrefix;
|
||||
|
||||
public MongoModelBuilderConfigurationOptions([NotNull] string collectionPrefix = "")
|
||||
{
|
||||
Check.NotNull(collectionPrefix, nameof(collectionPrefix));
|
||||
|
||||
CollectionPrefix = collectionPrefix;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,68 @@
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using MongoDB.Driver;
|
||||
using Volo.Abp.DependencyInjection;
|
||||
using Volo.Abp.Domain.Entities;
|
||||
using Volo.Abp.Reflection;
|
||||
|
||||
namespace Volo.Abp.MongoDB
|
||||
{
|
||||
public class MongoModelSource : IMongoModelSource, ISingletonDependency
|
||||
{
|
||||
protected readonly ConcurrentDictionary<Type, MongoDbContextModel> ModelCache = new ConcurrentDictionary<Type, MongoDbContextModel>();
|
||||
|
||||
public virtual MongoDbContextModel GetModel(AbpMongoDbContext dbContext)
|
||||
{
|
||||
return ModelCache.GetOrAdd(
|
||||
dbContext.GetType(),
|
||||
_ => CreateModel(dbContext)
|
||||
);
|
||||
}
|
||||
|
||||
protected virtual MongoDbContextModel CreateModel(AbpMongoDbContext dbContext)
|
||||
{
|
||||
var modelBuilder = CreateModelBuilder();
|
||||
BuildModelFromDbContextType(modelBuilder, dbContext.GetType());
|
||||
BuildModelFromDbContextInstance(modelBuilder, dbContext);
|
||||
return modelBuilder.Build();
|
||||
}
|
||||
|
||||
protected virtual MongoModelBuilder CreateModelBuilder()
|
||||
{
|
||||
return new MongoModelBuilder();
|
||||
}
|
||||
|
||||
protected virtual void BuildModelFromDbContextType(IMongoModelBuilder modelBuilder, Type dbContextType)
|
||||
{
|
||||
var collectionProperties =
|
||||
from property in dbContextType.GetTypeInfo().GetProperties(BindingFlags.Public | BindingFlags.Instance)
|
||||
where
|
||||
ReflectionHelper.IsAssignableToGenericType(property.PropertyType, typeof(IMongoCollection<>)) &&
|
||||
typeof(IEntity).IsAssignableFrom(property.PropertyType.GenericTypeArguments[0])
|
||||
select property;
|
||||
|
||||
foreach (var collectionProperty in collectionProperties)
|
||||
{
|
||||
BuildModelFromDbContextCollectionProperty(modelBuilder, collectionProperty);
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual void BuildModelFromDbContextCollectionProperty(IMongoModelBuilder modelBuilder, PropertyInfo collectionProperty)
|
||||
{
|
||||
var entityType = collectionProperty.PropertyType.GenericTypeArguments[0];
|
||||
var collectionAttribute = collectionProperty.GetCustomAttributes().OfType<MongoCollectionAttribute>().FirstOrDefault();
|
||||
|
||||
modelBuilder.Entity(entityType, b =>
|
||||
{
|
||||
b.CollectionName = collectionAttribute?.CollectionName ?? collectionProperty.Name;
|
||||
});
|
||||
}
|
||||
|
||||
protected virtual void BuildModelFromDbContextInstance(IMongoModelBuilder modelBuilder, AbpMongoDbContext dbContext)
|
||||
{
|
||||
dbContext.CreateModel(modelBuilder);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue