Skip to content

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

Merged
merged 4 commits into from
Jul 28, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/JsonApiDotNetCore/Internal/ResourceGraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ namespace JsonApiDotNetCore.Internal
public class ResourceGraph : IResourceGraph
{
private List<ResourceContext> Resources { get; }
private Type ProxyInterface { get; }
Copy link
Contributor

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:

private static readonly proxyTargetAccessorType = Type.GetType("Castle.DynamicProxy.IProxyTargetAccessor, Castle.Core");


public ResourceGraph(List<ResourceContext> resources)
{
Resources = resources;
ProxyInterface = Type.GetType("Castle.DynamicProxy.IProxyTargetAccessor, Castle.Core");
}

/// <inheritdoc />
Expand All @@ -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));
Expand Down Expand Up @@ -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;
Copy link
Contributor

Choose a reason for hiding this comment

The 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: IsLazyLoadingProxyForResourceType(). The usage of a specific interface to answer the question is an implementation detail.


private static Expression RemoveConvert(Expression expression)
=> expression is UnaryExpression unaryExpression
&& unaryExpression.NodeType == ExpressionType.Convert
Expand Down
37 changes: 37 additions & 0 deletions test/UnitTests/Internal/ResourceGraphBuilder_Tests.cs
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
Expand Down Expand Up @@ -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()
Copy link
Contributor

Choose a reason for hiding this comment

The 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 { }

}

}
1 change: 1 addition & 0 deletions test/UnitTests/UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<ItemGroup>
<PackageReference Include="Bogus" Version="$(BogusVersion)" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="$(EFCoreVersion)" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="$(EFCoreVersion)" />
<PackageReference Include="Moq" Version="$(MoqVersion)" />
<PackageReference Include="xunit" Version="$(XUnitVersion)" />
<PackageReference Include="xunit.runner.visualstudio" Version="$(XUnitVersion)" />
Expand Down