-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added eager loading of one-to-one and one-to-many entity relations * Removed dead code * Fixed: Eagerloads can have nested Eagerload properties * Added safeguard against infinite loop. * Cleanup and realign of project files * Removed unused project dependencies * Empty commit to restart TravisCI * Post-merge fixes * Post-merge fixes * More post-merge fixes * Review feedback: make EagerLoad private
- Loading branch information
1 parent
24999c4
commit 3773cf9
Showing
16 changed files
with
406 additions
and
44 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 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 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.