Last active
May 16, 2018 00:08
-
-
Save dcomartin/99c282a4c49c4652b338645b61cfdae4 to your computer and use it in GitHub Desktop.
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
public class ParentEntity | |
{ | |
private readonly Action<object, string> _lazyLoader; | |
public Guid Id { get; set; } | |
public string Name { get; set; } | |
private List<ChildEntity> _children; | |
public List<ChildEntity> Children | |
{ | |
get | |
{ | |
if (_children == null) | |
{ | |
Console.WriteLine($"WARNING: Lazy Loading {nameof(Children)}"); | |
_lazyLoader?.Load(this, ref _children);; | |
} | |
return _children; | |
} | |
set => _children = value; | |
} | |
public ParentEntity(Action<object, string> lazyLoader) | |
{ | |
_lazyLoader = lazyLoader; | |
} | |
} | |
public class ChildEntity | |
{ | |
public Guid Id { get; set; } | |
public Guid ParentId { get; set; } | |
public string Name { get; set; } | |
} | |
public static class PocoLoadingExtensions | |
{ | |
public static TRelated Load<TRelated>( | |
this Action<object, string> loader, | |
object entity, | |
ref TRelated navigationField, | |
[CallerMemberName] string navigationName = null) | |
where TRelated : class | |
{ | |
loader?.Invoke(entity, navigationName); | |
return navigationField; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment