Home > .NET Domain Model, Developing Using CodeFluent Entities > Computed Properties and Computed Format

Computed Properties and Computed Format


Computed Properties

Say you have a Customer entity which among other properties has a property FirstName and a LastName.

For data binding purposes in your UI, you’d probably like to have an extra FullName property, however, you certainly don’t want to persist it.

Using CodeFluent Entities you can declare what we call computed properties meaning that they’re not persistent and are computed at run time.

You can declare a computed property by setting the “Is Computed” property to true in the Modeler, are setting the computed attribute to true in XML.

image

As explained in the documentation, the computed attribute is in fact an alias to the following settings:

  • not persistent,
  • no relation,
  • has an OnGet event rule.

In practice here’s how we’d define our FullName property in the model (XML view):

<Customer>
<Id />
<FirstName />
<LastName />
<FullName computed="true" />
<cf:snippet>
private void OnGetFullName()
{
           _fullName = FirstName + LastName;
}
</cf:snippet>
</Customer>

Note: you don’t have to create a snippet, you could also define the OnGetFullName in a partial class.

 

Computed Format

The Computed Format attribute is in fact exactly for those kind of scenarios, so you don’t have to implement the OnGet<PropertyName> method anymore. This attribute is available in the Business Object Model Producer namespace (http://www.softfluent.com/codefluent/producers.model/2005/1) and whose prefix is “cfom”.

Note: you can find all xml namespaces and prefixes on the following page: http://www.codefluententities.com/documentation/webframe.html?CodeFluent_Schemas.html.

cfom:computedFormat assumes the target object implements the cool and versatile (but little known) System.IFormattable interface, and it just so happens that CodeFluent Entities provides a built-in aspect (a.k.a. CodeFluent Pattern) named AutoFormattable which adds the IFormattable implementation to all specified entities (requires .NET 3.5).

Here’s our model now (XML view):

<cf:project xmlns:cf="http://www.softfluent.com/codefluent/2005/1"
            xmlns:cfom="http://www.softfluent.com/codefluent/producers.model/2005/1"
            xmlns:af="http://www.softfluent.com/codefluent/patterns/autoformattable/2008/9"
            af:autoFormattable="true" defaultNamespace="Sample">

   <cf:import path="%CF_PATTERNS_PATH%\SoftFluent.AutoFormattable.xml" runTemplate="true" step="Methods" />
 
  <Customer>
    <Id />
    <FirstName />
    <LastName />
    <FullName computed="true" cfom:computedFormat="{FirstName} {LastName}" />
  </Customer>
 
</cf:project>

Et voilà! It has exactly the same result as the first method, however I didn’t have to implement the OnGet<PropertyName> method myself, plus more globally, all my entities implement the IFormattable interface now.

  1. Antoine
    August 5, 2011 at 9:14 am

    Hello,

    Is it possible to set the attribute “cfom:computedFormat” using the CFE Modeler Edition?

  2. Carl
    August 5, 2011 at 12:16 pm

    Hi,

    Two ways using the Modeler:
    1. Obviously, you can open the XML view and add it,
    2. When adding producers, its namespace is automatically added, so once you added the Business Object Model Producer, the cfom namespace will be added and available properties will appear in the property grid, in the “Aspect Properties” tab.
    I admit, that the tab name can be a little misleading, it might be renamed in future version since this tab can contain any extra-properties may they be introduced by aspects or producers.

    Regards,
    Carl

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s