mirror of https://github.com/abpframework/abp.git
15 changed files with 172 additions and 17 deletions
@ -0,0 +1,17 @@ |
|||
using Microsoft.EntityFrameworkCore; |
|||
using Microsoft.EntityFrameworkCore.Infrastructure; |
|||
|
|||
namespace AbpDesk.EntityFrameworkCore |
|||
{ |
|||
/* This class is needed for EF Core command line tooling */ |
|||
|
|||
public class AbpDeskDefaultDbContextFactory : IDbContextFactory<AbpDeskDbContext> |
|||
{ |
|||
public AbpDeskDbContext Create(DbContextFactoryOptions options) |
|||
{ |
|||
var builder = new DbContextOptionsBuilder<AbpDeskDbContext>(); |
|||
builder.UseSqlServer("Server=localhost;Database=AbpDesk;Trusted_Connection=True;"); |
|||
return new AbpDeskDbContext(builder.Options); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,30 @@ |
|||
using Microsoft.Extensions.DependencyInjection.Extensions; |
|||
using Volo.Abp.Domain.Entities; |
|||
using Volo.Abp.Domain.Repositories; |
|||
using Volo.Abp.EntityFrameworkCore; |
|||
using Volo.Abp.Repositories.EntityFrameworkCore; |
|||
|
|||
namespace Microsoft.Extensions.DependencyInjection |
|||
{ |
|||
public static class AbpEfCoreServiceCollectionExtensions |
|||
{ |
|||
public static IServiceCollection AddDefaultEfCoreRepositories<TDbContext>(this IServiceCollection services) |
|||
where TDbContext : AbpDbContext<TDbContext> |
|||
{ |
|||
//TODO: Add options to use a provided type as default repository.
|
|||
|
|||
var dbContextType = typeof(TDbContext); |
|||
|
|||
foreach (var entityType in DbContextHelper.GetEntityTypes(dbContextType)) |
|||
{ |
|||
var primaryKeyType = EntityHelper.GetPrimaryKeyType(entityType); |
|||
var repositoryInterfaceType = typeof(IRepository<,>).MakeGenericType(entityType, primaryKeyType); |
|||
var repositoryImplementationType = typeof(EfCoreRepository<,,>).MakeGenericType(dbContextType, entityType, primaryKeyType); |
|||
|
|||
services.TryAddTransient(repositoryInterfaceType, repositoryImplementationType); |
|||
} |
|||
|
|||
return services; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,23 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Reflection; |
|||
using Microsoft.EntityFrameworkCore; |
|||
using Volo.Abp.Domain.Entities; |
|||
using Volo.Abp.Reflection; |
|||
|
|||
namespace Volo.Abp.EntityFrameworkCore |
|||
{ |
|||
internal static class DbContextHelper |
|||
{ |
|||
public static IEnumerable<Type> GetEntityTypes(Type dbContextType) |
|||
{ |
|||
return |
|||
from property in dbContextType.GetTypeInfo().GetProperties(BindingFlags.Public | BindingFlags.Instance) |
|||
where |
|||
ReflectionHelper.IsAssignableToGenericType(property.PropertyType, typeof(DbSet<>)) && |
|||
ReflectionHelper.IsAssignableToGenericType(property.PropertyType.GenericTypeArguments[0], typeof(IEntity<>)) |
|||
select property.PropertyType.GenericTypeArguments[0]; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,38 @@ |
|||
using System; |
|||
using System.Reflection; |
|||
using Volo.Abp.Reflection; |
|||
|
|||
namespace Volo.Abp.Domain.Entities |
|||
{ |
|||
/// <summary>
|
|||
/// Some helper methods for entities.
|
|||
/// </summary>
|
|||
public static class EntityHelper |
|||
{ |
|||
public static bool IsEntity(Type type) |
|||
{ |
|||
return ReflectionHelper.IsAssignableToGenericType(type, typeof (IEntity<>)); |
|||
} |
|||
|
|||
public static Type GetPrimaryKeyType<TEntity>() |
|||
{ |
|||
return GetPrimaryKeyType(typeof (TEntity)); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Gets primary key type of given entity type
|
|||
/// </summary>
|
|||
public static Type GetPrimaryKeyType(Type entityType) |
|||
{ |
|||
foreach (var interfaceType in entityType.GetTypeInfo().GetInterfaces()) |
|||
{ |
|||
if (interfaceType.GetTypeInfo().IsGenericType && interfaceType.GetGenericTypeDefinition() == typeof (IEntity<>)) |
|||
{ |
|||
return interfaceType.GenericTypeArguments[0]; |
|||
} |
|||
} |
|||
|
|||
throw new AbpException("Can not find primary key type of given entity type: " + entityType + ". Be sure that this entity type implements " + typeof(IEntity<>).AssemblyQualifiedName); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,38 @@ |
|||
using System; |
|||
using System.Reflection; |
|||
|
|||
namespace Volo.Abp.Reflection |
|||
{ |
|||
public static class ReflectionHelper |
|||
{ |
|||
/// <summary>
|
|||
/// Checks whether <paramref name="givenType"/> implements/inherits <paramref name="genericType"/>.
|
|||
/// </summary>
|
|||
/// <param name="givenType">Type to check</param>
|
|||
/// <param name="genericType">Generic type</param>
|
|||
public static bool IsAssignableToGenericType(Type givenType, Type genericType) |
|||
{ |
|||
var givenTypeInfo = givenType.GetTypeInfo(); |
|||
|
|||
if (givenTypeInfo.IsGenericType && givenType.GetGenericTypeDefinition() == genericType) |
|||
{ |
|||
return true; |
|||
} |
|||
|
|||
foreach (var interfaceType in givenTypeInfo.GetInterfaces()) |
|||
{ |
|||
if (interfaceType.GetTypeInfo().IsGenericType && interfaceType.GetGenericTypeDefinition() == genericType) |
|||
{ |
|||
return true; |
|||
} |
|||
} |
|||
|
|||
if (givenTypeInfo.BaseType == null) |
|||
{ |
|||
return false; |
|||
} |
|||
|
|||
return IsAssignableToGenericType(givenTypeInfo.BaseType, genericType); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue