WAQS: Entity calculated properties

7 reasons to use WAQS

WAQS documentation

 

WAQS server generation created a Specifications Folder. It’s where we will define (by default) our business code.

As I previously wrote, the idea is to write our business code outside the rest of the code. So it won’t be possible to extend entities class using partial keyword.

In our case, we will use static methods.

WAQS is based on convention. This way homogenizes the business code potentially written by many developers.

Calculated properties must start with “Get” and been an extension method on an entity (with only this entity as method parameter).

For example, if we want to define a CustomerName property for Customer type which is the aggregation of CompanyName and ContactName, we can write this:

public static class NorthwindSpecifications
{
    public static string GetCustomerName(this Customer c)
    {
        return string.Concat(c.CompanyName, " - ", c.ContactName);
    }
}


After transforming T4 templates, we now have a property CustomerName generated on the client Customer class and its get has our code:



public string CustomerName
{
    get
    {
if (Specifications != null && Specifications.HasCustomerName)
         return Specifications.CustomerName;
        return string.Concat(this.CompanyName, " - ", this.ContactName);
    }
   
set
    {
        throw new System.InvalidOperationException();
    }
}


I will explain later the if and the set.



 



Now imagine that you add this new calculated property



    public static string GetCustomerName(this Order o)
    {
        return o.Customer.GetCustomerName();
    }


In this case, the generated property is the following:



public string CustomerName
{
    get
    {
        if (Specifications != null && Specifications.HasCustomerName)
            return Specifications.CustomerName;
        if (this.Customer == null)
            return default (string);
        return this.Customer.CustomerName;
    }
    set
    {
        throw new System.InvalidOperationException();
    }
}


So, as you can see, WAQS adds test to avoid NullReferenceException.

This entry was posted in 16868. 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>