'Where should I place read models in the DDD architecture?

Currently I learn CQRS and DDD without Event Sourcing (not yet). I need to return from my repository to a query handler a model different from a domain model. Should I create a read model in a domain project? I need to use this read model in a repository (an infrastructure project), a repository interface (a domain project) and a query handler (an application project)?

A further description:

In my database I have a table Posts related to a table Comments. There is a foreign key: PostId in the table Comments. And this is a domain model in my project, as you can see there is no PostId in Comment:

public class Post : Entity, IAggregateRoot
{
    public Guid PostId { get; private set; }

    public string Title { get; private set; }

    public string Content { get; private set; }
    
    public List<Comment> Comments { get; private set; }
}

public class Comment : Entity
{
    public Guid CommentId { get; private set; }

    public string Author { get; private set; }

    public string Content { get; private set; }
}

I would like to return from my repository a comment which contains not only CommentId, Author, Content but also PostId and PostTitle. This is my repository:

public class CommentsRepository : ICommentsRepository
{
    private readonly ApplicationDbContext _applicationDbContext;

    public CommentsRepository(ApplicationDbContext applicationDbContext)
    {
        _applicationDbContext = applicationDbContext;
    }

    public async Task<Comment> GetByIdAsync(Guid id)
    {
        var comment = await _applicationDbContext.Comments
            .SingleOrDefaultAsync(x => x.CommentId == id);

        return comment;
    }
}

Should I create a model CommentReadModel (with properties: CommentId, Author, Content, PostId and PostTitle) and write SQL in the repository to fill in that model? If yes, where should I place that model because it will be needed in 3 projects:

  • DomainProject (there is the interface: ICommentsRepository)
  • InfrastructureProject (there is the implementation: CommentsRepository)
  • ApplicationProject (there is a query handler which calls the repository) ??


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source