有条件的包含取决于当前页面是一个好习惯吗?
Is conditional includes depending on the current page a good practise?
在我的网站中,每个 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
}
这是一种不好的做法吗?
如果是,是否有任何适当的解决方案来进行包含大量包含 speeder 的查询?
非常感谢!
编辑
这里是当前 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;
}
}
}
由于在不同页面之间重复此查询所涉及的工作很少,因此根本没有必要概括查询以便每个页面都可以调用这一个方法。只需让每个页面执行自己的查询,执行它需要的包含。
在我的网站中,每个 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
}
这是一种不好的做法吗? 如果是,是否有任何适当的解决方案来进行包含大量包含 speeder 的查询?
非常感谢!
编辑
这里是当前 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;
}
}
}
由于在不同页面之间重复此查询所涉及的工作很少,因此根本没有必要概括查询以便每个页面都可以调用这一个方法。只需让每个页面执行自己的查询,执行它需要的包含。