Skip to content

Commit a4b1244

Browse files
committed
Added CastleProxyHelper.
1 parent c0f544c commit a4b1244

File tree

8 files changed

+65
-53
lines changed

8 files changed

+65
-53
lines changed

src/Abp.EntityFramework.GraphDiff/GraphDiff/Extensions/GraphDiffExtensions.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@ public static class GraphDiffExtensions
2525
public static TEntity AttachGraph<TEntity, TPrimaryKey>(this IRepository<TEntity, TPrimaryKey> repository, TEntity entity)
2626
where TEntity : class, IEntity<TPrimaryKey>, new()
2727
{
28-
var iocResolver = ((AbpRepositoryBase<TEntity, TPrimaryKey>)repository).IocResolver;
29-
30-
using (var mappingManager = iocResolver.ResolveAsDisposable<IEntityMappingManager>())
28+
using (var mappingManager = repository.GetIocResolver().ResolveAsDisposable<IEntityMappingManager>())
3129
{
3230
var mapping = mappingManager.Object.GetEntityMappingOrNull<TEntity>();
3331
return repository

src/Abp.EntityFramework/EntityFramework/Repositories/EfRepositoryExtensions.cs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,13 @@ public static class EfRepositoryExtensions
1111
public static DbContext GetDbContext<TEntity, TPrimaryKey>(this IRepository<TEntity, TPrimaryKey> repository)
1212
where TEntity : class, IEntity<TPrimaryKey>
1313
{
14-
var repositoryWithDbContext = repository as IRepositoryWithDbContext;
15-
if (repositoryWithDbContext == null)
14+
var repositoryWithDbContext = ProxyHelper.UnProxy(repository) as IRepositoryWithDbContext;
15+
if (repositoryWithDbContext != null)
1616
{
17-
throw new ArgumentException("Given repository does not implement IRepositoryWithDbContext", nameof(repository));
17+
return repositoryWithDbContext.GetDbContext();
1818
}
1919

20-
var targetWithDbContext = CastleProxyHelper.GetProxyTargetOrNull(repository) as IRepositoryWithDbContext;
21-
if (targetWithDbContext != null)
22-
{
23-
return targetWithDbContext.GetDbContext();
24-
}
25-
26-
return repositoryWithDbContext.GetDbContext();
20+
throw new ArgumentException("Given repository does not implement IRepositoryWithDbContext", nameof(repository));
2721
}
2822

2923
public static void DetachFromDbContext<TEntity, TPrimaryKey>(this IRepository<TEntity, TPrimaryKey> repository, TEntity entity)

src/Abp.EntityFrameworkCore/EntityFrameworkCore/Repositories/EfCoreRepositoryExtensions.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,12 @@ public static class EfCoreRepositoryExtensions
1111
public static DbContext GetDbContext<TEntity, TPrimaryKey>(this IRepository<TEntity, TPrimaryKey> repository)
1212
where TEntity : class, IEntity<TPrimaryKey>
1313
{
14-
var repositoryWithDbContext = repository as IRepositoryWithDbContext;
14+
var repositoryWithDbContext = ProxyHelper.UnProxy(repository) as IRepositoryWithDbContext;
1515
if (repositoryWithDbContext != null)
1616
{
1717
return repositoryWithDbContext.GetDbContext();
1818
}
1919

20-
var targetWithDbContext = CastleProxyHelper.GetProxyTargetOrNull(repository) as IRepositoryWithDbContext;
21-
if (targetWithDbContext != null)
22-
{
23-
return targetWithDbContext.GetDbContext();
24-
}
25-
2620
throw new ArgumentException("Given repository does not implement IRepositoryWithDbContext", nameof(repository));
2721
}
2822

src/Abp/Domain/Repositories/RepositoryExtensions.cs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
using System.Linq.Expressions;
44
using System.Threading;
55
using System.Threading.Tasks;
6+
using Abp.Dependency;
67
using Abp.Domain.Entities;
8+
using Abp.Reflection;
79

810
namespace Abp.Domain.Repositories
911
{
@@ -13,14 +15,28 @@ public static async Task EnsureLoadedAsync<TEntity, TPrimaryKey, TProperty>(
1315
this IRepository<TEntity, TPrimaryKey> repository,
1416
TEntity entity,
1517
Expression<Func<TEntity, IEnumerable<TProperty>>> propertyExpression,
16-
CancellationToken cancellationToken = default(CancellationToken))
18+
CancellationToken cancellationToken = default(CancellationToken)
19+
)
1720
where TEntity : class, IEntity<TPrimaryKey>
1821
where TProperty : class
1922
{
20-
if (repository is ISupportsExplicitLoading<TEntity, TPrimaryKey>)
23+
var repo = ProxyHelper.UnProxy(repository) as ISupportsExplicitLoading<TEntity, TPrimaryKey>;
24+
if (repo != null)
2125
{
22-
await (repository as ISupportsExplicitLoading<TEntity, TPrimaryKey>).EnsureLoadedAsync(entity, propertyExpression, cancellationToken);
26+
await repo.EnsureLoadedAsync(entity, propertyExpression, cancellationToken);
2327
}
2428
}
29+
30+
public static IIocResolver GetIocResolver<TEntity, TPrimaryKey>(this IRepository<TEntity, TPrimaryKey> repository)
31+
where TEntity : class, IEntity<TPrimaryKey>
32+
{
33+
var repo = ProxyHelper.UnProxy(repository) as AbpRepositoryBase<TEntity, TPrimaryKey>;
34+
if (repo != null)
35+
{
36+
return repo.IocResolver;
37+
}
38+
39+
throw new ArgumentException($"Given {nameof(repository)} is not inherited from {typeof(AbpRepositoryBase<TEntity, TPrimaryKey>).AssemblyQualifiedName}");
40+
}
2541
}
2642
}

