使用纯POCO实体框架获取存储库模式中的策略示例 [英] Fetching Strategy example in repository pattern with pure POCO Entity framework
问题描述
我试图用实体框架和存储库模式推出策略模式,使用一个简单的例子,例如 User
和 Post
其中一个用户有很多帖子。
从这个答案 添加界面以支持您将使用该用户的角色。 / p> 现在我的仓库看起来像这样: 策略(我被卡住的地方)。该代码怎么办?我可以举一个如何实现这个的例子,我在哪里可以这样做? 我的基本问题是此问题中提出的问题。我希望能够让用户没有帖子和使用策略模式的帖子。 如果我们谈论策略模式,那么IFetchingStrategy必须传递给IUserRepository,所以我想你应该修改Get操作: 但我不知道如何使用EF实现这样的接口。 如果我们回到以前的问题,也可以这样完成: 你将使用这种方法: 但我猜这个实现只适用于第一级导航属性。 I'm trying to roll out a strategy pattern with entity framework and the repository pattern using a simple example such as From this answer here, I have the following domain: Add interfaces to support the roles in which you will use the user. Now my repository looks like this: 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? 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: 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: You will use the method this way: But I guess that this implementation works only for first level of navigation properties. 这篇关于使用纯POCO实体框架获取存储库模式中的策略示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
interface IUser {
public Guid UserId {get;组; }
public string UserName {get;组; }
public IEnumerable< Post>帖子{get;组; }
}
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)
}
public interface IUserRepository
{
用户获取< TRole>(Guid userId,IFetchingStrategy< TRole>策略)其中TRole:IUser;
}
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
});
User
and Post
in which a user has many posts.public interface IUser {
public Guid UserId { get; set; }
public string UserName { get; set; }
public IEnumerable<Post> Posts { get; set; }
}
public interface IAddPostsToUser : IUser {
public void AddPost(Post post);
}
public interface IUserRepository {
User Get<TRole>(Guid userId) where TRole : IUser;
}
public interface IFetchingStrategy<TRole> {
TRole Fetch(Guid id, IRepository<TRole> role)
}
public interface IUserRepository
{
User Get<TRole>(Guid userId, IFetchingStrategy<TRole> strategy) where TRole : IUser;
}
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);
}
}
User userWithoutPosts = repository.Get(guid, null);
User userWithPosts = repository.Get(guid, new List<Expression<Func<User,object>>>
{
u => u.Posts
});