使用纯POCO实体框架获取存储库模式中的策略示例 [英] Fetching Strategy example in repository pattern with pure POCO Entity framework

查看:150
本文介绍了使用纯POCO实体框架获取存储库模式中的策略示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用实体框架和存储库模式推出策略模式,使用一个简单的例子,例如 User Post 其中一个用户有很多帖子。



从这个答案

interface IUser {
public Guid UserId {get;组; }
public string UserName {get;组; }
public IEnumerable< Post>帖子{get;组; }
}

添加界面以支持您将使用该用户的角色。 / p>

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

现在我的仓库看起来像这样:

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

策略(我被卡住的地方)。该代码怎么办?我可以举一个如何实现这个的例子,我在哪里可以这样做?

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

我的基本问题是此问题中提出的问题。我希望能够让用户没有帖子和使用策略模式的帖子。

解决方案

如果我们谈论策略模式,那么IFetchingStrategy必须传递给IUserRepository,所以我想你应该修改Get操作:

  public interface IUserRepository 
{
用户获取< TRole>(Guid userId,IFetchingStrategy< TRole>策略)其中TRole:IUser;
}

但我不知道如何使用EF实现这样的接口。



如果我们回到以前的问题,也可以这样完成:

 code> public interface IUserRepository 
{
User Get(Guid userId,IEnumerable< Expression< Func< User,object>>> eagerLoading);
}

public class UserRepository:IUserRepository
{
public User Get(Guid userId,IEnumerable< Expression< Func< User,object>>> eagerLoading)
{
ObjectQuery< User> query = GetBaseQuery(); //以某种方式获取查询,例如从ObjectSet< User>

if(eagerLoading!= null)
{
foreach(eagerLoading中的var表达式)
{
//开箱即用不支持。你需要这样:
// http://msmvps.com/blogs/matthieu/archive/2008/06/06/entity-framework-include-with-func-next.aspx
query = query .INCLUDE(表达);
}
}

return query.SingleOrDefault(u => u.Id == userId);
}
}

你将使用这种方法:

 用户userWithoutPosts = repository.Get(guid,null); 
用户userWithPosts = repository.Get(guid,新列表< Func<用户,对象>>>>
{
u => u.Posts
});

但我猜这个实现只适用于第一级导航属性。


I'm trying to roll out a strategy pattern with entity framework and the repository pattern using a simple example such as User and Post in which a user has many posts.

From this answer here, I have the following domain:

public interface IUser {
  public Guid UserId { get; set; }
  public string UserName { get; set; }
  public IEnumerable<Post> 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);
}

Now my repository looks like this:

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

Strategy (Where I'm stuck). What do I do with this code? Can I have an example of how to implement this, where do I put this?

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

My basic problem was what was asked in this question. I'd like to be able to get Users without posts and users with posts using the strategy pattern.

解决方案

If we talk about strategy pattern then IFetchingStrategy must be passed to IUserRepository so I think you should modify Get operation:

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

But I'm not sure how to implement such interfaces with EF.

If we return to your former question, it can also be accomplished this way:

public interface IUserRepository 
{   
    User Get(Guid userId, IEnumerable<Expression<Func<User,object>>> eagerLoading); 
} 

public class UserRepository : IUserRepository
{
    public User Get(Guid userId, IEnumerable<Expression<Func<User,object>>> eagerLoading)
    {
        ObjectQuery<User> query = GetBaseQuery(); // get query somehow, for example from ObjectSet<User>

        if (eagerLoading != null)
        {
            foreach(var expression in eagerLoading)
            {
                // This is not supported out of the box. You need this:
                // http://msmvps.com/blogs/matthieu/archive/2008/06/06/entity-framework-include-with-func-next.aspx
                query = query.Include(expression);
            }
        }

        return query.SingleOrDefault(u => u.Id == userId);
    }
}

You will use the method this way:

User userWithoutPosts = repository.Get(guid, null);
User userWithPosts = repository.Get(guid, new List<Expression<Func<User,object>>>
    {
        u => u.Posts 
    });

But I guess that this implementation works only for first level of navigation properties.

这篇关于使用纯POCO实体框架获取存储库模式中的策略示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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