Entity framework 使用纯POCO实体框架在存储库模式中获取策略示例
我正试图使用一个简单的示例,例如Entity framework 使用纯POCO实体框架在存储库模式中获取策略示例,entity-framework,repository-pattern,poco,strategy-pattern,Entity Framework,Repository Pattern,Poco,Strategy Pattern,我正试图使用一个简单的示例,例如User和Post推出一个包含实体框架和存储库模式的策略模式,其中一个用户有许多帖子 根据此答案,我拥有以下域: public interface IUser { public Guid UserId { get; set; } public string UserName { get; set; } public IEnumerable<Post> Posts { get; set; } } 现在,我的存储库如下所示: public i
User
和Post
推出一个包含实体框架和存储库模式的策略模式,其中一个用户有许多帖子
根据此答案,我拥有以下域:
public interface IUser {
public Guid UserId { get; set; }
public string UserName { get; set; }
public IEnumerable<Post> Posts { get; set; }
}
现在,我的存储库如下所示:
public interface IUserRepository {
User Get<TRole>(Guid userId) where TRole : IUser;
}
公共接口存储库{
用户获取(Guid userId),其中TRole:IUser;
}
策略(我被卡住了)。我该如何处理此代码?我能举一个例子说明如何实现这一点吗?我应该把它放在哪里
public interface IFetchingStrategy<TRole> {
TRole Fetch(Guid id, IRepository<TRole> role)
}
公共接口IFetchingStrategy{
TRole获取(Guid id,IRepository角色)
}
我的基本问题是,我的回答是什么。我希望能够使用策略模式获取没有帖子的用户和有帖子的用户。如果我们讨论策略模式,那么IFetchingStrategy必须传递给IUserRepository,因此我认为您应该修改get操作:
public interface IUserRepository
{
User Get<TRole>(Guid userId, IFetchingStrategy<TRole> strategy) where TRole : IUser;
}
公共接口存储库
{
用户获取(Guid userId,IFetchingStrategy策略),其中TRole:IUser;
}
但是我不知道如何用EF实现这样的接口
如果我们回到前一个问题,也可以通过以下方式完成:
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
});
公共接口存储库
{
用户获取(Guid用户ID,IEnumerable);
}
公共类UserRepository:IUserRepository
{
公共用户Get(Guid用户ID,IEnumerable)
{
ObjectQuery query=GetBaseQuery();//以某种方式获取查询,例如从ObjectSet获取查询
如果(正在加载!=null)
{
foreach(加载中的var表达式)
{
//不支持开箱即用。您需要:
// http://msmvps.com/blogs/matthieu/archive/2008/06/06/entity-framework-include-with-func-next.aspx
query=query.Include(表达式);
}
}
返回query.SingleOrDefault(u=>u.Id==userId);
}
}
您将通过以下方式使用该方法:
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
});
User userWithoutPosts=repository.Get(guid,null);
用户userWithPosts=repository.Get(guid,新列表
{
u=>u.职位
});
但我猜这个实现只适用于第一级导航属性。如果我们讨论策略模式,那么IFetchingStrategy必须传递给IUserRepository,所以我认为您应该修改Get操作:
public interface IUserRepository
{
User Get<TRole>(Guid userId, IFetchingStrategy<TRole> strategy) where TRole : IUser;
}
公共接口存储库
{
用户获取(Guid userId,IFetchingStrategy策略),其中TRole:IUser;
}
但是我不知道如何用EF实现这样的接口
如果我们回到前一个问题,也可以通过以下方式完成:
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
});
公共接口存储库
{
用户获取(Guid用户ID,IEnumerable);
}
公共类UserRepository:IUserRepository
{
公共用户Get(Guid用户ID,IEnumerable)
{
ObjectQuery query=GetBaseQuery();//以某种方式获取查询,例如从ObjectSet获取查询
如果(正在加载!=null)
{
foreach(加载中的var表达式)
{
//不支持开箱即用。您需要:
// http://msmvps.com/blogs/matthieu/archive/2008/06/06/entity-framework-include-with-func-next.aspx
query=query.Include(表达式);
}
}
返回query.SingleOrDefault(u=>u.Id==userId);
}
}
您将通过以下方式使用该方法:
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
});
User userWithoutPosts=repository.Get(guid,null);
用户userWithPosts=repository.Get(guid,新列表
{
u=>u.职位
});
但是我猜这个实现只适用于第一级导航属性。下面的文章中的一个答案使用策略模式来操作查询
以下帖子中的答案使用策略模式来处理查询
这就是他们所说的战略模式?不,这不是战略模式。您想解决问题还是使用该模式?:):)这比策略模式更有意义,至少我不必为每种类型的用户编写一堆类。谢谢。这就是他们所说的战略模式?不,这不是战略模式。您想解决问题还是使用该模式?:):)这比策略模式更有意义,至少我不必为每种类型的用户编写一堆类。谢谢