Skip to content

Instantly share code, notes, and snippets.

@dcomartin
Last active May 16, 2018 00:08
Show Gist options
  • Save dcomartin/99c282a4c49c4652b338645b61cfdae4 to your computer and use it in GitHub Desktop.
Save dcomartin/99c282a4c49c4652b338645b61cfdae4 to your computer and use it in GitHub Desktop.
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