Search code examples
breeze

use of expand in breeze when mapping to DTOs on the server


I have had to move queries off the main database in order to meet requirements for complex authorization - for example a user with a given authorization role can only view data for individuals in the same institution.

I am using the Breeze .net DocCode sample for guidance, and have copied the premise for the mapping of domain models to DTOs.

get { return ForCurrentUser(Context.Orders).Select(o => new Order {
        OrderID = o.OrderID,
        ....
        OrderDetails = o.OrderDetails.Select(od => new OrderDetail
        {
            ProductID = od.ProductID, 
            UnitPrice = od.UnitPrice
            ...
        })

The problem is that which mapped properties to .include(entity framework method)/.expand (breeze method) is now a concern of the mapping function (for example, the above code will always return the OrderDetails collection, whether I want them or not). I would like to still only eagerly load/expand properties if the javascript client generated predicate has a .expand directive for that property.

Is this at all possible, or am I stuck with manually defining different mapping functions on the server, depending on what properties I want expanded? (I am happy to use tools such as automapper if that would solve or simplify the problem)

Thank you


Solution

  • You will need to use the ODataQueryOptions as a parameter to your controller method. This gives you the details of the query predicates in your server method, so that you can apply them as needed rather that having them applied automatically. This will let you expand, or not, based upon the query.

    See this answer and this answer to see how it works.