Browse Source

Mongodb configuration to map entities to collections.

pull/81/head
Halil İbrahim Kalkan 10 years ago
parent
commit
1828593051
  1. 10
      src/AbpDesk/AbpDesk.ConsoleDemo/AbpDesk/ConsoleDemo/BlogPostLister.cs
  2. 7
      src/AbpDesk/AbpDesk.MongoBlog/AbpDesk/Blogging/AbpDeskMongoDbContext.cs
  3. 3
      src/Volo.Abp.MongoDB/Microsoft/Extensions/DependencyInjection/AbpMongoDbServiceCollectionExtensions.cs
  4. 5
      src/Volo.Abp.MongoDB/Volo/Abp/Domain/Repositories/MongoDB/MongoDbRepository.cs
  5. 33
      src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/AbpMongoDbContext.cs
  6. 2
      src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/AbpMongoDbModule.cs
  7. 3
      src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/DependencyInjection/EfCoreRepositoryRegistrar.cs
  8. 2
      src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/IMongoDatabaseProvider.cs
  9. 17
      src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/MongoEntityMapping.cs
  10. 6
      src/Volo.Abp.MongoDB/Volo/Abp/Uow/MongoDB/UnitOfWorkMongoDatabaseProvider.cs

10
src/AbpDesk/AbpDesk.ConsoleDemo/AbpDesk/ConsoleDemo/BlogPostLister.cs

@ -2,6 +2,7 @@
using System.Globalization; using System.Globalization;
using AbpDesk.Blogging; using AbpDesk.Blogging;
using Volo.Abp.Domain.Repositories; using Volo.Abp.Domain.Repositories;
using Volo.Abp.Guids;
using Volo.Abp.Uow; using Volo.Abp.Uow;
using Volo.DependencyInjection; using Volo.DependencyInjection;
@ -11,11 +12,16 @@ namespace AbpDesk.ConsoleDemo
{ {
private readonly IQueryableRepository<BlogPost> _blogPostRepository; private readonly IQueryableRepository<BlogPost> _blogPostRepository;
private readonly IUnitOfWorkManager _unitOfWorkManager; private readonly IUnitOfWorkManager _unitOfWorkManager;
private readonly IGuidGenerator _guidGenerator;
public BlogPostLister(IQueryableRepository<BlogPost> blogPostRepository, IUnitOfWorkManager unitOfWorkManager) public BlogPostLister(
IQueryableRepository<BlogPost> blogPostRepository,
IUnitOfWorkManager unitOfWorkManager,
IGuidGenerator guidGenerator)
{ {
_blogPostRepository = blogPostRepository; _blogPostRepository = blogPostRepository;
_unitOfWorkManager = unitOfWorkManager; _unitOfWorkManager = unitOfWorkManager;
_guidGenerator = guidGenerator;
} }
public void List() 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))); //blog.Comments.Add(new BlogPostComment("@john", "good post! " + DateTime.Now.ToString(CultureInfo.InvariantCulture), star: (byte)RandomHelper.GetRandom(1, 6)));
//_blogPostRepository.Update(blog); //_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) foreach (var blogPost in _blogPostRepository)
{ {

7
src/AbpDesk/AbpDesk.MongoBlog/AbpDesk/Blogging/AbpDeskMongoDbContext.cs

@ -1,4 +1,3 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
using Volo.Abp.Data; using Volo.Abp.Data;
using Volo.Abp.MongoDB; using Volo.Abp.MongoDB;
@ -10,11 +9,11 @@ namespace AbpDesk.Blogging
{ {
public const string ConnectionStringName = "AbpDeskMongoBlog"; public const string ConnectionStringName = "AbpDeskMongoBlog";
private static readonly Type[] EntityCollectionTypes = { private static readonly MongoEntityMapping[] EntityCollectionTypes = {
typeof(BlogPost) new MongoEntityMapping(typeof(BlogPost), "BlogPosts")
}; };
public override IReadOnlyList<Type> GetEntityCollectionTypes() public override IReadOnlyList<MongoEntityMapping> GetMappings()
{ {
return EntityCollectionTypes; return EntityCollectionTypes;
} }

3
src/Volo.Abp.MongoDB/Microsoft/Extensions/DependencyInjection/AbpMongoDbServiceCollectionExtensions.cs

@ -1,5 +1,4 @@
using System; using System;
using Volo.Abp.Domain.Repositories.MongoDB;
using Volo.Abp.MongoDB; using Volo.Abp.MongoDB;
using Volo.Abp.MongoDB.DependencyInjection; using Volo.Abp.MongoDB.DependencyInjection;
@ -13,6 +12,8 @@ namespace Microsoft.Extensions.DependencyInjection
var options = new MongoDbContextRegistrationOptions(); var options = new MongoDbContextRegistrationOptions();
optionsBuilder?.Invoke(options); optionsBuilder?.Invoke(options);
services.AddSingleton<TMongoDbContext>();
new MongoDbRepositoryRegistrar(options) new MongoDbRepositoryRegistrar(options)
.AddRepositories(services, typeof(TMongoDbContext)); .AddRepositories(services, typeof(TMongoDbContext));

5
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 TMongoDbContext : AbpMongoDbContext
where TEntity : class, IEntity<TPrimaryKey> where TEntity : class, IEntity<TPrimaryKey>
{ {
public virtual string CollectionName public virtual string CollectionName => DatabaseProvider.DbContext.GetCollectionName<TEntity>();
{
get { return typeof(TEntity).Name; } //TODO: a better naming, or a way of easily overriding it?
}
public virtual IMongoCollection<TEntity> Collection => Database.GetCollection<TEntity>(CollectionName); public virtual IMongoCollection<TEntity> Collection => Database.GetCollection<TEntity>(CollectionName);

33
src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/AbpMongoDbContext.cs

@ -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;
}
} }
} }

2
src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/AbpMongoDbModule.cs

@ -9,8 +9,6 @@ namespace Volo.Abp.MongoDB
{ {
public override void ConfigureServices(IServiceCollection services) public override void ConfigureServices(IServiceCollection services)
{ {
//TODO: ServiceCollection extensions to register mongodbcontext!
services.TryAddTransient(typeof(IMongoDatabaseProvider<>), typeof(UnitOfWorkMongoDatabaseProvider<>)); services.TryAddTransient(typeof(IMongoDatabaseProvider<>), typeof(UnitOfWorkMongoDatabaseProvider<>));
services.AddAssemblyOf<AbpMongoDbModule>(); services.AddAssemblyOf<AbpMongoDbModule>();
} }

3
src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/DependencyInjection/EfCoreRepositoryRegistrar.cs

@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using Volo.Abp.Domain.Repositories; using Volo.Abp.Domain.Repositories;
using Volo.Abp.Domain.Repositories.MongoDB; using Volo.Abp.Domain.Repositories.MongoDB;
@ -15,7 +16,7 @@ namespace Volo.Abp.MongoDB.DependencyInjection
protected override IEnumerable<Type> GetEntityTypes(Type dbContextType) protected override IEnumerable<Type> GetEntityTypes(Type dbContextType)
{ {
var mongoDbContext = (AbpMongoDbContext)Activator.CreateInstance(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) protected override Type GetRepositoryTypeForDefaultPk(Type dbContextType, Type entityType)

2
src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/IMongoDatabaseProvider.cs

@ -5,6 +5,8 @@ namespace Volo.Abp.MongoDB
public interface IMongoDatabaseProvider<TMongoDbContext> public interface IMongoDatabaseProvider<TMongoDbContext>
where TMongoDbContext : AbpMongoDbContext where TMongoDbContext : AbpMongoDbContext
{ {
TMongoDbContext DbContext { get; }
IMongoDatabase GetDatabase(); IMongoDatabase GetDatabase();
} }
} }

17
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;
}
}
}

