在资料库模式共享库 [英] Shared repositories in repository pattern

查看:92
本文介绍了在资料库模式共享库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

库的依赖?可以说,我有一个类似如下域:

 公共类用户
{
公众诠释用户ID {搞定;组; }
公共懒<邮政和GT;帖子{搞定;组; }
}

公共类邮报
{
公众诠释{帖子ID获取;组; }
公众用户海报{搞定;组; }
}



我的帖子的用户。



如何设置我的存储库,以便为每个用户,它返回帖子列表和每,我得到的海报



我有2个仓库:

 公共类UserRepository:IUserRepository 
{
公开IQueryable的<使用者> GetUsers();
}

公共类PostRepository:IPostRepository
{
公众的IQueryable<邮政和GT; GetPosts();
}



我试着从1库到另一个呼叫,但它在结束了崩溃,因为有一个循环依赖。



我使用的是与实体框架库模式的POCO方法。


解决方案

我有2个仓库:

 公开类UserRepository:IUserRepository 
{
公共IQueryable的<使用者> GetUsers();
}

公共类PostRepository:IPostRepository
{
公众的IQueryable<邮政和GT; GetPosts();
}




这很可能成为问题。



您可能不想简单地 GetUsers 。正如你说的,你要装入骨料,或顶层,实体的子实体。



会发生什么是根据使用的情况下 - 你的眼前目的是 - 你会想在该用户的变化。你可能会像 GetUsers 方法,结束了 GetUsersWithPosts GetUsersForImportantReport GetUsersForAttendingAnIceCreamSocial ,等等等等,广告naseum。



现在缺少的是概念一个角色。



您检索什么作用,为用户?模型明确在这里。



首先得有个基本接口为您汇总。基本性能可以去这里

 公共接口IUSER {
公众的Guid用户Id {搞定;组; }
公共字符串用户名{获得;组; }
公开的IEnumerable<岗位> {搞定;组; }
}

添加接口,支持将在其中使用用户的角色。

  public接口IAddPostsToUser:IUSER {
公共无效AddPost(后后);

$ B $:
}

你的资料库可以被定义为这样的

和b

 公共接口IUserRepository {
的用户获取和LT; TRole>(GUID用户id)其中TRole:IUSER;
}

现在,使用该角色定义抓取策略;

 公共接口IFetchingStrategy< TRole> {
TRole取(GUID ID,IRepository< TRole>的作用)
}

您的存储库将获得通过注入或服务的位置抓取策略和呼叫取。您可以通过资源库提供机制,FetchingStrategy查询或有FetchingStrategy注入或找到它需要查询什么服务。



当然,究竟如何查询将取决于你的ORM。



但是造型将有助于避免在不同情况下装载实体图形许多问题的这种方式。


Repository dependency? Lets say I have a domain that looks like the following:

public class User
{
    public int UserId { get; set; }
    public Lazy<Post> Posts { get; set; }
}

public class Post
{
    public int PostId { get; set; }
    public User Poster { get; set; }
}

A user with my posts.

How do I set up my repository so that for every user, it returns a list of Post and for every post, I get the Poster?

I have 2 repositories:

public class UserRepository : IUserRepository
{
    public IQueryable<User> GetUsers();
}

public class PostRepository : IPostRepository
{
    public IQueryable<Post> GetPosts();
}

I tried calling from 1 repository to the other, but it ends up in a crash because there is a circular dependency.

I'm using the POCO method of the repository pattern with entity framework.

解决方案

I have 2 repositories:

public class UserRepository : IUserRepository
{
    public IQueryable<User> GetUsers();
}

public class PostRepository : IPostRepository
{
    public IQueryable<Post> GetPosts();
}

This is likely to become problematic.

You likely don't want to simply GetUsers. As you say, you want child entities loaded with the aggregate, or top-level, entity.

What will occur is that depending on the context of use - what your immediate purpose is - you will want variations on that User. You'll likely end up with methods like GetUsers, 'GetUsersWithPosts, GetUsersForImportantReport, GetUsersForAttendingAnIceCreamSocial, etc., etc., ad naseum.

What's missing is the concept of a role.

What role are you retrieving the User for? Model that explicitly here.

Start with a base interface for your aggregate. Basic properties can go here

public interface IUser {
  public Guid UserId { get; set; }
  public string UserName { get; set; }
  public IEnumerable<Posts> { get; set; }
}

Add interfaces to support the roles in which you will use the user.

public interface IAddPostsToUser : IUser {
  public void AddPost(Post post);
}

And your repository can be defined as such:

public interface IUserRepository {
  User Get<TRole>(Guid userId) where TRole : IUser;
}

Now, use that role to define a fetching strategy;

public interface IFetchingStrategy<TRole> {
  TRole Fetch(Guid id, IRepository<TRole> role)
}

Your repository would get the fetching strategies through injection or service location and call fetch. You can pass the repository in to provide the mechanism for the FetchingStrategy to query or have the FetchingStrategy inject or locate what services it needs to query.

Of course, exactly how you query will depend on your ORM.

But this way of modeling will help avoid many problems with loading entity graphs in different scenarios.

这篇关于在资料库模式共享库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