Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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;
- }
- }
Add Comment
Please, Sign In to add comment