WAQS Querying: IncludeFor

7 reasons to use WAQS

WAQS documentation

 

Imagine that you want to calculate a calculated property in memory or you need to execute a service method.

As I previously explain, I don’t want to have lazy loading. So it’s very important to load all navigation properties need to execute the expected code.

So, for this scenario, WAQS introduces the IncludeFor method.

 

Analyzing specification code, WAQS deduces the dependences of each method defined in the specifications.

Based on it, WAQS can add the different includes.

 

So for example, imagine that we use the first MakeInvoice method I defined here:

public static Invoice MakeInvoice(this Order o)
{
     Invoice invoice = new Invoice { OrderId = o.Id, CustomerId = o.CustomerId, Total = GetTotal(o) };
     if (o.Customer != null)
     {
         invoice.CustomerCompanyName = o.Customer.CompanyName;
         invoice.CustomerContactName = o.Customer.ContactName;
     }
     foreach (var od in o.OrderDetails)
         invoice.InvoiceDetails.Add(
new InvoiceDetail { OrderDetailId = od.Id, Quantity = od.Quantity, UnitPrice = od.UnitPrice, Discount = od.Discount, Amount = GetAmount(od) });
     return invoice; }

 

Now, we want to get orders and we want to be able to make their invoice. We can use this query:

await _context.Orders.AsAsyncQueryable().IncludeForMakeInvoice().Execute()

 

With this IncludeForMakeInvoice generated method:

public static IAsyncQueryable<Order> IncludeForMakeInvoice(this IAsyncQueryable<Order> query)
{
     bool done = false;
     IncludeForMakeInvoiceCustomQueryable(ref query, ref done);
     if (done)
       return query;
     query = query.IncludeCustomer().IncludeOrderDetailsWithExpression (e2 => e2.WithAmount()).WithTotal();      return query; } static partial void IncludeForMakeInvoiceCustomQueryable(ref IAsyncQueryable<Order> query, ref bool done);
Published Sun, Feb 16 2014 3:55 by Matthieu MEZIL

Leave a Comment

(required) 
(required) 
(optional)
(required) 
If you can't read this number refresh your screen
Enter the numbers above: