mirror of https://github.com/abpframework/abp.git
10 changed files with 70 additions and 18 deletions
@ -1,16 +1,43 @@ |
|||||
using System; |
using System; |
||||
using System.Collections.Generic; |
using System.Collections.Generic; |
||||
|
using System.Linq; |
||||
|
using Volo.ExtensionMethods.Collections.Generic; |
||||
|
|
||||
namespace Volo.Abp.MongoDB |
namespace Volo.Abp.MongoDB |
||||
{ |
{ |
||||
public abstract class AbpMongoDbContext |
public abstract class AbpMongoDbContext |
||||
{ |
{ |
||||
//TODO: Array of an EntityInfo object which contains EntityType and CollectionName
|
private static readonly MongoEntityMapping[] EmptyTypeList = new MongoEntityMapping[0]; |
||||
private static readonly Type[] EmptyTypeList = new Type[0]; |
|
||||
|
|
||||
public virtual IReadOnlyList<Type> GetEntityCollectionTypes() |
private readonly Lazy<Dictionary<Type, MongoEntityMapping>> _mappingsByType; |
||||
|
|
||||
|
protected AbpMongoDbContext() |
||||
|
{ |
||||
|
_mappingsByType = new Lazy<Dictionary<Type, MongoEntityMapping>>(() => |
||||
|
{ |
||||
|
return GetMappings().ToDictionary(m => m.EntityType); |
||||
|
}, true); |
||||
|
} |
||||
|
|
||||
|
public virtual IReadOnlyList<MongoEntityMapping> GetMappings() |
||||
{ |
{ |
||||
return EmptyTypeList; |
return EmptyTypeList; |
||||
} |
} |
||||
|
|
||||
|
public string GetCollectionName<T>() |
||||
|
{ |
||||
|
return GetMapping<T>().CollectionName; |
||||
|
} |
||||
|
|
||||
|
private MongoEntityMapping GetMapping<T>() |
||||
|
{ |
||||
|
var mapping = _mappingsByType.Value.GetOrDefault(typeof(T)); |
||||
|
if (mapping == null) |
||||
|
{ |
||||
|
throw new AbpException("Unmapped entity type: " + typeof(T).AssemblyQualifiedName); |
||||
|
} |
||||
|
|
||||
|
return mapping; |
||||
|
} |
||||
} |
} |
||||
} |
} |
||||
@ -0,0 +1,17 @@ |
|||||
|
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; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue