如何强制 RIA 服务在一个查询方法中包含子实体,但不在另一种查询方法中包含子实体

发布于 2024-11-03 17:30:12 字数 1553 浏览 2 评论 0原文

场景如下:

我在“组”和“用户”之间建立了关联,由“UserGroupAssignment”对象表示。

public class UserGroupAssignment
{
  [Key]
  public virtual long Id { get; set; }

  [Association("UserAssignmentToUser", "UserId", "Id", IsForeignKey = true)]
  public virtual User { get; set; }  

  [Association("UserAssignmentToGroup", "GroupId", "Id", IsForeignKey = true)]
  public virtual Group { get; set; }    

  public virtual bool IsPrimary { get; set; }    

  public virtual DateTime? ValidFrom { get; set; }    

  public virtual DateTime? ValidTo { get; set; }    
}

我有两个业务逻辑方法,GetUserAssignmentsForGroups 和 GetGroupAssignmentsForUsers,我分别返回分配的 User 和 Group 属性。即 GetUserAssignmentsForGroup 接受一个 GroupId 并返回填充了 User 属性的该组的分配,

我想要的是将这两个方法公开为域查询方法,例如。所以:

[Query]
public IQueryable<UserGroupAssignment> GetAssignmentsForGroupWithUsers(long groupId)
{
  return this.businessLogic.GetUserAssignmentsForGroups(groupId);
}

[Query]
public IQueryable<UserGroupAssignment> GetAssignmentsForUserWithGroups(long userId)
{
  return this.businessLogic.GetGroupAssignmentsForUsers(userId)
}

我的问题是,虽然业务逻辑方法通过 NHibernate 返回正确填充的分配,但 RIA 服务并未通过线路传递子实体(用户或组),

我不想在网络上使用 [Ininclude] 属性 。 UserAssignment 类的用户或组属性,因为我想最大限度地减少线路上的有效负载 - 例如,当我只对每个 UserAssignment 的用户感兴趣时,我不想发送组,

所以我的问题是 。这:

我如何告诉 RIA 服务 明确包含用户子实体 在一个域中查询方法和组 另一个中的子实体?

请记住,我在后端使用 NHibernate,并在 RIA 服务中使用自定义查询方法,因此无法在客户端查询中使用 EF 样式包含。

谢谢乔尔

Here's the scenario:

I've got an association between "Groups" and "Users, represented by a "UserGroupAssignment" object.

public class UserGroupAssignment
{
  [Key]
  public virtual long Id { get; set; }

  [Association("UserAssignmentToUser", "UserId", "Id", IsForeignKey = true)]
  public virtual User { get; set; }  

  [Association("UserAssignmentToGroup", "GroupId", "Id", IsForeignKey = true)]
  public virtual Group { get; set; }    

  public virtual bool IsPrimary { get; set; }    

  public virtual DateTime? ValidFrom { get; set; }    

  public virtual DateTime? ValidTo { get; set; }    
}

I have two business logic methods, GetUserAssignmentsForGroups and GetGroupAssignmentsForUsers that I return the assignments with the User and Group properties populated respectively. i.e. GetUserAssignmentsForGroup takes a GroupId and returns the assignments for that Group with the User property populated.

What I want is to expose those two methods as domain query methods like so:

[Query]
public IQueryable<UserGroupAssignment> GetAssignmentsForGroupWithUsers(long groupId)
{
  return this.businessLogic.GetUserAssignmentsForGroups(groupId);
}

[Query]
public IQueryable<UserGroupAssignment> GetAssignmentsForUserWithGroups(long userId)
{
  return this.businessLogic.GetGroupAssignmentsForUsers(userId)
}

My problem is that whilst the business logic methods return the correctly populated Assignments via NHibernate, RIA Services is NOT passing the sub-entities (User or Group) across the wire.

I don't want to use [Include] attributes on the User or Group properties of the UserAssignment class, as I want to minimise the payload over the wire - I don't want to send the group over when I'm only interested in the User of each UserAssignment, for example.

So my question is this:

How do I tell RIA services to
explicitly include User sub-entities
in one domain query method and Group
sub-entities in the other?

Remember, I'm using NHibernate at the back end and custom query methods in the RIA Services, so can't use the EF-style include in the client query.

Thanks

Joel

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

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

发布评论

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

评论(2

梦初启 2024-11-10 17:30:12

您应该在元数据类中应用 [Ininclude] 属性。然后创建一个域服务方法来获取不包含属性的数据,以及一个单独的方法来获取包含属性的数据。

您可能会发现此帖子有助于理解[包含] 属性有效。

you should apply the [Include] attribute in the metadata class. then create one domain service method for fetching data without properties included, and a separate method for fetching data with properties included.

You might find this thread helpful in understanding how [Include] attribute works.

演出会有结束 2024-11-10 17:30:12

老问题,但仍然很有趣。你找到解决办法了吗?

据我所知,WCF RIA 架构并不是那么容易。
一种简单而肮脏的方法可能是重写 Query 方法,强制返回 IQueryable 的枚举(我猜您正在使用 LINQ to nHibernate,在这种情况下,祝您好运),然后检查 HttpContext (您正在使用 WCF RiaServices因此您必须打开 aspNetCompatibility)并将您不想通过线路发送的引用(用户或组)设置为 null。
无论如何,这种方式迫使您使用 [IncludeAttribute]。但是,我没有看到任何合理的路线可以避免使用它,并且这种方式允许您在需要时通过线路发送实体。

IMO 我相信,为了完全避免使用 [Ininclude],您必须推出自己的序列化器服务器端和反序列化器客户端,或者更改 UserGroupAssignment 实体,以便用户属性成为包含您决定的序列化用户(或组)的字符串根据您的方法评估或不评估。

如果您已经找到解决方案,请告诉我们,这个问题很有趣。

Old question, but still interesting. Did you find a solution ?

As far as I know of WCF RIA Architecture it isn't so easy.
An easy and dirty way could be to override the Query method, force the enumeration of the IQueryable being returned (I guess you're using LINQ to nHibernate, in which case, good luck) then examine the HttpContext (you're using WCF RiaServices so you MUST have aspNetCompatibility turned on) and set to null the reference that you don't want to send over the wire (User or Group).
Anyway this way FORCE you to use the [IncludeAttribute]. However I don't see any reasonable route that avoid its use, and this way allow you to send the entity over the wire just when you need to.

IMO I belive that in order to totally avoid the use of [Include] you must rollout your own serializer serverside and deserializer clientside or change the UserGroupAssignment entity so that the user property become a string containing the serialized User (or Group) that you decide to valorize or not according your method.

Please let us knows if you already found a solution, the question is interesting.

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