Instantly share code, notes, and snippets.

Embed
What would you like to do?
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