src/Abp/Reflection/CastleProxyHelper.cs

Lines changed: 0 additions & 27 deletions
This file was deleted.

src/Abp/Reflection/ProxyHelper.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System.Linq;
2+
using System.Reflection;
3+
4+
namespace Abp.Reflection
5+
{
6+
public static class ProxyHelper
7+
{
8+
/// <summary>
9+
/// Returns dynamic proxy target object if this is a proxied object, otherwise returns the given object.
10+
/// </summary>
11+
public static object UnProxy(object obj)
12+
{
13+
if (obj.GetType().Namespace != "Castle.Proxies")
14+
{
15+
return obj;
16+
}
17+
18+
var targetField = obj.GetType().GetTypeInfo()
19+
.GetFields()
20+
.FirstOrDefault(f => f.Name == "__target");
21+
22+
if (targetField == null)
23+
{
24+
return obj;
25+
}
26+
27+
return targetField.GetValue(obj);
28+
}
29+
}
30+
}

test/Abp.TestBase.SampleApplication.Tests/EntityFramework/EfRepositoryExtensions_Tests.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Abp.Domain.Repositories;
2+
using Abp.Domain.Uow;
23
using Abp.EntityFramework.Repositories;
34
using Abp.Extensions;
45
using Abp.TestBase.SampleApplication.Crm;
@@ -20,13 +21,18 @@ public EfRepositoryExtensions_Tests()
2021
[Fact]
2122
public void Should_Get_DbContext()
2223
{
23-
_companyRepository.GetDbContext().ShouldBeOfType<SampleApplicationDbContext>();
24+
using (var uow = Resolve<IUnitOfWorkManager>().Begin())
25+
{
26+
_companyRepository.GetDbContext().ShouldBeOfType<SampleApplicationDbContext>();
27+
28+
uow.Complete();
29+
}
2430
}
2531

2632
[Fact]
2733
public void Should_Get_IocResolver()
2834
{
29-
_companyRepository.As<AbpRepositoryBase<Company, int>>().IocResolver.ShouldNotBeNull();
35+
_companyRepository.GetIocResolver().ShouldNotBeNull();
3036
}
3137
}
3238
}

test/Abp.TestBase.SampleApplication.Tests/People/PersonRepository_General_Tests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Linq;
22
using Abp.Domain.Repositories;
33
using Abp.Domain.Uow;
4+
using Abp.Reflection;
45
using Abp.TestBase.SampleApplication.EntityFramework.Repositories;
56
using Abp.TestBase.SampleApplication.People;
67
using Shouldly;
@@ -20,7 +21,7 @@ public PersonRepository_General_Tests()
2021
[Fact]
2122
public void Should_Use_Custom_Base_Repository_Class()
2223
{
23-
(_personRepository is SampleApplicationEfRepositoryBase<Person>).ShouldBeTrue();
24+
(ProxyHelper.UnProxy(_personRepository) is SampleApplicationEfRepositoryBase<Person>).ShouldBeTrue();
2425
}
2526

2627
[Fact]

0 commit comments

Comments
 (0)