6
src/Volo.Abp.MongoDB/Volo/Abp/Uow/MongoDB/UnitOfWorkMongoDatabaseProvider.cs

@ -7,15 +7,19 @@ namespace Volo.Abp.Uow.MongoDB
public class UnitOfWorkMongoDatabaseProvider<TMongoDbContext> : IMongoDatabaseProvider<TMongoDbContext> public class UnitOfWorkMongoDatabaseProvider<TMongoDbContext> : IMongoDatabaseProvider<TMongoDbContext>
where TMongoDbContext : AbpMongoDbContext where TMongoDbContext : AbpMongoDbContext
{ {
public TMongoDbContext DbContext { get; }
private readonly IUnitOfWorkManager _unitOfWorkManager; private readonly IUnitOfWorkManager _unitOfWorkManager;
private readonly IConnectionStringResolver _connectionStringResolver; private readonly IConnectionStringResolver _connectionStringResolver;
public UnitOfWorkMongoDatabaseProvider( public UnitOfWorkMongoDatabaseProvider(
IUnitOfWorkManager unitOfWorkManager, IUnitOfWorkManager unitOfWorkManager,
IConnectionStringResolver connectionStringResolver) IConnectionStringResolver connectionStringResolver,
TMongoDbContext dbContext)
{ {
_unitOfWorkManager = unitOfWorkManager; _unitOfWorkManager = unitOfWorkManager;
_connectionStringResolver = connectionStringResolver; _connectionStringResolver = connectionStringResolver;
DbContext = dbContext;
} }
public IMongoDatabase GetDatabase() public IMongoDatabase GetDatabase()

Loading…
Cancel
Save