February 2010 - Posts
Rx - for beginners (part 5): marble diagrams, select and where
this post is the 5th in a series of posts about the new Reactive Framework (Rx).
the series TOC can found here.

this post will focus on marble diagrams which is used for visualizing IObservable stream.
on this post we will visualize the select and where clause, while in the
upcoming posts we will discuss other operations that can be
used to upon IObservable streams.
the code sample for this post is available here.
What should we visualize?
the event stream should visualize the following:
- on next
- on error
- on complete

How should we read the diagram?
marble diagram can have one or more horizontal line.
- each horizontal line present the timeline of single IObservable stream.
- the ellipse present single occurrence of new value event (on next).
- the vertical line present the end of the stream sequence (on complete).
- the X sign present exception (on error).
The hidden assumption used by the Rx framework is that the IObservable stream
should stop either on complete or on error.
How can we draw select clause using the marble diagram?
Code Snippet
- // Create IObservable that has increasing long value each second
- var timeStream = Observable.Interval(TimeSpan.FromSeconds(1));
-
- // invert the timeStream stream using Linq statement
- var negativeTimeStream = from value in timeStream
- select -value;
-
- negativeTimeStream.Subscribe(value => Console.WriteLine(value));
line 2, creating IObservable stream that increment the projected value each second.
line 5, is using Linq statement to convert the stream into negative value.
line 8, subscribe to the stream and write it to the console.
the result is shown in the following image.
the marble diagrams for the above snippet will look like the following diagram:
the marble diagrams show the construction of new IObservable stream using the select operation.
the vertical arrow present the transition from the source stream into the destination stream.
as the case with IEnumerable the select statement is extension method which
can be used on any IObservable stream, we will get same result for the following code:
Code Snippet
- var negativeTimeStream = timeStream.Select(value => -value);
Where clause marble diagram?
the where clause of the following snippet:
Code Snippet
- // taking only even values
- //var evenTimeStream = timeStream.Where(value => value % 2 == 0);
- var evenTimeStream = from value in timeStream
- where value % 2 == 0
- select value;
will be present:
Summary
marble diagrams is how stream manipulation can be visualize.
we already surveyed the select and where operations, and we will continue
with other operation in the upcoming posts.
Code Samples
the code sample for this post is available here.
תגים של Technorati:
Rx,
IObservable,
IObserver
Rx - for beginners (part 4): anonymous observer handler
this post is the 4th in a series of posts about the new Reactive Framework (Rx).
the series TOC can found here.
this post will focus on anonymous observer handler.
the code sample for this post can be download from here.
Anonymous Observer
as we mention on the previous posts, IObserver is used as a callback interface
which can be subscribe to the IObservable,
but this is a bit of overkill, when what we subscribe is relatively small (like Console.Write).
wouldn't it be nice to use Lamda expression instead?
doesn't we want to have something similar to button1.Click += (s,e)=>Console.WriteLine(…); .
IObservable extensions
using Lamda expression syntax in order to subscribe delegate into the IObservable is
available because IObservable got the following extension options for its Subscribe method
(under the hood we are speaking about extension methods).
- Action<TSource> onNext
- Action<TSource> onNext, Action onCompleted
- Action<TSource> onNext, Action<Exception> onError
- Action<TSource> onNext, Action<Exception> onError, Action onCompleted
so assuming we got IObservable instance called foo, the following code is perfectly OK:
- IDisposable unsubscribe = foo.Subscribe(value => Console.WriteLine(value));
for simple subscription only for the on next callback.
subscribing both for on next and on error will look as follow:
- IDisposable unsubscribe = foo.Subscribe(
- value => Console.WriteLine(value),
- exc => Console.WriteLine(exc));
what is actually happens is that under the hood the subscribe method creating
instance of IObserver, attaching the delegate, and hand it to the IObservable subscribe method.
Code Sample
I took the previous post's sample, which look as follow:
Code Snippet
- class Program
- {
- static void Main(string[] args)
- {
- var obs = new Observer();
- var observable = new FakeObservableFeeder();
- IDisposable unsubscribe = observable.Subscribe(obs);
- Console.ReadKey();
- unsubscribe.Dispose();
- }
- private class Observer : IObserver<int>
- {
- void IObserver<int>.OnCompleted()
- {
- Console.WriteLine("Done");
- }
- void IObserver<int>.OnError(Exception exception)
- {
- Console.WriteLine("Error: " + exception.Message);
- }
- void IObserver<int>.OnNext(int value)
- {
- Console.WriteLine(value);
- }
- }
- }
you can see at line 11, that we implementing IObserver with code one line
for each methods (Console.WriteLine ).
and at line 7, we subscribing this observer into the IObservable instance.
Code Sample using anonymous observer
this can be match simpler using anonymous observer, as shown in the following snippet:
Code Snippet
- class Program
- {
- static void Main(string[] args)
- {
- var observable = new FakeObservableFeeder();
- // subscribing handlers (Anonymous observer)
- IDisposable unsubscribe = observable.Subscribe(
- value => Console.WriteLine(value),
- exc => Console.WriteLine(exc),
- () => Console.WriteLine("Done"));
- Console.ReadKey();
- unsubscribe.Dispose();
- }
- }
definitely less code was written.
you can see the subscription at line 7.
- line 8, is handling the on next action.
- line 9, is handling the on error action.
- line 10, is handling the on complete action.
Summary
what we have seen is that simple action can be subscribed using anonymous observer handlers.
תגים של Technorati:
IObservable,
IObserver,
Rx
MEF Preview 9 was released
MEF preview 9 changes will be reflected the in the release of .NET 4.0 and Silverlight 4.0.
except from bug fixing, there was some changes to the API,
most of the changes is related to System.ComponentModel.Composition.Initilization.dll,
which is not yet available only for none Silverlight application :-(
here is a short list of API changes:
-
PackageCatalog were brought back, and changed it name to DeploymentCatalog.
-
PartCreator was renamed to ExportFactory.
-
PartInitializer was renamed to CompositionInitializer
-
CompositionHost.InitializeContainer was renamed to CompositionHost.Initialize
i do hope that the System.ComponentModel.Composition.Initilization.dll will finally find it place
into the CLR 4.0 release.
you can download it from the MEF codeplex.
the information was taken from Wes' Puzzling Blog.
UPDATES:
From: Glenn block
Unfortunately we will not be shipping CompositionInitializer / Host as part of .NET 4.0. .NET 4.0 and SL are on different ship cycles and the desktop locked long before we had the functionality in SL. We will do an OOB release on codeplex for desktop. I currently have an early version on my SkyDrive, but we will get bits updated for codeplex. The plan is to role CI into the desktop in the future.
Rx - for beginners (part 3): IObservable Vs. IEnumerable
this post is the 3rd in a series of posts about the new Reactive Framework (Rx).
the series TOC can found here.
this post will focus on how exactly the IObservable/IObserver mirror IEnumerable/IEnumerator.
IEnumerator operations
IEnumerable expose the following operations:
Code Snippet
- public interface IEnumerator<T> : IDisposable, IEnumerator
- {
- T Current { get; }
- }
-
- public interface
- {
- object Current { get; }
- bool MoveNext();
- void Reset();
- }
- Move next: indicate whether the operation completed
(no more item available on the item source).
and it may also throw Exception if something have got wrong. - Current: is handing the current item.
- Reset: is restarting the iteration.
IObserver mirrored operations
IObserver is mirroring IEnumerator, instead of asking the IEnumerator,
IObserver will tell you what's is happening.
this is known as "don't call us we will call you" :-)
Code Snippet
- public interface IObserver<T>
- {
- void OnCompleted();
- void OnError(Exception exception);
- void OnNext(T value);
- }
the IObserver is actually a callback interface which will be called by the items feeder,
whenever something occurs.
- On complete: is mirroring move next, by notifying that the feeding source
has no more items. - On error: is also mirroring move next, but this time it mirror the move next throw exception behavior.
- On next: is mirroring the current property, by pushing new value into
the the observer OnNext implementation. - because of the push nature of the IObserver the reset does not mirrored.
IEnumerable Operations
Code Snippet
- public interface IEnumerable<T> : IEnumerable
- {
- IEnumerator<T> GetEnumerator();
- }
IEnumerable has one method that return IEnumerator with can be use
to iterate through the items by pulling the items sequentially.
IObservable mirrored operations
Code Snippet
- public interface IObservable<T>
- {
- IDisposable Subscribe(IObserver<T> observer);
- }
- through IObservable we can subscribe to content feeders by handing
implementation IObserver, then the feeder can use the IObserver interface
for each of the operation we discussed earlier. - the subscribe operation return IDisposable instance that is used
for unsubscribe the observer from the feeder.
this is match safer then using unsubscribe method because it doesn't has side effects,
just think of what's happens when you subscribe anonymous delegate.
in fact the disposable instance is mirroring IEnumerator<T> which is inheriting from IDisposable.
Summary
by mirroring IEnumerable/IEnumerator, IObservable/IObserver can change the data
flow direction from pull to push.
Code Sample
you can find code sample that implement simple scenario of feeder
that feed increasing integer sequence as long as the integer value is below 100.
the solution contain 2 projects, one implement the scenario using enumerable and the
other is using observable.
the code sample can be download from here.
תגים של Technorati:
IObservable,
IObserver,
Rx
Rx - for beginners TOC
Also available
תגים של Technorati:
Iobservable,
Iobserver,
Rx
Rx – Code cartoon
the following cartoon emphasis the Rx – IObservable push nature against the
pull nature of IEnumerable.
Summary
Reactive approach can reduce your phone bill :-)
תגים של Technorati:
IObservable,
IObserver,
Rx
Rx - Reactive Extension (part 2)
this post is the second in a series of posts about the new Reactive Framework (Rx).
the previous post discuss the concept of Rx (Reactive Framework Extension),
this post will focus on some basic practice.

