-
-
Notifications
You must be signed in to change notification settings - Fork 158
fix: support for lazy loading proxies #793
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,10 +13,12 @@ namespace JsonApiDotNetCore.Internal | |
public class ResourceGraph : IResourceGraph | ||
{ | ||
private List<ResourceContext> Resources { get; } | ||
private Type ProxyInterface { get; } | ||
|
||
public ResourceGraph(List<ResourceContext> resources) | ||
{ | ||
Resources = resources; | ||
ProxyInterface = Type.GetType("Castle.DynamicProxy.IProxyTargetAccessor, Castle.Core"); | ||
} | ||
|
||
/// <inheritdoc /> | ||
|
@@ -26,7 +28,9 @@ public ResourceContext GetResourceContext(string resourceName) | |
=> Resources.SingleOrDefault(e => e.ResourceName == resourceName); | ||
/// <inheritdoc /> | ||
public ResourceContext GetResourceContext(Type resourceType) | ||
=> Resources.SingleOrDefault(e => e.ResourceType == resourceType); | ||
=> IsDynamicProxy(resourceType) ? | ||
Resources.SingleOrDefault(e => e.ResourceType == resourceType.BaseType) : | ||
Resources.SingleOrDefault(e => e.ResourceType == resourceType); | ||
/// <inheritdoc /> | ||
public ResourceContext GetResourceContext<TResource>() where TResource : class, IIdentifiable | ||
=> GetResourceContext(typeof(TResource)); | ||
|
@@ -125,6 +129,8 @@ private IEnumerable<IResourceField> Getter<T>(Expression<Func<T, dynamic>> selec | |
$"For example: 'article => article.Title' or 'article => new {{ article.Title, article.PageCount }}'."); | ||
} | ||
|
||
private bool IsDynamicProxy(Type resourceType) => ProxyInterface?.IsAssignableFrom(resourceType) ?? false; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A better name for this method would indicate what it functionally represents, for example: |
||
|
||
private static Expression RemoveConvert(Expression expression) | ||
=> expression is UnaryExpression unaryExpression | ||
&& unaryExpression.NodeType == ExpressionType.Convert | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
using JsonApiDotNetCore.Builders; | ||
using JsonApiDotNetCore.Configuration; | ||
using JsonApiDotNetCore.Internal; | ||
using JsonApiDotNetCore.Models; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Logging.Abstractions; | ||
using Castle.DynamicProxy; | ||
using Xunit; | ||
|
||
namespace UnitTests.Internal | ||
|
@@ -41,12 +43,47 @@ public void Adding_DbContext_Members_That_Do_Not_Implement_IIdentifiable_Logs_Wa | |
Assert.Equal("Entity 'UnitTests.Internal.ResourceGraphBuilder_Tests+TestContext' does not implement 'IIdentifiable'.", loggerFactory.Logger.Messages[0].Text); | ||
} | ||
|
||
[Fact] | ||
public void GetResourceContext_Yields_Right_Type_For_Proxy() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider: GetResourceContext_Yields_Right_Type_For_LazyLoadingProxy |
||
{ | ||
// Arrange | ||
var resourceGraphBuilder = new ResourceGraphBuilder(new JsonApiOptions(), NullLoggerFactory.Instance); | ||
resourceGraphBuilder.AddResource<Bar>(); | ||
var resourceGraph = (ResourceGraph)resourceGraphBuilder.Build(); | ||
var proxyGenerator = new ProxyGenerator(); | ||
|
||
// Act | ||
var proxy = proxyGenerator.CreateClassProxy<Bar>(); | ||
var result = resourceGraph.GetResourceContext(proxy.GetType()); | ||
|
||
// Assert | ||
Assert.Equal(typeof(Bar), result.ResourceType); | ||
} | ||
|
||
[Fact] | ||
public void GetResourceContext_Yields_Right_Type_For_Identifiable() | ||
{ | ||
// Arrange | ||
var resourceGraphBuilder = new ResourceGraphBuilder(new JsonApiOptions(), NullLoggerFactory.Instance); | ||
resourceGraphBuilder.AddResource<Bar>(); | ||
var resourceGraph = (ResourceGraph)resourceGraphBuilder.Build(); | ||
|
||
// Act | ||
var result = resourceGraph.GetResourceContext(typeof(Bar)); | ||
|
||
// Assert | ||
Assert.Equal(typeof(Bar), result.ResourceType); | ||
} | ||
|
||
private class Foo { } | ||
|
||
private class TestContext : DbContext | ||
{ | ||
public DbSet<Foo> Foos { get; set; } | ||
} | ||
|
||
public class Bar : Identifiable { } | ||
|
||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this field should be named
proxyTargetAccessorType
, because that is what it is. Additionally, its value does not change during execution, so it can be evaluated once: