I am new to CQRS and Event Sourcing.
I have problems either properly modelling my Domain or I am misunderstanding something (highly likely).
Domain background. We have a collection of Lines
. The lines have a direction and keep track of which Hole
is to the left and right of it. So together the lines build up a “spiderweb”.
// Model
public class Hole
{
public int Id { get; set; }
}
public class Line
{
public int Id { get; set; }
public Point Start { get; set; }
public Point End { get; set; }
public int? HoleLeft { get; set; }
public int? HoleRight { get; set; }
// Extra code to store events, apply events and keep track of uncommitted events.
}
We also keep track of the Holes on the outside of the web. Therefor some lines only has either a HoleLeft or a HoleRight. Now to the problem. Say that we send new CreateLineCommand. We have business logic that demands that
- A line has either a HoleLeft or a HoleRight
- The HoleLeft/Right exists
- No line crosses another line
In the command handler I can then do something like this.
public class CreateLineCommandHandler
{
private readonly ILineRepository _lineRepo;
private readonly IHoleRepository _holeRepo;
public CreateLineCommandHandler(ILineRepository lineRepository, IHoleRepository holeRepository)
{
_lineRepo = lineRepository;
_holeRepo = holeRepository;
}
public void Handle(CreateLineCommand command)
{
Hole left = _holeRepo.GetById(command.HoleLeft);
Hole right = _holeRepo.GetById(command.HoleRight);
if (command.HoleLeft is null && command.HoleRight is null)
throw new Exception("Only left or right can be null, not both.");
if (command.HoleLeft == command.HoleRight)
throw new Exception("Left and right holes can not be the same.");
if (command.HoleLeft is not null && left is null)
throw new Exception("Unable to find left hole");
if (command.HoleRight is not null && right is null)
throw new Exception("Unable to find right hole");
// Now what I would like to do is something like this.
List<Line> lines = _lineRepo.GetLinesFromHoles([left.id, right.id]);
foreach (Line l in lines)
{
// check if crossing and if so throw exception.
}
Line line = new Line(command.Id,
command.Start,
command.End,
command.HoleLeft,
command.HoleRight);
_lineRepo.Save(line);
}
}
My repositories are the connection to the event store. So GetById fetches all events for the aggregate id input, applies all the events and returns the result. Save will get all uncommitted events and save those.
A GetLinesFromHoles
command seems impossible unless I involve the read model. In a relational db this would be an easy select statement. Maybe this was too long a preamble to ask how can we do execute business logic in command handlers that requires data from other aggregates or via “foreign keys” when we are working with an event store.
I’ve tried the above solution and get stuck at this fundamental issue. I’ve looked around on stack overflow and googled the issue without success.
codingman123 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.