mirror of https://github.com/abpframework/abp.git
13 changed files with 217 additions and 22 deletions
@ -0,0 +1,10 @@ |
|||
using System.ComponentModel.DataAnnotations; |
|||
|
|||
namespace Volo.Abp.Identity |
|||
{ |
|||
public class UpdateIdentityUserRolesDto |
|||
{ |
|||
[Required] |
|||
public string[] RoleNames { get; set; } |
|||
} |
|||
} |
|||
@ -0,0 +1,25 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq.Expressions; |
|||
using System.Threading; |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp.Domain.Entities; |
|||
|
|||
namespace Volo.Abp.Domain.Repositories |
|||
{ |
|||
public interface ISupportsExplicitLoading<TEntity, TPrimaryKey> |
|||
where TEntity : class, IEntity<TPrimaryKey> |
|||
{ |
|||
Task EnsureCollectionLoadedAsync<TProperty>( |
|||
TEntity entity, |
|||
Expression<Func<TEntity, IEnumerable<TProperty>>> propertyExpression, |
|||
CancellationToken cancellationToken) |
|||
where TProperty : class; |
|||
|
|||
Task EnsurePropertyLoadedAsync<TProperty>( |
|||
TEntity entity, |
|||
Expression<Func<TEntity, TProperty>> propertyExpression, |
|||
CancellationToken cancellationToken) |
|||
where TProperty : class; |
|||
} |
|||
} |
|||
@ -0,0 +1,68 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq.Expressions; |
|||
using System.Threading; |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp.Domain.Entities; |
|||
using Volo.Abp.DynamicProxy; |
|||
using Volo.Abp.Threading; |
|||
|
|||
namespace Volo.Abp.Domain.Repositories |
|||
{ |
|||
public static class RepositoryExtensions |
|||
{ |
|||
public static async Task EnsureCollectionLoadedAsync<TEntity, TPrimaryKey, TProperty>( |
|||
this IRepository<TEntity, TPrimaryKey> repository, |
|||
TEntity entity, |
|||
Expression<Func<TEntity, IEnumerable<TProperty>>> propertyExpression, |
|||
CancellationToken cancellationToken = default(CancellationToken) |
|||
) |
|||
where TEntity : class, IEntity<TPrimaryKey> |
|||
where TProperty : class |
|||
{ |
|||
var repo = ProxyHelper.UnProxy(repository) as ISupportsExplicitLoading<TEntity, TPrimaryKey>; |
|||
if (repo != null) |
|||
{ |
|||
await repo.EnsureCollectionLoadedAsync(entity, propertyExpression, cancellationToken); |
|||
} |
|||
} |
|||
|
|||
public static void EnsureCollectionLoaded<TEntity, TPrimaryKey, TProperty>( |
|||
this IRepository<TEntity, TPrimaryKey> repository, |
|||
TEntity entity, |
|||
Expression<Func<TEntity, IEnumerable<TProperty>>> propertyExpression |
|||
) |
|||
where TEntity : class, IEntity<TPrimaryKey> |
|||
where TProperty : class |
|||
{ |
|||
AsyncHelper.RunSync(() => repository.EnsureCollectionLoadedAsync(entity, propertyExpression)); |
|||
} |
|||
|
|||
public static async Task EnsurePropertyLoadedAsync<TEntity, TPrimaryKey, TProperty>( |
|||
this IRepository<TEntity, TPrimaryKey> repository, |
|||
TEntity entity, |
|||
Expression<Func<TEntity, TProperty>> propertyExpression, |
|||
CancellationToken cancellationToken = default(CancellationToken) |
|||
) |
|||
where TEntity : class, IEntity<TPrimaryKey> |
|||
where TProperty : class |
|||
{ |
|||
var repo = ProxyHelper.UnProxy(repository) as ISupportsExplicitLoading<TEntity, TPrimaryKey>; |
|||
if (repo != null) |
|||
{ |
|||
await repo.EnsurePropertyLoadedAsync(entity, propertyExpression, cancellationToken); |
|||
} |
|||
} |
|||
|
|||
public static void EnsurePropertyLoaded<TEntity, TPrimaryKey, TProperty>( |
|||
this IRepository<TEntity, TPrimaryKey> repository, |
|||
TEntity entity, |
|||
Expression<Func<TEntity, TProperty>> propertyExpression |
|||
) |
|||
where TEntity : class, IEntity<TPrimaryKey> |
|||
where TProperty : class |
|||
{ |
|||
AsyncHelper.RunSync(() => repository.EnsurePropertyLoadedAsync(entity, propertyExpression)); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,32 @@ |
|||
using System.Linq; |
|||
using System.Reflection; |
|||
|
|||
namespace Volo.Abp.DynamicProxy |
|||
{ |
|||
public static class ProxyHelper |
|||
{ |
|||
/// <summary>
|
|||
/// Returns dynamic proxy target object if this is a proxied object, otherwise returns the given object.
|
|||
/// </summary>
|
|||
public static object UnProxy(object obj) |
|||
{ |
|||
//TODO: This code depends on Castle, so we should find a better way.
|
|||
|
|||
if (obj.GetType().Namespace != "Castle.Proxies") |
|||
{ |
|||
return obj; |
|||
} |
|||
|
|||
var targetField = obj.GetType().GetTypeInfo() |
|||
.GetFields() |
|||
.FirstOrDefault(f => f.Name == "__target"); |
|||
|
|||
if (targetField == null) |
|||
{ |
|||
return obj; |
|||
} |
|||
|
|||
return targetField.GetValue(obj); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue