Browse Source

Provide an option to set base repository classes, instead of defaults.

pull/81/head
Halil İbrahim Kalkan 10 years ago
parent
commit
0ef5daeb0e
  1. 2
      src/Volo.Abp/Microsoft/Extensions/DependencyInjection/ServiceCollectionRepositoryExtensions.cs
  2. 20
      src/Volo.Abp/Volo/Abp/Data/CommonDbContextRegistrationOptions.cs
  3. 10
      src/Volo.Abp/Volo/Abp/Data/ICommonDbContextRegistrationOptionsBuilder.cs
  4. 20
      src/Volo.Abp/Volo/Abp/Domain/Repositories/RepositoryRegistrarBase.cs
  5. 41
      test/Volo.Abp.Tests/Volo/Abp/Domain/Repositories/RepositoryRegistration_Tests.cs

2
src/Volo.Abp/Microsoft/Extensions/DependencyInjection/ServiceCollectionRepositoryExtensions.cs

@ -41,7 +41,7 @@ namespace Microsoft.Extensions.DependencyInjection
private static bool BothSupportsDefaultPrimaryKey(Type entityType, Type repositoryImplementationType) private static bool BothSupportsDefaultPrimaryKey(Type entityType, Type repositoryImplementationType)
{ {
return typeof(IEntity).GetTypeInfo().IsAssignableFrom(entityType) && return typeof(IEntity<string>).GetTypeInfo().IsAssignableFrom(entityType) &&
ReflectionHelper.IsAssignableToGenericType(repositoryImplementationType, typeof(IRepository<>)); ReflectionHelper.IsAssignableToGenericType(repositoryImplementationType, typeof(IRepository<>));
} }
} }

20
src/Volo.Abp/Volo/Abp/Data/CommonDbContextRegistrationOptions.cs

