beamish
beamish

Reputation: 2827

How to use Paging in breeze on expanded entities?

I'm writting a web application using EF on the server side and breeze on the client side.

I have the following classes defined in EF:

public class Product
{
   [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }
    public string Name { get; set; }
    [InverseProperty("Product")]
    public virtual ICollection<Property> Properties { get; set; }
}

public class Property
{
    [Key]
    public int Id { get; set; }
    public string Type { get; set; }
    public string Name { get; set; }
    public string DefaultValue { get; set; }
    [ForeignKey("Product")]
    public int ProductId { get; set; }
    public Product Product { get; set; }
}

public class PropValue
{
    [Key]
    [Column(Order = 0)]
    [InverseProperty("RecordId")]
    public int RecordId { get; set; }
    public Property Record { get; set; }
    [Key]
    [Column(Order = 1)]
    public int PropertyId { get; set; }
    public Property Property { get; set; }
    public string Value { get; set; }
}

These classes allows me to create at runtime various 'Products' each having any number of 'Properties' (the name of the property is in 'Property', the value of the property is in 'PropValue').

On the client side, I need to retrieve the Products in breeze as a list of items, where each item holds the 'Id' and the 'Name' from the 'Product' table as well as the properties relevant to that item, both from the 'Property' table (e.g. 'Name' of the property) as well as from the 'PropValue' table (particularly 'Value' holding the value for that property). I also need the items to be filtered by certain conditions, such as only 'Products' having some 'Property' with a 'Value' greater than 0 etc.

This can be done by creating a breeze query with 'expand' to retrieve data from all tables, such as:

var resultsQuery = new breeze.EntityQuery()
    .from("Records")
    .where(Predicate.and(andArray))
    .expand("propValues,product,product.properties")

where the 'andArray' is an array of breeze Predicates each defined as something similar to:

Predicate.create("propValues", "any", Predicate.create("propertyId", "==", propID).and("value", ">", val)

and always having the following Predicate in the 'andArray' to retrieve only properties relevant to the product:

Predicate.create("productId", "==", productId)

using this approach, the code on the server side is:

[HttpGet]
[AllowAnonymous]
[EnableBreezeQuery(AllowedQueryOptions = AllowedQueryOptions.All)]
public IQueryable<Record> Records()
{
    return _contextProvider.Context.Records;
}

It is important for me to use the caching mechanism breeze provides so that I can retrieve the data from the remote server only once.

The problem with the above approach is that I need to use paging: orderby, take and skip. For example I want the results ordered by the value of a certain property, divided in pages each having 5 items, and I only want the 2nd page. The above breeze query has no paging in it, and I cannot see how to add paging to this query.

Is it possible? How can I do that?

Is it possible to create a breeze query on the client side that will enable this? that would be best since I can do the filtering via breeze (using 'Predicate' as shown in the example above) and rely on breeze to cache the data.

Or do I need to create some code on the server side that will combine the various fields from the above 3 tables into a list of items, possibly as JSON objects, that are filtered on the server side and not on the client side? and will that list be cached?

If possible I will greatly appreciate a simple example code for the server and the client.

Thank you !

Upvotes: 0

Views: 86

Answers (0)

Related Questions