-
-
Notifications
You must be signed in to change notification settings - Fork 158
Eager loading #701
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
Eager loading #701
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
c0d404d
Added eager loading of one-to-one and one-to-many entity relations
d33b03c
Removed dead code
5a0c835
Fixed: Eagerloads can have nested Eagerload properties
aab1210
Added safeguard against infinite loop.
f9c6f19
Cleanup and realign of project files
847f83f
Removed unused project dependencies
028440a
Empty commit to restart TravisCI
6b043f7
Merge branch 'master' into eager-loading
cf8b79f
Post-merge fixes
e73fa24
Merge branch 'master' into eager-loading
ea0a2f2
Post-merge fixes
0d4b380
More post-merge fixes
b6675cf
Review feedback: make EagerLoad private
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace JsonApiDotNetCoreExample.Models | ||
{ | ||
public class Country | ||
{ | ||
public int Id { get; set; } | ||
public string Name { get; set; } | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,50 @@ | ||
using System.Collections.Generic; | ||
using System.ComponentModel.DataAnnotations.Schema; | ||
using System.Linq; | ||
using JsonApiDotNetCore.Models; | ||
|
||
namespace JsonApiDotNetCoreExample.Models | ||
{ | ||
public sealed class Passport : Identifiable | ||
public class Passport : Identifiable | ||
{ | ||
[Attr] | ||
public int? SocialSecurityNumber { get; set; } | ||
|
||
[Attr] | ||
public bool IsLocked { get; set; } | ||
|
||
[HasOne] | ||
public Person Person { get; set; } | ||
|
||
[Attr] | ||
[NotMapped] | ||
public string BirthCountryName | ||
{ | ||
get => BirthCountry.Name; | ||
set | ||
{ | ||
if (BirthCountry == null) | ||
{ | ||
BirthCountry = new Country(); | ||
} | ||
|
||
BirthCountry.Name = value; | ||
} | ||
} | ||
|
||
[EagerLoad] | ||
public Country BirthCountry { get; set; } | ||
|
||
[Attr(isImmutable: true)] | ||
[NotMapped] | ||
public string GrantedVisaCountries | ||
{ | ||
get => GrantedVisas == null ? null : string.Join(", ", GrantedVisas.Select(v => v.TargetCountry.Name)); | ||
// The setter is required only for deserialization in unit tests. | ||
set { } | ||
} | ||
|
||
[EagerLoad] | ||
public ICollection<Visa> GrantedVisas { get; set; } | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using System; | ||
using JsonApiDotNetCore.Models; | ||
|
||
namespace JsonApiDotNetCoreExample.Models | ||
{ | ||
public class Visa | ||
{ | ||
public int Id { get; set; } | ||
|
||
public DateTime ExpiresAt { get; set; } | ||
|
||
[EagerLoad] | ||
public Country TargetCountry { get; set; } | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/JsonApiDotNetCore/Models/Annotation/EagerLoadAttribute.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Reflection; | ||
|
||
namespace JsonApiDotNetCore.Models | ||
{ | ||
/// <summary> | ||
/// Used to unconditionally load a related entity that is not exposed as a json:api relationship. | ||
/// </summary> | ||
/// <remarks> | ||
/// This is intended for calculated properties that are exposed as json:api attributes, which depend on a related entity to always be loaded. | ||
/// <example><![CDATA[ | ||
/// public class User : Identifiable | ||
/// { | ||
/// [Attr(isImmutable: true)] | ||
/// [NotMapped] | ||
/// public string DisplayName => Name.First + " " + Name.Last; | ||
/// | ||
/// public Name Name { get; set; } | ||
/// } | ||
/// | ||
/// public class Name // not exposed as resource, only database table | ||
/// { | ||
/// public string First { get; set; } | ||
/// public string Last { get; set; } | ||
/// } | ||
/// | ||
/// public class Blog : Identifiable | ||
/// { | ||
/// [HasOne] | ||
/// public User Author { get; set; } | ||
/// } | ||
/// ]]></example> | ||
/// </remarks> | ||
public sealed class EagerLoadAttribute : Attribute | ||
{ | ||
public PropertyInfo Property { get; internal set; } | ||
|
||
public IList<EagerLoadAttribute> Children { get; internal set; } | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.