@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using JetBrains.Annotations;
using Volo.Abp.Domain.Entities; using Volo.Abp.Domain.Entities;
using Volo.Abp.Domain.Repositories; using Volo.Abp.Domain.Repositories;
using Volo.Abp.Reflection; using Volo.Abp.Reflection;
@ -8,7 +9,11 @@ namespace Volo.Abp.Data
{ {
public class CommonDbContextRegistrationOptions : ICommonDbContextRegistrationOptionsBuilder public class CommonDbContextRegistrationOptions : ICommonDbContextRegistrationOptionsBuilder
{ {
//TODO: Provide an option to set base repository classes, instead of defaults. public bool SpecifiedDefaultRepositoryTypes => DefaultRepositoryImplementationType != null && DefaultRepositoryImplementationTypeWithDefaultPrimaryKey != null;
public Type DefaultRepositoryImplementationType { get; private set; }
public Type DefaultRepositoryImplementationTypeWithDefaultPrimaryKey { get; private set; }
public bool RegisterDefaultRepositories { get; private set; } public bool RegisterDefaultRepositories { get; private set; }
@ -25,12 +30,25 @@ namespace Volo.Abp.Data
{ {
RegisterDefaultRepositories = true; RegisterDefaultRepositories = true;
IncludeAllEntitiesForDefaultRepositories = includeAllEntities; IncludeAllEntitiesForDefaultRepositories = includeAllEntities;
return this; return this;
} }
public ICommonDbContextRegistrationOptionsBuilder WithCustomRepository<TEntity, TRepository>() public ICommonDbContextRegistrationOptionsBuilder WithCustomRepository<TEntity, TRepository>()
{ {
WithCustomRepository(typeof(TEntity), typeof(TRepository)); WithCustomRepository(typeof(TEntity), typeof(TRepository));
return this;
}
public ICommonDbContextRegistrationOptionsBuilder WithDefaultRepositoryClasses([NotNull] Type repositoryImplementationType, [NotNull] Type repositoryImplementationTypeWithDefaultPrimaryKey)
{
Check.NotNull(repositoryImplementationType, nameof(repositoryImplementationType));
Check.NotNull(repositoryImplementationTypeWithDefaultPrimaryKey, nameof(repositoryImplementationTypeWithDefaultPrimaryKey));
DefaultRepositoryImplementationType = repositoryImplementationType;
DefaultRepositoryImplementationTypeWithDefaultPrimaryKey = repositoryImplementationTypeWithDefaultPrimaryKey;
return this; return this;
} }

10
src/Volo.Abp/Volo/Abp/Data/ICommonDbContextRegistrationOptionsBuilder.cs

@ -1,3 +1,5 @@
using System;
namespace Volo.Abp.Data namespace Volo.Abp.Data
{ {
public interface ICommonDbContextRegistrationOptionsBuilder public interface ICommonDbContextRegistrationOptionsBuilder
@ -18,5 +20,13 @@ namespace Volo.Abp.Data
/// <typeparam name="TEntity">Entity type</typeparam> /// <typeparam name="TEntity">Entity type</typeparam>
/// <typeparam name="TRepository">Repository type</typeparam> /// <typeparam name="TRepository">Repository type</typeparam>
ICommonDbContextRegistrationOptionsBuilder WithCustomRepository<TEntity, TRepository>(); ICommonDbContextRegistrationOptionsBuilder WithCustomRepository<TEntity, TRepository>();
/// <summary>
/// Uses given class(es) for default repositories.
/// </summary>
/// <param name="repositoryImplementationType">Repository implementation type</param>
/// <param name="repositoryImplementationTypeWithDefaultPrimaryKey">Repository implementation type for default primary key type (<see cref="string"/>)</param>
/// <returns></returns>
ICommonDbContextRegistrationOptionsBuilder WithDefaultRepositoryClasses(Type repositoryImplementationType, Type repositoryImplementationTypeWithDefaultPrimaryKey);
} }
} }

20
src/Volo.Abp/Volo/Abp/Domain/Repositories/RepositoryRegistrarBase.cs

@ -1,6 +1,5 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Reflection;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.Data; using Volo.Abp.Data;
using Volo.Abp.Domain.Entities; using Volo.Abp.Domain.Entities;
@ -45,9 +44,22 @@ namespace Volo.Abp.Domain.Repositories
protected void RegisterDefaultRepository(IServiceCollection services, Type dbContextType, Type entityType) protected void RegisterDefaultRepository(IServiceCollection services, Type dbContextType, Type entityType)
{ {
var repositoryImplementationType = typeof(IEntity).GetTypeInfo().IsAssignableFrom(entityType) var primaryKeyType = EntityHelper.GetPrimaryKeyType(entityType);
? GetRepositoryTypeForDefaultPk(dbContextType, entityType) var isDefaultPrimaryKey = primaryKeyType == typeof(string);
: GetRepositoryType(dbContextType, entityType, EntityHelper.GetPrimaryKeyType(entityType));
Type repositoryImplementationType;
if (Options.SpecifiedDefaultRepositoryTypes)
{
repositoryImplementationType = isDefaultPrimaryKey
? Options.DefaultRepositoryImplementationTypeWithDefaultPrimaryKey.MakeGenericType(entityType)
: Options.DefaultRepositoryImplementationType.MakeGenericType(entityType, primaryKeyType);
}
else
{
repositoryImplementationType = isDefaultPrimaryKey
? GetRepositoryTypeForDefaultPk(dbContextType, entityType)
: GetRepositoryType(dbContextType, entityType, primaryKeyType);
}
services.AddDefaultRepository(entityType, repositoryImplementationType); services.AddDefaultRepository(entityType, repositoryImplementationType);
} }

41
test/Volo.Abp.Tests/Volo/Abp/Domain/Repositories/RepositoryRegistration_Tests.cs

@ -59,8 +59,9 @@ namespace Volo.Abp.Domain.Repositories
var services = new ServiceCollection(); var services = new ServiceCollection();
var options = new CommonDbContextRegistrationOptions(); var options = new CommonDbContextRegistrationOptions();
options.WithDefaultRepositories(true); options
options.WithCustomRepository<MyTestAggregateRootWithDefaultPk, MyTestAggregateRootWithDefaultPkCustomRepository>(); .WithDefaultRepositories(true)
.WithCustomRepository<MyTestAggregateRootWithDefaultPk, MyTestAggregateRootWithDefaultPkCustomRepository>();
//Act //Act
@ -73,6 +74,29 @@ namespace Volo.Abp.Domain.Repositories
services.ShouldContainTransient(typeof(IRepository<MyTestEntityWithCustomPk, int>), typeof(MyTestDefaultRepository<MyTestEntityWithCustomPk, int>)); services.ShouldContainTransient(typeof(IRepository<MyTestEntityWithCustomPk, int>), typeof(MyTestDefaultRepository<MyTestEntityWithCustomPk, int>));
} }
[Fact]
public void Should_Register_Default_Repositories_With_Custom_Base()
{
//Arrange
var services = new ServiceCollection();
var options = new CommonDbContextRegistrationOptions();
options
.WithDefaultRepositories(true)
.WithDefaultRepositoryClasses(typeof(MyTestCustomBaseRepository<,>), typeof(MyTestCustomBaseRepository<>));
//Act
new MyTestRepositoryRegistrar(options).AddRepositories(services, typeof(MyFakeDbContext));
//Assert
services.ShouldContainTransient(typeof(IRepository<MyTestAggregateRootWithDefaultPk>), typeof(MyTestCustomBaseRepository<MyTestAggregateRootWithDefaultPk>));
services.ShouldContainTransient(typeof(IRepository<MyTestAggregateRootWithDefaultPk, string>), typeof(MyTestCustomBaseRepository<MyTestAggregateRootWithDefaultPk>));
services.ShouldContainTransient(typeof(IRepository<MyTestEntityWithCustomPk, int>), typeof(MyTestCustomBaseRepository<MyTestEntityWithCustomPk, int>));
}
public class MyTestRepositoryRegistrar : RepositoryRegistrarBase<CommonDbContextRegistrationOptions> public class MyTestRepositoryRegistrar : RepositoryRegistrarBase<CommonDbContextRegistrationOptions>
{ {
public MyTestRepositoryRegistrar(CommonDbContextRegistrationOptions options) public MyTestRepositoryRegistrar(CommonDbContextRegistrationOptions options)
@ -146,5 +170,18 @@ namespace Volo.Abp.Domain.Repositories
{ {
} }
public class MyTestCustomBaseRepository<TEntity> : MyTestCustomBaseRepository<TEntity, string>, IRepository<TEntity>
where TEntity : class, IEntity<string>
{
}
public class MyTestCustomBaseRepository<TEntity, TPrimaryKey> : MyTestDefaultRepository<TEntity, TPrimaryKey>
where TEntity : class, IEntity<TPrimaryKey>
{
}
} }
} }

Loading…
Cancel
Save