hdolder.com srl  

Software R&D
 

   hdc Home    |    Content    |    Blog    |    Director    |    Contact    |    mail us   

 

M&P BXaml in .NET, Part 3

Posted on July 30, 2013 by Herman Dolder

In the article titled " M&P H5J Declarative Composition of Concurrent Operations, Part 1" we introduced an example coded in TypeScript in wich we declaratively composed a simple "pipeline" dataflow of concurrent-asynchronous operations.

In this article we present the same example ported to .NET (coded in C#) using the same BXaml code than in TypeScript.

The following code corresponds to the BAsyncAdder BLOCK that encapsulates the concurrent operation, is in .NET.

As in TypeScript the BAsyncAdder BLOCL has four properties, three are for input (delay, qty IN) one is for output (OUT).

When a numeric value is asigned to its IN property BAsyncAdder adds the value of the qty property to it , after the time (miliseconds) specified in the delay property, asigns the result to the OUT property.

using System;
using System.Threading;
using System.Threading.Tasks;
using blkx;
using obx;

namespace bdkx
{
public class BAsyncAdder : BLOCKBase
{
public BAsyncAdder()
: base()
{
}

private int _IN;
public int IN
{
set
{
_IN = value;
DoWork();
}
}

private int _OUT;
public int OUT
{
get
{
return _OUT;
}
set
{
_OUT = value;
OnPropertyChanged("OUT");
}
}

private int _ERROR;
public int ERROR
{
get
{
return _ERROR;
}
set
{
_ERROR = value;
OnPropertyChanged("ERROR");
}
}

private int _qty = 0;
public int qty
{
set
{
_qty = value;
}
}

private int _delay = 1000;
public int delay
{
set
{
_delay = value;
}
}

private void DoWork()
{
Task.Run(async () =>
{
await Task.Delay(_delay);
return _IN + _qty;
}).ContinueWith((task) =>
{
OUT = task.Result;
});
}

public void BStart()
{
}

} // class
} // namespace

As in TypeScript the following BAsync4Adders BLOCK composes internally a pipeline dataflow of four BAsyncAdder BLOCKS.

BAsync4Adders has two properties, one is for input (IN) one is for output (ERROR). The properties IN ERROR are defined as observable properties because they will be used in databindings.

The BXaml code inside the BLOCK instantiates initializates the four BAsyncAdder BLOCKS binds them using BXaml default "OneWay" normal Databindings. The Bindings are maid using BXaml Markup Extensions.

using System;
using System.Threading;
using System.Threading.Tasks;
using blkx;
using obx;

namespace bdkx
{
public class BAsync4Adders : BLOCKBase
{
public BAsync4Adders()
: base()
{
CreateInnerBlocks();
}

dynamic xx;

void CreateInnerBlocks()
{
xx = OB.XA.Load Start(this, XamlCode);
}
private int _IN;
public int IN
{
get
{
return _IN;
}
set
{
_IN = value;
OnPropertyChanged("IN");
}
}

private int _OUT;
public int OUT
{
set
{
_OUT = value;
OnPropertyChanged("OUT");
}
}

public void BStart()
{
}

public void Result(int res)
{
OB.EnqueueUIT(() =>
{
System.Windows.Forms.MessageBox.Show("RESULT = " + res);
});
}

private string XamlCode =
"<mpXaml " +
" xmlns:bdk='bdkx' " +
" xmlns:blk='blkx' " +
" > " +
"<blk:BRegion > " +
" <bdk:BAsyncAdder id='ADDR1' " +
" qty='2' " +
" delay='100' " +
" IN='{cls:`Binding`, SourcePath:`IN` }' " +
" /> " +
" <bdk:BAsyncAdder id='ADDR2' " +
" qty='4' " +
" delay='200' " +
" IN='{cls:`Binding`, SourcePath:`ADDR1.OUT` }' " +
" /> " +
" <bdk:BAsyncAdder id='ADDR3' " +
" qty='8' " +
" delay='300' " +
" IN='{cls:`Binding`, SourcePath:`ADDR2.OUT` }' " +
" /> " +
" <bdk:BAsyncAdder id='ADDR4' " +
" qty='16' " +
" delay='400' " +
" IN='{cls:`Binding`, SourcePath:`ADDR3.OUT` }' " +
" /> " +
" <blk:BPropTrigger ActionPath='Result' PropertyPath='ADDR4.OUT' /> " +
"</blk:BRegion> " +
"</mpXaml> ";

} // class
}// namespace

When we execute

BAsync4Adders xx = new BAsyncdders();
xx.BStart();
xx.IN = 100;

the final result displayed after 1000 milliseconds is130.

This first example

  • does not consider exceptions handling.
  • includes a binding between a property of the container BLOCK and a property of one the inner (contained) BLOCKS ("ADDR1").
    shows the use of a BPropTrigger (Property Trigger) BLOCK.
  • shows a Full Declarative Composition in which the order of the definitions of the internal BLOCKS are not significative because the precedence of the operations are defined by the Bindings.

Comments

HTML5, Typescript and BXaml constitute a Trilogy that puts the Javascript environment at the same level of abstraction and functionality than the .NET environment facilitating the "TwoWay" portability of concepts and applications between the two platforms.

The "Potential Difference" (voltage) between the two platforms is significantly reduced by the availability of Lambda Expressions and Closures in both, and by the incorporation of dynamic elements in .NET: DynamicObjects, ExpandoObjects and the "dynamic" datatype.

The greatest differences of potential exist in three areas:

  • the quality of the development tools available.
  • the quality of the Controls available for the User
    Interfaces.
  • the databinding facilities.

For M&P H5J we have adopted the scheme of databinding of XAML (in Javascript most of the databinding schemas evolve from AJAX and are not XAML-friendly).

The availability of BXaml on both platforms is a bridge for the "TwoWay" portability of the M&P applications, especially at the conceptual and logical levels (interfaces). At the physical (code) level the equivalent BLOCKS have differences due to the idiosyncrasies of each platform and their different facilities for optimizations.

The Trilogy HTML5, TypeScript, BXaml provides a natural ecologic environment for the implementation of M&P applications in the ultraportable Javascript environment.

See also

 

 

TBW The BLOCKS World 2015  hdolder.com srl
C30731
2015-06-26