mirror of https://github.com/abpframework/abp
mongodb integration #52
parent
86a352c605
commit
c39deb10e6
@ -0,0 +1,20 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup>
|
||||
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
|
||||
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.Props" Condition="'$(VSToolsPath)' != ''" />
|
||||
<PropertyGroup Label="Globals">
|
||||
<ProjectGuid>63244dc7-34be-44e1-bf6f-f2672e59af36</ProjectGuid>
|
||||
<RootNamespace>
|
||||
</RootNamespace>
|
||||
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">.\obj</BaseIntermediateOutputPath>
|
||||
<OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath>
|
||||
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup>
|
||||
<SchemaVersion>2.0</SchemaVersion>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.targets" Condition="'$(VSToolsPath)' != ''" />
|
||||
</Project>
|
||||
@ -0,0 +1,22 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Volo.Abp.Data;
|
||||
using Volo.Abp.MongoDB;
|
||||
|
||||
namespace AbpDesk.Blogging
|
||||
{
|
||||
[ConnectionStringName(ConnectionStringName)]
|
||||
public class AbpDeskMongoDbContext : AbpMongoDbContext
|
||||
{
|
||||
public const string ConnectionStringName = "AbpDeskMongoBlog";
|
||||
|
||||
private static readonly Type[] EntityCollectionTypes = {
|
||||
typeof(BlogPost)
|
||||
};
|
||||
|
||||
public override IReadOnlyList<Type> GetEntityCollectionTypes()
|
||||
{
|
||||
return EntityCollectionTypes;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,10 @@
|
||||
using Volo.Abp.Data;
|
||||
using Volo.Abp.Domain.Repositories;
|
||||
|
||||
namespace Microsoft.Extensions.DependencyInjection
|
||||
{
|
||||
public class AbpDbContextRegistrationOptions : CommonDbContextRegistrationOptions, IAbpDbContextRegistrationOptionsBuilder
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
@ -1,57 +0,0 @@
|
||||
using System;
|
||||
using Volo.Abp;
|
||||
using Volo.Abp.Domain.Entities;
|
||||
using Volo.Abp.Domain.Repositories;
|
||||
using Volo.Abp.Reflection;
|
||||
|
||||
namespace Microsoft.Extensions.DependencyInjection
|
||||
{
|
||||
public class AddAbpDbContextOptions
|
||||
{
|
||||
internal RepositoryRegistrationOptions RepositoryOptions { get; set; }
|
||||
|
||||
public AddAbpDbContextOptions()
|
||||
{
|
||||
RepositoryOptions = new RepositoryRegistrationOptions();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Registers default repositories for this DbContext.
|
||||
/// </summary>
|
||||
/// <param name="includeAllEntities">
|
||||
/// Registers repositories only for aggregate root entities by default.
|
||||
/// set <see cref="includeAllEntities"/> to true to include all entities.
|
||||
/// </param>
|
||||
public void WithDefaultRepositories(bool includeAllEntities = false)
|
||||
{
|
||||
RepositoryOptions.RegisterDefaultRepositories = true;
|
||||
RepositoryOptions.IncludeAllEntitiesForDefaultRepositories = includeAllEntities;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Registers custom repository for a specific entity.
|
||||
/// Custom repositories overrides default repositories.
|
||||
/// </summary>
|
||||
/// <typeparam name="TEntity">Entity type</typeparam>
|
||||
/// <typeparam name="TRepository">Repository type</typeparam>
|
||||
public void WithCustomRepository<TEntity, TRepository>()
|
||||
{
|
||||
WithCustomRepository(typeof(TEntity), typeof(TRepository));
|
||||
}
|
||||
|
||||
private void WithCustomRepository(Type entityType, Type repositoryType)
|
||||
{
|
||||
if (!ReflectionHelper.IsAssignableToGenericType(entityType, typeof(IEntity<>)))
|
||||
{
|
||||
throw new AbpException($"Given entityType is not an entity: {entityType.AssemblyQualifiedName}. It must implement {typeof(IEntity<>).AssemblyQualifiedName}.");
|
||||
}
|
||||
|
||||
if (!ReflectionHelper.IsAssignableToGenericType(repositoryType, typeof(IRepository<,>)))
|
||||
{
|
||||
throw new AbpException($"Given repositoryType is not a repository: {entityType.AssemblyQualifiedName}. It must implement {typeof(IRepository<,>).AssemblyQualifiedName}.");
|
||||
}
|
||||
|
||||
RepositoryOptions.CustomRepositories[entityType] = repositoryType;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,10 @@
|
||||
using Volo.Abp.Data;
|
||||
using Volo.Abp.Domain.Repositories;
|
||||
|
||||
namespace Microsoft.Extensions.DependencyInjection
|
||||
{
|
||||
public interface IAbpDbContextRegistrationOptionsBuilder : ICommonDbContextRegistrationOptionsBuilder
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
@ -1,19 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Microsoft.Extensions.DependencyInjection
|
||||
{
|
||||
internal class RepositoryRegistrationOptions
|
||||
{
|
||||
public bool RegisterDefaultRepositories { get; set; }
|
||||
|
||||
public bool IncludeAllEntitiesForDefaultRepositories { get; set; }
|
||||
|
||||
public Dictionary<Type, Type> CustomRepositories { get; set; }
|
||||
|
||||
public RepositoryRegistrationOptions()
|
||||
{
|
||||
CustomRepositories = new Dictionary<Type, Type>();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,10 @@
|
||||
using Volo.Abp.Data;
|
||||
using Volo.Abp.Domain.Repositories;
|
||||
|
||||
namespace Microsoft.Extensions.DependencyInjection
|
||||
{
|
||||
public interface IMongoDbContextRegistrationOptionsBuilder : ICommonDbContextRegistrationOptionsBuilder
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,10 @@
|
||||
using Volo.Abp.Data;
|
||||
using Volo.Abp.Domain.Repositories;
|
||||
|
||||
namespace Microsoft.Extensions.DependencyInjection
|
||||
{
|
||||
public class MongoDbContextRegistrationOptions : CommonDbContextRegistrationOptions, IMongoDbContextRegistrationOptionsBuilder
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Volo.Abp.MongoDB
|
||||
{
|
||||
public abstract class AbpMongoDbContext
|
||||
{
|
||||
private static readonly Type[] EmptyTypeList = new Type[0];
|
||||
|
||||
public virtual IReadOnlyList<Type> GetEntityCollectionTypes()
|
||||
{
|
||||
return EmptyTypeList;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,69 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Volo.Abp;
|
||||
using Volo.Abp.Domain.Entities;
|
||||
using Volo.Abp.Domain.Repositories;
|
||||
using Volo.Abp.Reflection;
|
||||
|
||||
namespace Microsoft.Extensions.DependencyInjection
|
||||
{
|
||||
public class CommonDbContextRegistrationOptions : ICommonDbContextRegistrationOptionsBuilder
|
||||
{
|
||||
public bool RegisterDefaultRepositories { get; set; }
|
||||
|
||||
public bool IncludeAllEntitiesForDefaultRepositories { get; set; }
|
||||
|
||||
public Dictionary<Type, Type> CustomRepositories { get; set; }
|
||||
|
||||
public CommonDbContextRegistrationOptions()
|
||||
{
|
||||
CustomRepositories = new Dictionary<Type, Type>();
|
||||
}
|
||||
|
||||
public void WithDefaultRepositories(bool includeAllEntities = false)
|
||||
{
|
||||
RegisterDefaultRepositories = true;
|
||||
IncludeAllEntitiesForDefaultRepositories = includeAllEntities;
|
||||
}
|
||||
|
||||
public void WithCustomRepository<TEntity, TRepository>()
|
||||
{
|
||||
WithCustomRepository(typeof(TEntity), typeof(TRepository));
|
||||
}
|
||||
|
||||
private void WithCustomRepository(Type entityType, Type repositoryType)
|
||||
{
|
||||
if (!ReflectionHelper.IsAssignableToGenericType(entityType, typeof(IEntity<>)))
|
||||
{
|
||||
throw new AbpException($"Given entityType is not an entity: {entityType.AssemblyQualifiedName}. It must implement {typeof(IEntity<>).AssemblyQualifiedName}.");
|
||||
}
|
||||
|
||||
if (!ReflectionHelper.IsAssignableToGenericType(repositoryType, typeof(IRepository<,>)))
|
||||
{
|
||||
throw new AbpException($"Given repositoryType is not a repository: {entityType.AssemblyQualifiedName}. It must implement {typeof(IRepository<,>).AssemblyQualifiedName}.");
|
||||
}
|
||||
|
||||
CustomRepositories[entityType] = repositoryType;
|
||||
}
|
||||
|
||||
public bool ShouldRegisterDefaultRepositoryFor(Type entityType)
|
||||
{
|
||||
if (!RegisterDefaultRepositories)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (CustomRepositories.ContainsKey(entityType))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!IncludeAllEntitiesForDefaultRepositories && !ReflectionHelper.IsAssignableToGenericType(entityType, typeof(IAggregateRoot<>)))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,22 @@
|
||||
namespace Microsoft.Extensions.DependencyInjection
|
||||
{
|
||||
public interface ICommonDbContextRegistrationOptionsBuilder
|
||||
{
|
||||
/// <summary>
|
||||
/// Registers default repositories for this DbContext.
|
||||
/// </summary>
|
||||
/// <param name="includeAllEntities">
|
||||
/// Registers repositories only for aggregate root entities by default.
|
||||
/// set <see cref="includeAllEntities"/> to true to include all entities.
|
||||
/// </param>
|
||||
void WithDefaultRepositories(bool includeAllEntities = false);
|
||||
|
||||
/// <summary>
|
||||
/// Registers custom repository for a specific entity.
|
||||
/// Custom repositories overrides default repositories.
|
||||
/// </summary>
|
||||
/// <typeparam name="TEntity">Entity type</typeparam>
|
||||
/// <typeparam name="TRepository">Repository type</typeparam>
|
||||
void WithCustomRepository<TEntity, TRepository>();
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,10 @@
|
||||
using JetBrains.Annotations;
|
||||
|
||||
namespace Volo.Abp.Uow
|
||||
{
|
||||
public interface IUnitOfWorkAccessor
|
||||
{
|
||||
[CanBeNull]
|
||||
IUnitOfWork UnitOfWork { get; }
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue