CommentService で 2 つのリポジトリを使用しています。
_commentRepository
_userRepository
_commentRepository.GetAll() 関数を使用して、[Id]、[Content]、[UserId] の情報を含むすべてのコメントのリストを取得します。
すべてのコメントと、_userRepository によって取得可能な一致するユーザー情報の一部を含むリストを作成し、これを DTO に保存しようとしています。
public ListOutput<CommentUserDto> GetCommentsWithUserInformation()
{
var comments = _commentRepository.GetAll();
// TODO: Add comment and user information to CommentUserDto.
return new ListOutput<CommentUserDto>
{
Items = Mapper.Map<List<CommentUserDto>>(comments)
};
}
どうすればこれを機能させることができますか?
私が見つけたいくつかの可能なアイデア:
- _commentUserRepository の作成
- include を使用して 2 つのテーブルを結合する (私は EF を使用しています)
- ロジックの結合を担当するドメイン レイヤーにマネージャーを作成します。
編集:
コメントモデル:
public class Comment
{
public virtual string Content { get; set; }
public virtual DateTime CreationTime { get; set; }
public virtual long UserId { get; set; } // Id of User that posted Comment (always filled in)
}
ユーザーモデル:
public class User {
public virtual string Name { get; set; }
public virtual string Surname { get; set; }
public virtual string Email { get; set; }
public virtual string Password { get; set; }
}
CommentUserDto: // ビューのアクセス可能なクラス
public class CommentUserDto {
public string Content { get; set; } // from comment
public DateTime CreationTime { get; set; } // from comment
public string PosterName { get; set; } // from user
public string PosterSurname { get; set; } // from user
}