WAQS: Service methods

7 reasons to use WAQS

WAQS documentation

 

A service method in WAQS vocabulary is neither a calculated property, nor a metadata definition nor a validation method.

Service methods could be used to define some process.

So for example, you could define a method MakeInvoice that makes an invoice from an Order.

public static Invoice MakeInvoice(this Order o)
{
    var invoice = new Invoice
{
OrderId = o.Id,
CustomerId = o.CustomerId,
Total = GetTotal(o),
Dicount = GetDiscount(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;
}


From this code, WAQS generates a MakeInvoice method in Order class on the client and a method in the client context with the Order as parameter that calls the first one.



A service method is not necessary an extension method but, anyway, it’s a static method. If it’s only a static method, WAQS just generates a method in the client context.



Note that for polymorphism, it must be an extension method.



Also note that you have to be very vigilant with service methods that take entities as parameter or return entities. I will explain why later.



 



Great! But we often want to define this kind of method in the server (SOA).



With WAQS, if you use the DAL context or the service (I will write about it at the end of this post) or if you use NotApplicableOnClient attribute, WAQS only generates a method awaitable in the client context. For example, imagine that you want to persist the new Invoice in your MakeInvoice method. You can do it like this:



public static Invoice MakeInvoice(this Order o, INorthwindWAQSEntities context)
{

    var invoice = new Invoice
        {
           OrderId = o.Id,
           CustomerId = o.CustomerId,
           Total = GetTotal(o),
           Dicount = GetDiscount(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)
        });
    context.Invoices.Add(invoice);
    context.SaveChanges();
    return invoice;
}



Now, on the client, we just have an awaitable method in the client context that we can call like this:



await _context.MakeInvoiceAsync(o);



 



In my sample, I used the DAL context (INorthwindWAQSEntities) but I could also use the service (INorthwindService).



If you are sure about what you are doing you can use the DAL context. Using the service instead includes all validation rules in the saving process.



 



Note that because of WCF usage, service methods name must be unique.

This entry was posted in 16868, 17895. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>