In this post we will create small WPF application that got images from different
image feeds providers (the source code can be found here).
Application layout
The application layout is described in the following diagram.
the image data will be pushed into the PhotoDataSourceProvider class which implement IObserver.
Code Snippet
- public class PhotoDataSourceProvider : DataSourceProvider, IObserver<ImageData>
- {
- ...
- }
the class will take the pushed data and expose it to the UI through the DataSourceProvider overrides.
DataSourceProvider is used as mediator between the UI and the Feeders in order to make the UI data thread safe.
In short the IObserver is where the data get pushed.
The feeders implement IObservable
Code Snippet
- public abstract class ImageFeederBase: IObservable<ImageData>
- {
- public IDisposable Subscribe(IObserver<ImageData> observer)
- {
- ..
- }
- }
all feeders inherit from ImageFeederBase.
IObservable expose the Subscribe method, which can be use for subscribing the
PhotoDataSourceProvider (because it is an observer).
this is how the PhotoDataSourceProvider constructor look like:
Code Snippet
- public PhotoDataSourceProvider()
- {
- var picasaFeeder = new PicasaImageFeeder();
- picasaFeeder.Subscribe(this);
- var flickrAircraftFeeder = new FlickrImageFeeder("Aircraft");
- flickrAircraftFeeder.Subscribe(this);
- var flickrFlowerFeeder = new FlickrImageFeeder("Flower");
- flickrFlowerFeeder.Subscribe(this);
- var flickrSmileyFeeder = new FlickrImageFeeder("Smiley");
- flickrSmileyFeeder.Subscribe(this);
- var fileSystemJpgFeeder = new FileSystemImageFeeder("*.jpg");
- fileSystemJpgFeeder.Subscribe(this);
- var fileSystemPngFeeder = new FileSystemImageFeeder("*.png");
- fileSystemPngFeeder.Subscribe(this);
- }
it subscribing itself for different feeders.
then whenever the feeder Has new image, Complete or Has Error
the PhotoDataSourceProvider will be notify through it IObserver implementation.
Code Snippet
- void IObserver<ImageData>.OnCompleted()
- {
- ...
- }
- void IObserver<ImageData>.OnError(Exception exception)
- {
- ...
- }
- void IObserver<ImageData>.OnNext(ImageData value)
- {
- ...
- }
Summary
the Rx is using the IObservable and IObserver in order to formalize push based operation.
the IObservable is the push supplier, while the IObserver is the push target.
IObserver can be subscribe to one or more IObservable.
Source Code
the source code can be found here.
תגים של Technorati:
IObserver,
IObservable,
Rx
Rx - Reactive Extension (part 1)
this post is the first in a series of posts about the new Reactive Framework (Rx).
What is the Reactive Framework Extension?
the Rx formalize the push based programming pattern.
it is dealing with event like collection, in manner that can be describe as the mirroring of IEnumerable<T>.
How does it mirror the IEnumerable<T>?
the Rx framework most fundamental interface are,
IObservable<T> which is mirroring IEnumerable<T>.
Code Snippet
- // Summary:
- // Represents a push-style collection.
- public interface IObservable<T>
- {
- // Summary:
- // Subscribes an observer to the observable sequence.
- IDisposable Subscribe(IObserver<T> observer);
- }
and IObserver<T> which is the mirroring IEnumerator<T>
Code Snippet
- // Summary:
- // Supports push-style iteration over an observable sequence.
- public interface IObserver<T>
- {
- // Summary:
- // Notifies the observer of the end of the sequence.
- void OnCompleted();
- //
- // Summary:
- // Notifies the observer that an exception has occurred.
- void OnError(Exception exception);
- //
- // Summary:
- // Notifies the observer of a new value in the sequence.
- void OnNext(T value);
- }
actually we getting formalized pattern for push collection (collection that may be endless)
which can push data into our IObserver<T> instance.
on of the benefit of this formalization is that we can use Linq query upon that push collection.
we will discuss IObservable<T> and IObserver<T> in more details on the following post of this series.
Interactive Vs. Reactive (pull Vs. Push)
we will discuss Interactive (IEnumerable<T>) Vs. Reactive (IObservable<T>) paradigms
by using journal reading analogy.
let say that you do like to read several journals at your home each day.
before the reading part the journals should reach your home.
you do have 2 options (Interactive or Reactive approach):
- Interactive: going each day to the nearby store (or several stores in
case that the nearby store does not have all of your favorite journals), collect the journals,
wait on the payment queue, pay for it and return home for reading.
- Reactive: subscribe to journal delivery service which will send the journals to
your door front each day.
Summary
the Reactive model is more native for parallel computing
and it can save the collecting time:
- go to the store
- wait on the payment queue (in the software world waiting for blocking threads)
in the next pasts we will discuss more benefit, scenarios and techniques.
How to get started?
in order of start using the Rx you have to download the library from here (it is available both for framework 3.5 and 4.0).
then you have to add reference to System.Reactive
Catalogs (code cartoon)
catalog are search boundary for the composition container.
you can read more about MEF on MEF for beginner series, the TOC is available here
MEF for Beginner (Catalogs) - part 10
this is the 10th post of the MEF for Beginner series, the series TOC is available here.
this post will focus on Catalogs.
What is MEF Catalogs?
MEF container is using catalogs as its search area definition.
the catalog have instructions about where can MEF look for
the compose-able parts (import and export definitions).
in short catalogs are actually a discovery instruction.
Out of the box catalogs
MEF is shipping with the following out of the box catalogs:
Code Snippet
- var typeCat = new TypeCatalog(typeof(IFoo));
- var asmCat = new AssemblyCatalog(typeof(Program).Assembly);
- var dirCat = new DirectoryCatalog("US", "*.dll");
- var aggCat = new AggregateCatalog(typeCat, asmCat, dirCat);
- var container = new CompositionContainer(aggCat);
- container.Compose(...);
- TypeCatalog (line 1) define that specific types should includes in the discovery process.
- DirectoryCatalog (line 2) instruct the discovery process to search for compose-able pars
within specific assembly. - DirectoryCatalog (line 3) is the most common used catalog, which instruct MEF to
search for compose-able parts within assemblies under specific directory using search pattern ("*.dll"). - AggregateCatalog (line 4) is aggregation of different catalogs instances.
this catalog enable to add and remove catalogs at runtime.
UPDATE
Silverlight (CTP 9) introduce new catalog named DeploymentCatalog which should replace the PackageCatalog.
my next post will discuss more on this topic (soon).
Silverlight is adding the PackageCatalog which is searching within assemblies which include
in the package.
the following snippet demonstrate the Silverlight PackageCatalog:
Code Snippet
- var catalogs = new PackageCatalog();
- catalogs.AddPackage(Package.Current);
- var container = new CompositionContainer(catalogs);
- container.Compose(...);
as you can see the PackageCatalog can include multiple packages.
Summary
The catalogs use to define the search boundary of the composition container discovery process.
different catalogs can be use for different boundary ranges.
Samples
each of the previous parts of this series contains samples which is using catalogs
you can explore the previous parts from the TOC.
does MEF apply the SOA paradigm?
in this post I'm going to argue that the Manage Extensibility Framework (MEF),
is actually applying to the 4 tenet of Service Orientation Architecture (SOA),
therefore MEF is actually, in-process implementation of the SOA paradigm.
Background
when we speaking about SOA we used to think about technologies like web services and WCF,
which is cross process technologies.
but does SOA define that services should always be consumed from the clouds?
local services like loggers, rule engine, cache and more are everyday practice for in-process services.
What make your architecture, service oriented?
different architect having different opinions of how to construct SOA architecture,
but most of them agree that SOA design should apply to the 4 tenets of SOA.
The 4 tenets of SOA
Service should have Explicit Boundaries.
Service should be Autonomous.
Service API should rely on Contract.
Service behavior and compatibility should rely on Policy.
How MEF apply to the 4 tenets?
MEF boundaries define explicitly by the Export definition.
separating exports into separate dlls will create autonomous unit which has no need
for any reference but the reference for the services contract.
Imports does not aware of the actual implementation, imports are solely relay on the contract
(composition will find export that mach the import contract).
MEF has different policies mechanism like metadata, creation policy,
instantiation policy, recomposition.
Summary
despite MEF being an in-process technology it does apply to the 4 tenet of SOA,
therefore it can consider as such.
תגים של Technorati:
MEF,
extensibility,
SOA,
CLR 4
CLR 4.0 tip (Strong Name)
CLR 4.0 come with some changes of the security API.
one useful new API is a new way of getting evidence without iteration loop.
and this is how it look:
Code Snippet
- StrongName sn = typeof(Program).Assembly.Evidence.GetHostEvidence<StrongName>();
CLR 4.0 transparent security
CLR 4.0 come with some changes to the security model.
in general the CLR 4.0 no longer assume the sandbox for the application.
you can still sandboxed your AppDomain and the CLR 4.0 will onerous that sandbox,
but running the same exe from local computer or from network shared folder
won't grant the exe different privileges (sandbox).
as result the security setting goes back into the administrators hands,
as it was before the .NET era and application restriction will be define equally
for both manage and unmanaged code.
the security is back again done at the OS level.
what you should be aware of the the transparent code concept,
and that security is no longer can be define at the assembly level (all the definition should done at the AppDomain level).
Transparent code concept
the analysis of .NET fundamental operation observe that there are only 2 operation which are dangerous:
- unverifiable code
- calling unmanaged code (P-Invoke / COM)
potentially harm operations define as security critical, and cannot be invoke directly from security non critical (transparent) code.
if non security critical (transparent) code need access for security critical operation,
it should do so by calling security safe critical operation (which play the role of the man in the middle).
this exactly what happens when you need to persist data into file
using in-browser Silverlight application,
the file open define as security critical so you have no direct access to this operation,
and the isolated storage defined as security safe critical, so it give you indirect
access to the security critical file open but only to restricted file paths.
Code snippet
you can get the code snippet from here.
Code Snippet
- [assembly: AllowPartiallyTrustedCallers]
- [assembly: SecurityRules(SecurityRuleSet.Level2)]
- namespace TransparentSecurity
- {
- class Program
- {
- static void Main(string[] args)
- {
- var criticalFoo = new CriticalFoo();
- try
- {
- criticalFoo.DoDangerousOperation(AppDomain.CurrentDomain.BaseDirectory);
- }
- catch (Exception ex) { Console.WriteLine(ex.Message); }
-
- try
- {
- criticalFoo.DoSafeOperation(AppDomain.CurrentDomain.BaseDirectory);
- }
- catch (Exception ex) { Console.WriteLine(ex.Message); }
- }
- }
-
- public class CriticalFoo
- {
- [SecuritySafeCritical]
- public void DoSafeOperation (string path)
- {
- var prm = new FileIOPermission(FileIOPermissionAccess.AllAccess, path);
- prm.Demand();
- DoDangerousOperation(path);
- }
-
- [SecurityCritical]
- public void DoDangerousOperation (string path)
- {
- Console.WriteLine(path);
- }
- }
- }
line 1: by default your code consider security critical unless you decorate it with [AllowPartiallyTrustedCallers] attribute.
line 2: setting the security rules to level 2, is the default CLR 4.0 security model (level 1 is CLR 2 backward compatibility).
line 34: decorate the method as security critical (therefore it can be assessed only by security critical or security safe critical operations).
line 12: this invocation won't succeed because security non critical (transparent) operations cannot access security critical operations.
line 26: decorate the method as security safe critical (so this code can target security critical operation and be target by
security non critical code).
lines 29,30: restrict the access using demand for file permission to the specific folder.
Summary
CLR 4.0 security model is taking one step back from absolute security management,
and one step to the to the side and forward by giving us more simple and understandable security model.
the transparent model dose not restrict malicious code from accessing critical operation,
(because the malicious code can be decorate with security safe critical), what it does is helping the
CLR to put it's security focus on the dangerous code section.
Point of interest
you can learn more about the CLR 4.0 security concepts on this video by Shawn Farkas.
תגים של Technorati:
security,
clr 4,
.nrt