From 1828593051acd05f423f5dcb1c03327205afcbe5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Tue, 21 Feb 2017 17:33:26 +0300 Subject: [PATCH] Mongodb configuration to map entities to collections. --- .../AbpDesk/ConsoleDemo/BlogPostLister.cs | 10 ++++-- .../AbpDesk/Blogging/AbpDeskMongoDbContext.cs | 7 ++-- .../AbpMongoDbServiceCollectionExtensions.cs | 3 +- .../Repositories/MongoDB/MongoDbRepository.cs | 5 +-- .../Volo/Abp/MongoDB/AbpMongoDbContext.cs | 33 +++++++++++++++++-- .../Volo/Abp/MongoDB/AbpMongoDbModule.cs | 2 -- .../EfCoreRepositoryRegistrar.cs | 3 +- .../Abp/MongoDB/IMongoDatabaseProvider.cs | 2 ++ .../Volo/Abp/MongoDB/MongoEntityMapping.cs | 17 ++++++++++ .../UnitOfWorkMongoDatabaseProvider.cs | 6 +++- 10 files changed, 70 insertions(+), 18 deletions(-) create mode 100644 src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/MongoEntityMapping.cs diff --git a/src/AbpDesk/AbpDesk.ConsoleDemo/AbpDesk/ConsoleDemo/BlogPostLister.cs b/src/AbpDesk/AbpDesk.ConsoleDemo/AbpDesk/ConsoleDemo/BlogPostLister.cs index a1d942f71a..f9101e0a90 100644 --- a/src/AbpDesk/AbpDesk.ConsoleDemo/AbpDesk/ConsoleDemo/BlogPostLister.cs +++ b/src/AbpDesk/AbpDesk.ConsoleDemo/AbpDesk/ConsoleDemo/BlogPostLister.cs @@ -2,6 +2,7 @@ using System.Globalization; using AbpDesk.Blogging; using Volo.Abp.Domain.Repositories; +using Volo.Abp.Guids; using Volo.Abp.Uow; using Volo.DependencyInjection; @@ -11,11 +12,16 @@ namespace AbpDesk.ConsoleDemo { private readonly IQueryableRepository _blogPostRepository; private readonly IUnitOfWorkManager _unitOfWorkManager; + private readonly IGuidGenerator _guidGenerator; - public BlogPostLister(IQueryableRepository blogPostRepository, IUnitOfWorkManager unitOfWorkManager) + public BlogPostLister( + IQueryableRepository blogPostRepository, + IUnitOfWorkManager unitOfWorkManager, + IGuidGenerator guidGenerator) { _blogPostRepository = blogPostRepository; _unitOfWorkManager = unitOfWorkManager; + _guidGenerator = guidGenerator; } public void List() @@ -30,7 +36,7 @@ namespace AbpDesk.ConsoleDemo //blog.Comments.Add(new BlogPostComment("@john", "good post! " + DateTime.Now.ToString(CultureInfo.InvariantCulture), star: (byte)RandomHelper.GetRandom(1, 6))); //_blogPostRepository.Update(blog); - //_blogPostRepository.Insert(new BlogPost("Hello World 1!", DateTime.Now.ToString(CultureInfo.InvariantCulture))); + //_blogPostRepository.Insert(new BlogPost(_guidGenerator.Create(), "Hello World 1!", DateTime.Now.ToString(CultureInfo.InvariantCulture))); foreach (var blogPost in _blogPostRepository) { diff --git a/src/AbpDesk/AbpDesk.MongoBlog/AbpDesk/Blogging/AbpDeskMongoDbContext.cs b/src/AbpDesk/AbpDesk.MongoBlog/AbpDesk/Blogging/AbpDeskMongoDbContext.cs index aeb62dbcb0..6b270b796e 100644 --- a/src/AbpDesk/AbpDesk.MongoBlog/AbpDesk/Blogging/AbpDeskMongoDbContext.cs +++ b/src/AbpDesk/AbpDesk.MongoBlog/AbpDesk/Blogging/AbpDeskMongoDbContext.cs @@ -1,4 +1,3 @@ -using System; using System.Collections.Generic; using Volo.Abp.Data; using Volo.Abp.MongoDB; @@ -10,11 +9,11 @@ namespace AbpDesk.Blogging { public const string ConnectionStringName = "AbpDeskMongoBlog"; - private static readonly Type[] EntityCollectionTypes = { - typeof(BlogPost) + private static readonly MongoEntityMapping[] EntityCollectionTypes = { + new MongoEntityMapping(typeof(BlogPost), "BlogPosts") }; - public override IReadOnlyList GetEntityCollectionTypes() + public override IReadOnlyList GetMappings() { return EntityCollectionTypes; } diff --git a/src/Volo.Abp.MongoDB/Microsoft/Extensions/DependencyInjection/AbpMongoDbServiceCollectionExtensions.cs b/src/Volo.Abp.MongoDB/Microsoft/Extensions/DependencyInjection/AbpMongoDbServiceCollectionExtensions.cs index 47c35982e6..195ac135c4 100644 --- a/src/Volo.Abp.MongoDB/Microsoft/Extensions/DependencyInjection/AbpMongoDbServiceCollectionExtensions.cs +++ b/src/Volo.Abp.MongoDB/Microsoft/Extensions/DependencyInjection/AbpMongoDbServiceCollectionExtensions.cs @@ -1,5 +1,4 @@ using System; -using Volo.Abp.Domain.Repositories.MongoDB; using Volo.Abp.MongoDB; using Volo.Abp.MongoDB.DependencyInjection; @@ -13,6 +12,8 @@ namespace Microsoft.Extensions.DependencyInjection var options = new MongoDbContextRegistrationOptions(); optionsBuilder?.Invoke(options); + services.AddSingleton(); + new MongoDbRepositoryRegistrar(options) .AddRepositories(services, typeof(TMongoDbContext)); diff --git a/src/Volo.Abp.MongoDB/Volo/Abp/Domain/Repositories/MongoDB/MongoDbRepository.cs b/src/Volo.Abp.MongoDB/Volo/Abp/Domain/Repositories/MongoDB/MongoDbRepository.cs index ee443f8940..550f17fd8e 100644 --- a/src/Volo.Abp.MongoDB/Volo/Abp/Domain/Repositories/MongoDB/MongoDbRepository.cs +++ b/src/Volo.Abp.MongoDB/Volo/Abp/Domain/Repositories/MongoDB/MongoDbRepository.cs @@ -23,10 +23,7 @@ namespace Volo.Abp.Domain.Repositories.MongoDB where TMongoDbContext : AbpMongoDbContext where TEntity : class, IEntity { - public virtual string CollectionName - { - get { return typeof(TEntity).Name; } //TODO: a better naming, or a way of easily overriding it? - } + public virtual string CollectionName => DatabaseProvider.DbContext.GetCollectionName(); public virtual IMongoCollection Collection => Database.GetCollection(CollectionName); diff --git a/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/AbpMongoDbContext.cs b/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/AbpMongoDbContext.cs index 6225b90f3e..25b086d908 100644 --- a/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/AbpMongoDbContext.cs +++ b/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/AbpMongoDbContext.cs @@ -1,16 +1,43 @@ using System; using System.Collections.Generic; +using System.Linq; +using Volo.ExtensionMethods.Collections.Generic; namespace Volo.Abp.MongoDB { public abstract class AbpMongoDbContext { - //TODO: Array of an EntityInfo object which contains EntityType and CollectionName - private static readonly Type[] EmptyTypeList = new Type[0]; + private static readonly MongoEntityMapping[] EmptyTypeList = new MongoEntityMapping[0]; - public virtual IReadOnlyList GetEntityCollectionTypes() + private readonly Lazy> _mappingsByType; + + protected AbpMongoDbContext() + { + _mappingsByType = new Lazy>(() => + { + return GetMappings().ToDictionary(m => m.EntityType); + }, true); + } + + public virtual IReadOnlyList GetMappings() { return EmptyTypeList; } + + public string GetCollectionName() + { + return GetMapping().CollectionName; + } + + private MongoEntityMapping GetMapping() + { + var mapping = _mappingsByType.Value.GetOrDefault(typeof(T)); + if (mapping == null) + { + throw new AbpException("Unmapped entity type: " + typeof(T).AssemblyQualifiedName); + } + + return mapping; + } } } \ No newline at end of file diff --git a/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/AbpMongoDbModule.cs b/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/AbpMongoDbModule.cs index 192e69733c..0accee847a 100644 --- a/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/AbpMongoDbModule.cs +++ b/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/AbpMongoDbModule.cs @@ -9,8 +9,6 @@ namespace Volo.Abp.MongoDB { public override void ConfigureServices(IServiceCollection services) { - //TODO: ServiceCollection extensions to register mongodbcontext! - services.TryAddTransient(typeof(IMongoDatabaseProvider<>), typeof(UnitOfWorkMongoDatabaseProvider<>)); services.AddAssemblyOf(); } diff --git a/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/DependencyInjection/EfCoreRepositoryRegistrar.cs b/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/DependencyInjection/EfCoreRepositoryRegistrar.cs index 9ca7e20106..0ab8861ff5 100644 --- a/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/DependencyInjection/EfCoreRepositoryRegistrar.cs +++ b/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/DependencyInjection/EfCoreRepositoryRegistrar.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq; using Volo.Abp.Domain.Repositories; using Volo.Abp.Domain.Repositories.MongoDB; @@ -15,7 +16,7 @@ namespace Volo.Abp.MongoDB.DependencyInjection protected override IEnumerable GetEntityTypes(Type dbContextType) { var mongoDbContext = (AbpMongoDbContext)Activator.CreateInstance(dbContextType); - return mongoDbContext.GetEntityCollectionTypes(); + return mongoDbContext.GetMappings().Select(m => m.EntityType); } protected override Type GetRepositoryTypeForDefaultPk(Type dbContextType, Type entityType) diff --git a/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/IMongoDatabaseProvider.cs b/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/IMongoDatabaseProvider.cs index 07cee9a52e..71d5df8cf9 100644 --- a/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/IMongoDatabaseProvider.cs +++ b/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/IMongoDatabaseProvider.cs @@ -5,6 +5,8 @@ namespace Volo.Abp.MongoDB public interface IMongoDatabaseProvider where TMongoDbContext : AbpMongoDbContext { + TMongoDbContext DbContext { get; } + IMongoDatabase GetDatabase(); } } \ No newline at end of file diff --git a/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/MongoEntityMapping.cs b/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/MongoEntityMapping.cs new file mode 100644 index 0000000000..8b7c421ae9 --- /dev/null +++ b/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/MongoEntityMapping.cs @@ -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; + } + } +} \ No newline at end of file diff --git a/src/Volo.Abp.MongoDB/Volo/Abp/Uow/MongoDB/UnitOfWorkMongoDatabaseProvider.cs b/src/Volo.Abp.MongoDB/Volo/Abp/Uow/MongoDB/UnitOfWorkMongoDatabaseProvider.cs index be6a76926b..5212f8550b 100644 --- a/src/Volo.Abp.MongoDB/Volo/Abp/Uow/MongoDB/UnitOfWorkMongoDatabaseProvider.cs +++ b/src/Volo.Abp.MongoDB/Volo/Abp/Uow/MongoDB/UnitOfWorkMongoDatabaseProvider.cs @@ -7,15 +7,19 @@ namespace Volo.Abp.Uow.MongoDB public class UnitOfWorkMongoDatabaseProvider : IMongoDatabaseProvider where TMongoDbContext : AbpMongoDbContext { + public TMongoDbContext DbContext { get; } + private readonly IUnitOfWorkManager _unitOfWorkManager; private readonly IConnectionStringResolver _connectionStringResolver; public UnitOfWorkMongoDatabaseProvider( IUnitOfWorkManager unitOfWorkManager, - IConnectionStringResolver connectionStringResolver) + IConnectionStringResolver connectionStringResolver, + TMongoDbContext dbContext) { _unitOfWorkManager = unitOfWorkManager; _connectionStringResolver = connectionStringResolver; + DbContext = dbContext; } public IMongoDatabase GetDatabase()