RIA Services and Multiple / Dynamic Include Strategies

As an example, assume the following simple model:

public class Order
{
    public List<LineItem> LineItems { get; set; }
    public List<Fee> Fees { get; set; }
}

public class LineItem { }
public class Fee { }

      

With RIA services, if I want to get an order and include all of its items in the same network call, I can statically place the [Include] attribute in the above LineItems collection. This works great for one scenario, but what happens when I need multiple "included strategies"?

For example, one situation might require the inclusion of a payment collection and NOT a LineItems collection. Is there a way with RIA Services to control what's included at runtime without overriding your model and / or creating dtos with attributes statically allocated for each use case?

+2


a source to share


3 answers


Attributes

[Include] only work if the property is included in the Entity Framework (or whatever you are using). Therefore, while you cannot set [Include] based on the current script, you can control which objects are included by setting .Include on EF query. So instead of a single function called GetProducts on your DomainService, you will probably have more (GetProductsWithComments, etc.) that will be different, including the set in the EF request (see Jonx's answer).



0


a source


This is best done with views. If you cannot make views, you can create your own POCO objects / classes.

Since POCO classes do not exist in your domain model, there are a few things you need to do to get them to work with ria services.

  • Since ria is just a WCF form with entity serialization, POCO classes must be decorated with the [DataContract] attribute.
  • Any member of the POCO class must be decorated with [DataMember]
  • At least one property of the POCO class must have a [Key] attribute (System.ObjectModel.DataAnnotations) and must be unique to validate the Key validation.


Finally, to be able to use these poco classes in your service, at least one service method must return the IEnumerable or IQueriable of that poco class.

With this knowledge, you can create custom objects to represent the material hierarchy required for the user interface. The downside is that doing CRUD using these objects is a bit tricky. These objects are more commonly used for display.

Next, I would suggest you mark your ria service as partial and any custom service code you write to add it to another partial class that implements the service .... (save you the pain of the world when you update your domain model and restoring wcf ria service ...)

0


a source


You would do like this:

var product = _productRepository.GetProductSet()
.Include("Tags")
.Include("Attachments")
.Include("Comments")
.Include("Comments.User")
.Include("Comments.User.UserDetails")
.FirstOrDefault(p => p.ProductId == productId);

      

-1


a source







All Articles