Event Sourcing selecting foreign key relationships in command handlers?

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.

New contributor

codingman123 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật