03 Aug 2009 @ 8:42 AM 

Erik Meijer, the man behind LINQ is now come up with a framework called “Rx Framework” which contains API those decorates LINQ2Objects as mathematics dual.  Let us see in details.

LINQ2O and Mathematics Dual

Wikipedia says “A moprhism f:A->B is a monomorphism if  f.g=g.h implies g = h. Performing the dual operation, we get the statement that g.f = h.f  implies g = h. for a morphism f: B->A. This is precisely what is means for f to be an epimorphism. In short, the property of being a monomorphism is dual to the property of being an epimorphism.”

 LINQ to Objects is set of extension methods which enables to manipulate on a IEnumerable<T>.  From a layman point of view, a collection is nothing but a data source from where we are pulling data using LINQ to Objects and/or ofcourse for…each.  From duality, this is f:A->B.

It is very common that we need to notify a data source when new items need to be added or existing item to be updated either.  The interaction is happened either sychronously, but in general asychronously.  Since, we are living in non-deterministic, disconnected programming world.

Updating a data source would be based on some user-interaction in an application through event.  Event is programming world idiom for asynchronous invocation, or if you take any non-user interaction application which is again based on “event” driven architecture.  This model is based on GoF’s observer pattern.

Observer Pattern

GoF says “Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically”.

Observer Pattern

The observer those want to be notified whenever the subject undergoes a change in state should attach themselves with subject.  If you take button click event, we can write one or more event handlers.  Whenever a button clicked, it sends EventArgs to its subscribed observers (event handlers).

Introducing IObservable and IObserver

Let us  come back to the actual problem.  Since, IEnumerable<T> can only be used to pull data from a data source as read-only i.e f:A->B, here A is data source and B is my IEnumerable<T>.  However If I want  f: B->A which means that I would like to send/push data from “B” to “A”.  Here, I treat the callback or event handlers as “A” and the events as “B”.  It means that I need to push data to a data source as like as IEnumerable which is for pull.

As part of Reactive Framework, Meijar introduced two interfaces IObservable<T> and IObserver<T>.  Based on observer pattern, IObservable<T> is source.  See the following figure.

IObserverable and Observer

The  Subscribe() method is used to register one or more IObservers for notification which is similar to Subject.Attach() in the observer pattern.  The IDisposable is .NET idiom which mimics Source.Detach().  To understand the fundamental objective of these objects think in reverse in place with IEnumerable.  To traverse through the IObservable, create an IObserver, give it to an IObservable, and the IObservable “pushes” data into the IObserver by invoking its methods.

OnNext() is used to iterate over data sources and push the argument “value”.  OnCompleted() is post-iteration handler.

An Example

Theory is enough.  Let us see an example in Silverlight 3.0.  I create a button with mouse move event which acts as IObservable and create a TextBox which acts as IObserver.  Whenver a mouse moved on the button, the textbox observer appends coordinates into TextBox.Text property.

To create a mouse move observable, I used System.Linq.Observable which is used to create IObservables.  I extend the System.Windows.Controls.Button with GetMouseMoves() which returns IObservable<Event>MouseEventArgs>> like the following:

public static class ButtonExtension
{
    public static IObservable>
      GetMouseMoves(this Button button)
    {
        return Observable.FromEvent((EventHandler genericHandler)
          => new MouseEventHandler(genericHandler),
            mouseHandler => button.MouseMove += mouseHandler,
            mouseHandler => button.MouseMove -= mouseHandler);
    }
}

 The FromEvent() overload takes three Action(s) as arguments. One to convert from a generic event handler (EventHandler) to MouseEventHandler.  Remaining two register/unregister.  For observers, I create a custom observer for handling mouse coordinates like

public class CoordinateObserver : IObserver<Event<MouseEventArgs>>
{
    public TextBox CoordinateTextBox { get; set; }
 

    #region IObserver<Event<MouseEventArgs>> Members

    public void OnCompleted()
    {

    }

    public void OnError(Exception exception)
    {

    }

    public void OnNext(Event<MouseEventArgs> value)
    {
        CoordinateTextBox.Text += (string.Format(&quot;Clicked at ({0},{1})\n&quot;,
          value.EventArgs.GetPosition(CoordinateTextBox).X,
          value.EventArgs.GetPosition(CoordinateTextBox).Y));
    }

    #endregion
}

I create an instance of CoordinateObserver and subscribes it to button’s GetMouseMoves()

rxButton.GetMouseMoves().Subscribe
  (new CoordinateObserver { CoordinateTextBox = this.rxResult });

The results would be

 The output

Download and Resources

Till now, there is no official release of Reactive Framework.  However, The Silverlight 3.0 Unit Test Framework uses this which comes as part of Silverlight 3.0 Toolkit.  Assembly Name: System.Reactive.dll, which unfortunately builds with Silverlight’s System.Core, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e.  See the following image:

Assembly

  1. First download Silverlight 3.0 Toolkit and install it.
  2. Extract source code from $Program Files$\Microsoft SDKs\Silverlight\v3.0\Toolkit\Jul09\Source\Source Code.zip.
  3. Get the System.Reactive.dll from $Source Code Folder$\Binaries.

The full source code of the example is available at http://www.udooz.net/index.php?option=com_docman&task=doc_download&gid=5&Itemid=5.

Tags Tags: , , , ,
Categories: .NET, Silverlight
Posted By: udooz
Last Edit: 03 Aug 2009 @ 08 58 PM

EmailPermalinkComments (4)

Problem

Consume the application logic from presentation tier with domain entities those are specialized for this tier with minimal plumbing.

Forces

  • Set of code (validation, authentication, etc) to be written specific to presentation tier and the same needs to be written specific to business tier.
  • Automated way to share the logic between the tiers with trusted boundaries.
  • Agile way to consume business logics to RIA (particularly Silverlight) applications.
  • Line of RIA based business application those are not required to scale of SOA.
  • Data driven applications those are again not required to scale of SOA.

Solution

.NET RIA Services framework supports end-to-end use of data from DAL (data access layer) of your choice to presentation tier.  The data can be shaped for presentation tier.  This framework was built on ASP.NET to define and support a pattern for exposing a set of operations on your domain object to presentation tier particularly Silverlight.  A domain object contains set of CRUD and custom business operations with domain entities.  In this framework, the domain object is called as “DomainService”.  Once you define a DomainService at business tier based on the data from database using ADO.NET Entity Framework or LINQ2SQL, this framework generates code corresponding client tier code that can be used for data binding, validation, authentication, etc.

The following image which was taken from .NET RIA Services guide gives you to understand how this framework is designed to scale across different presentation technologies and DAL components.

.NET RIA Services

References

Tags Tags: , , ,
Categories: Silverlight
Posted By: udooz
Last Edit: 20 Mar 2009 @ 09 10 AM

EmailPermalinkComments (1)
 27 Dec 2008 @ 11:00 AM 

Inside AppManifest.xaml

The file contains the following:


<Deployment xmlns="

http://schemas.microsoft.com/client/2007/deployment"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

EntryPointAssembly="HelloSilverlightApp"

EntryPointType="HelloSilverlightApp.App"

RuntimeVersion="2.0.31005.0">

  <Deployment.Parts>

    <AssemblyPart x:Name="HelloSilverlightApp"

Source="HelloSilverlightApp.dll" />

  </Deployment.Parts>

</Deployment>

The manifest specifies the entry point assembly which should be loaded by the Silverlight host and within the assembly it requires a type derived from System.Windows.Application for start executing the application.

Here, the assembly is “HelloSilverlightApp.dll” and type is “App” in the namespace HelloSilverlightApp.

Warning: System.Windows.dll is part of Silverlight and not the one we used for WinForm development.

I read in some web sites those explained that XAP also contains a set of required Silverlight assemblies which are needed by this application. But I tried with lot of examples, the XAP contains application’s assembly and this manifest. Need to check.


Inside HelloSilverlightApp.dll Assembly

It contains two parts:

  1. The typical .NET type declaration of specific Siverlight application’s App and one or more Page. In this example,

    1. HelloSilverlightApp.App

    2. HelloSilverlightApp.Page

  2. Resource: HelloSilverlightApp.g.resources. It contains the declarative artifacts of this application:

    1. page.xaml

    2. app.xaml

Tags Tags: , ,
Categories: Uncategorized
Posted By: udooz
Last Edit: 27 Dec 2008 @ 11 00 AM

EmailPermalinkComments (0)
 27 Dec 2008 @ 7:06 AM 

I would like to share some of my basic understanding of Silverlight internals in this series.

XAP File

When you build a Silverlight 2.0 application, the final outcome would be:

1. ApplicationName.dll

2. AppManifest.xaml

3. ApplicationName.xap

When a request is made for a page which contains a Silverlight, the server sends the page with the Silverlight part as “xap” file to the client. The XAP is a normal archive file you use archive manager like 7-Zip to extract the content. It contains RequestSilverlightApp.dll and AppManifest.xaml. The silverlight CLR at the client side consumes these. This should be specified in either ASPX or HTML file which is the actual client requested page.

You use OBJECT tag along with DIV and IFRAME to load Silverlight host and specify the target XAP needs to be loaded.








<object data="data:application/x-silverlight,"

type="application/x-silverlight-2" width="100%" height="100%">

<param name="source" value="HelloSilverApp.xap"/>

In your ASPX, you can use ASP.NET server side control to specify Silverlight host and specify the target XAP. In the above declaration, the XAP file has been specified in the “source” parameter.


<asp:Silverlight ID="Xaml1" runat="server"

Source="~/ClientBin/HelloSilverApp.xap" MinimumVersion="2.0.31005.0"

Width="100%" Height="100%" />

In the above declaratin, the XAP file has been specified in the “Source” attribute.

Tags Tags:
Categories: Uncategorized
Posted By: udooz
Last Edit: 27 Dec 2008 @ 07 06 AM

EmailPermalinkComments (0)
 26 Oct 2008 @ 3:39 AM 
Since I was mostly spent my time on behind-the-scene technology, I had put a hold on “Silverlight”. Also, I am waiting for its final customer release.

Installed all Silverlight ingredients on my Visual Studio 2008 box successfully. Struck up with VS2008 SP1 update, because of its online 781MB installar size. Finally, I got an offline installar and finished all formalities.

I was starting the journey with Silverlight Web Script project. Created the following:

1. HelloWorld.xaml
2. HelloWorld.js
3. HelloWorld.html

When I tried to open in my browser, nothing was displayed. ;)

After a WinMerge comparision of HelloWorld.html with Default.html, found the issue that
<script …/>

So, use <script></script>

Yes, <script/> gives lot of trouble in normal HTMl also.

Okay, let me continue the journey.

Tags Tags:
Categories: Uncategorized
Posted By: udooz
Last Edit: 26 Oct 2008 @ 03 39 AM

EmailPermalinkComments (0)
\/ More Options ...
Change Theme...
  • Users » 1
  • Posts/Pages » 54
  • Comments » 39
Change Theme...
  • VoidVoid « Default
  • LifeLife
  • EarthEarth
  • WindWind
  • WaterWater
  • FireFire
  • LightLight