C#C
C#4y ago
Thinker

Turn recursive entities into model [Answered]

I have two db entities like this:
public sealed class TodoListEntity {
  public Guid Id { get; set; }
  public ICollection<TodoItemEntity> Items { get; set; } = null!;
}
public sealed class TodoItemEntity {
  public Guid Id { get; set; }
  // Other data

  public Guid ListId { get; set; }
  public TodoListEntity List { get; set; } = null!;
}

I wanna turn this into a model/DTO, so I have these extensions
public static TodoList ToModel(this TodoListEntity entity) =>
  new(entity.Id, entity.Items.Select(ToModel).ToArray());
public static TodoItem ToModel(this TodoItemEntity entity) =>
  new(entity.Id, /* Other data */, entity.List.ToModel());

This however causes a stack overflow because ToModel(TodoListEntity) calls ToModel(TodoItemEntity) and so on which causes unbound recursion. What would the simplest way to fix this be? I somehow have to keep a single list which I can pass to all items inside it.
Was this page helpful?