在我的网站中,每个page_load中检索到的当前用户都有很多依赖项(总共13个):
public User Get(Guid userId)
{
MyEntities entities = _contextManager.Context;
User user = entities.Users
.Include(x => x.Something1)
.Include(x => x.Something2)
.Include(x => x.Something3)
.Include(x => x.Something4)
.Include(x => x.Something5)
.Include(x => x.Something6)
.Include(x => x.Something7)
.Include(x => x.Something8)
.Include(x => x.Something9)
.Include(x => x.Something10)
.Include(x => x.Something11)
.Include(x => x.Something12)
.Include(x => x.Something13.Select(s => s.Something14))
.SingleOrDefault(u => (u.Id == userId));
return user;
}
但它需要很长时间才能保持这样。 但是,我不需要每一页中的所有相关对象。 因此,我认为我可以做类似的事情:
public partial class MyPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
App.GetCurrentUser(this);
}
}
在GetCurrentUser中:
public static void GetCurrentUser(System.Web.UI.Page page)
{
// Here only load data required by the current page
}
这是一种不好的做法吗? 如果是,是否有任何适当的解决方案来进行包含调速器的查询?
非常感谢!
修改
这是当前的App.GetCurrentUser:
public static User GetCurrentUser()
{
using (UnitOfWork uow = new UnitOfWork())
{
if (Membership.GetUser() != null)
{
UserRepo userRepo = new UserRepo();
Guid guid = (Guid)Membership.GetUser().ProviderUserKey;
User user = userRepo.Get(guid); // the function with the 13 includes
return user;
}
}
}
答案 0 :(得分:-1)
由于您在此查询中涉及的工作很少在各个页面之间重复,因此根本不需要概括查询,以便每个页面都可以调用此方法。只需让每个页面执行自己的查询,执行它所需的包含。