nHibernate,我可以告诉它 Fetch,但我可以告诉它 Stay吗?

发布于 2024-10-25 06:09:16 字数 1123 浏览 0 评论 0原文

正如问题所述,我知道使用 NHinbernate 我可以告诉 Fetch(Func) 甚至 FetchMany() 的特定查询。但如果我想反过来怎么办?

例如,假设我们有一个班级..

class Employee {
  public virtual string Name { get; set; }
  public virtual Address Address { get; set; }
  public virtual double Salary { get; set; }
}

如果Employee正在查看自己的个人资料,我会想要AddressSalary被渲染。但如果有不同的员工在寻找怎么办?构建一个 ASP.NET MVC 视图似乎更方便,但专门不返回需要隐藏的数据。就像..

if( // myself // ) {
   return employee = session.Query<Employee>()
       .Fetch(context => context.Address)
       .Take(1)
       .SingleOrDefault();
}
else
   return employee = session.Query<Employee>()
       .Deny(context => context.Address)
       .Deny(context => context.Salary)
       .Take(1)
       .SingleOrDefault();

那么我的视图可能看起来像..

@model Employee

<h2>@Model.Name</h2>
<h4>@Html.DisplayFor( model => model.Address )</h4>
<h4>@Model.Salary</h4>

我意识到这不是宇宙中最好的例子,但是这样的事情可能吗?到目前为止,我还没有找到任何明确告诉对象不要返回的方法。

Just as the question states, I know that using NHinbernate I can tell a specific query to Fetch(Func<T,bool>), and even FetchMany(). But what if I want to do the other way around?

For instance, let us say that we have a class ..

class Employee {
  public virtual string Name { get; set; }
  public virtual Address Address { get; set; }
  public virtual double Salary { get; set; }
}

If an Employee is looking at their own profile, I would want Address and Salary to be rendered. But what if a different employee is looking? It would seem more convenient to build one ASP.NET MVC View, but to specifically not return the data that needs to be hidden. So like ..

if( // myself // ) {
   return employee = session.Query<Employee>()
       .Fetch(context => context.Address)
       .Take(1)
       .SingleOrDefault();
}
else
   return employee = session.Query<Employee>()
       .Deny(context => context.Address)
       .Deny(context => context.Salary)
       .Take(1)
       .SingleOrDefault();

Then my View might look like ..

@model Employee

<h2>@Model.Name</h2>
<h4>@Html.DisplayFor( model => model.Address )</h4>
<h4>@Model.Salary</h4>

I realize this isn't the best example in the universe, but is such a thing possible? I haven't found any methods that explicitly tell an object not to return, so far.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

¢好甜 2024-11-01 06:09:16

与往常一样,ASP.NET MVC 中所有问题的答案都是:视图模型。因此,您获取存储库中的所有内容并返回包含所有属性的域 Employee 模型,然后将此 Employee 模型映射到 EmployeeViewModel 。然后这个视图模型将被传递给视图。当控制器根据用户在模型和视图模型之间映射时,它可能包含或不包含某些属性。

As always the answer to everything in ASP.NET MVC is : view models. So you fetch everything in your repository and return a domain Employee model containing all properties and then you would map this Employee model to an EmployeeViewModel. It is then this view model that will be passed to the view. And when the controller maps between the model and the view model depending on the user it might include or not some properties.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文