Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force RIA Services to include a sub-entity in one Query method but not another

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

like image 802
Rammesses Avatar asked Nov 14 '22 00:11

Rammesses


1 Answers

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.

like image 196
Yeonho Avatar answered Jan 19 '23 00:01

Yeonho