【问题标题】:Repository pattern - Joining data from two tables存储库模式 - 连接来自两个表的数据
【发布时间】:2015-09-18 12:17:07
【问题描述】:

我在 CommentService 中使用了两个存储库。

_commentRepository
_userRepository

使用 _commentRepository.GetAll() 函数,我获得了包含以下信息的所有 cmets 的列表:[Id]、[Content]、[UserId]。

我正在尝试创建一个列表,其中包含所有 cmets 和一些可通过 _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)
    };
}

我怎样才能做到这一点?

我发现的一些可能的想法:

  1. 创建 _commentUserRepository
  2. 使用 include 以某种方式连接两个表(我使用的是 E.F.)
  3. 在我的域层中创建一个负责组合逻辑的管理器。

编辑:

评论模型:

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
}

【问题讨论】:

  • 你在使用实体框架吗?
  • 我个人喜欢有一个用于评论和用户的存储库,然后在存储库之上我有我的服务类,它为我提供了来自这两个存储库的组合对象。如果您关心使用实体框架,Include 也是一个选项
  • 是的,我是(将澄清问题)。
  • 您的评论库返回什么对象?您可以发布 EF 生成的模型吗?如果你正确连接了评论和用户,你应该有从 cmets 到用户的连接,反之亦然。我认为这并不像您在这里介绍的那么复杂。

标签: .net asp.net-mvc asp.net-mvc-5 repository-pattern data-transfer-objects


【解决方案1】:

您不需要执行您提到的三个选项中的任何一个。如果您有从评论到用户的导航属性,您可以在映射中处理它。比如:

Mapper.CreateMap<Comment, CommentUserDto>().ForMember(dest => dest.UserName, opts => opts.MapFrom(src => src.User.UserName));

如果您没有导航属性,则在初始映射后,循环遍历 dto 对象列表并调用适当的 _userRepository 方法来获取用户信息并以这种方式填充 dto 对象的适当成员。

编辑

看到你的模型后,我会做的(假设导航属性不是一个选项)是这样的:

var comments = _commentRepository.GetAll();

var results = new List<CommentUserDto>();

foreach(Comment comment in comments)
{
   var user = _userRepository.Get(comment.userId);
   var commentUserDto = new CommentUserDto
   {
      Content = comment.Content,
      CreationTime = comment.CreationTime,
      PosterName = user.Name,
      PosterSurname = user.Surname
   }

   results.Add(commentUserDto);
}

return results;

【讨论】:

  • 谢谢,就像我提到的 Comment 类确实有一个 UserId 属性。立即尝试您的示例。
  • 在您的示例中,映射是在 Comment (具有 userid 属性)和 CommentUserDto (仅用于查看的必要字段)之间完成的。但是,需要的用户信息也应该存储在 CommentUserDto 中,以便视图可以访问它们。结果应该是一个包含所有 cmets 的列表,并且每个评论都包含匹配的用户信息。
  • 如果您不发布模型类,我们将无法帮助您。那么,请您使用 Comment、User 和 CommentUserDto 的模型类更新您的问题(我不能强调这一点)
  • @Sam 这就是为什么映射示例仅在您的 Comment 对象具有对 User 的导航属性时才有效的原因。如果没有(并且您不想添加一个),那么您需要采用另一种方式,在完成初始映射后,循环遍历 dto 对象列表并调用 _userRepository 方法来获取您需要的用户数据。
  • @MattyM 我的评论模型中有一个导航属性 (UserId)。但是将 Comment 映射到 CommentUserDto 将不会检索存储在用户表中的所需用户信息(查看模型的已编辑问题)。还是我理解错了?
猜你喜欢
  • 1970-01-01
  • 2014-10-05
  • 1970-01-01
  • 2011-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-27
相关资源
最近更新 更多