Guest User

Untitled

a guest
May 21st, 2018
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. public class ParentEntity
  2. {
  3. private readonly Action<object, string> _lazyLoader;
  4.  
  5. public Guid Id { get; set; }
  6. public string Name { get; set; }
  7.  
  8. private List<ChildEntity> _children;
  9. public List<ChildEntity> Children
  10. {
  11. get
  12. {
  13. if (_children == null)
  14. {
  15. Console.WriteLine($"WARNING: Lazy Loading {nameof(Children)}");
  16. _lazyLoader?.Load(this, ref _children);;
  17. }
  18.  
  19. return _children;
  20. }
  21. set => _children = value;
  22. }
  23.  
  24. public ParentEntity(Action<object, string> lazyLoader)
  25. {
  26. _lazyLoader = lazyLoader;
  27. }
  28.  
  29. }
  30.  
  31. public class ChildEntity
  32. {
  33. public Guid Id { get; set; }
  34. public Guid ParentId { get; set; }
  35. public string Name { get; set; }
  36. }
  37.  
  38. public static class PocoLoadingExtensions
  39. {
  40. public static TRelated Load<TRelated>(
  41. this Action<object, string> loader,
  42. object entity,
  43. ref TRelated navigationField,
  44. [CallerMemberName] string navigationName = null)
  45. where TRelated : class
  46. {
  47. loader?.Invoke(entity, navigationName);
  48.  
  49. return navigationField;
  50. }
  51. }
Add Comment
Please, Sign In to add comment