Skip to content
MatthieuMEZIL edited this page May 30, 2015 · 3 revisions

Calculated properties are very useful.

However, it is not always enough.

Indeed, if you want to know if a customer already bought something to an employee, you cannot use a calculated properties (because you have two parameters).

So, WAQS also has a concept of services methods for this.

For example, imagine that for an employee, you want to know if he already sold anything to a customer.

You can use this method in specifications.

Add the EmployeeSpecifications class:

public static class EmployeeSpecifications
{
    public static string GetFullName(this Employee employee)
    {
        return employee.FirstName + " " + employee.LastName;
    }

    public static bool AlreadySoldTo(this Employee employee, Customer customer)
    {
        return customer.Orders.Any(o => o.EmployeeId == employee.Id);
    }
}

Then, after running WAQS / Update Solution Generated Code AlreadySoldTo extension method becomes a method of Employee class and a method of the client context.
Note that if you use a specifications method which is not an extension one, WAQS only generates the method on the client context.

For this workshop, you will change the MainWindow and the MainWindowViewModel to support an employee selection using a combobox.

Now, if you look at the LoadCustomerAsync method, you can see that we can use the AlreadySoldTo method in the LINQ query which is pretty cool IMHO.

var customers = await (from c in _context.Customers.AsAsyncQueryable()
                       where selectedEmployee.AlreadySoldTo(c)
                       let totalSpent = c.TotalSpent
                       orderby totalSpent descending
                       select new CustomerInfo
                       {
                           Id = c.Id,
                           Name = c.FullName,
                           TotalSpent = totalSpent
                       }).ExecuteAsync();

As always, you can download the source here.