DCSIMG
Bnaya Eshet

Bnaya Eshet

Disclaimer

The 3rd day of the SDP

The 3rd day of the SDP was over and I want to thanks all the attendants.
my yesterday session was about C# 5 async and await.

today I will have a full day tutorial on Rx and TPL Dataflow.
you can download the demo code and also the presentation for second and third days from here (the link is also having the demo code for today's sessions).

sdp, clr, cryptography , rx, tpl, dataflow, async, await

anyway, I also want to recommend 2of yesterday session.

if you were attend at the conference you should have an access to the sessions video page.
first I want to recommend Ofir Makmal's session about .NET 4.5 CLR improvements.

Ofir was talking about subjects like:

  • Multi-core JIT
  • Managed Profile Guided Optimization
  • Auto-Ngen
  • Background Server GC

my favorite subject was Multi-core JIT.

the following is a snippet for Multi-core JITing.

Code Snippet
  1. static void Main(string[] args)
  2. {
  3.     ProfileOptimization.SetProfileRoot(@"C:\MyApplicationFolder");
  4.     ProfileOptimization.StartProfile("startup.profile");
  5. }

my second recommendation is Manu Cohen-Yashar's session about .NET cryptography.
Manu's talk is great for anyone who want to use the .NET cryptography library.

Async / Await for .NET 4, Silverlight and Windows Phone

Async / Await for .NET 4, Silverlight and Windows Phone

if you have to target .NET 4, Silverlight and Windows Phone and
still want to use the async / await pattern.

the BCL team provide you with a new NuGet package named Microsoft.Bcl.Async.

Parallel.Async, await, Task, TPL, BCL

this package was announced as stable a few week ago.
so you can check it out if you're having VS 2012 but should target one of the above platforms.

be aware that this package won't work with 2010.

EF should adopt the WAQS framework

EF should adopt the WAQS framework

I was speaking with Matthieu MEZIL about his WAQS framework,
it is a layer of abstraction over the Entity Framework which I think you should check out.
Matthieu is working on it on his free time (currently as an open source) and it is amazing to see what he was able to achieve.
this framework is already been in used by some of real word company and it is improving over time (today Matthieu is working on a new version of it).

WAQS, EF, Database, ORM

as I see it, Microsoft EF team should adopt this framework as part of the EF open source project. I hope that the EF team will put the effort to merge it into their source code.

WAQS is addressing many of the painful scenarios under EF, with a great design and  productivity.

as I understand Matthieu is willing to let the EF team to take ownership over his labor and I believe that it is an opportunity that will be pity to waste (everything within this framework came out of Matthieu's field experience in order to ease his customer pain).

this post is a call to action, if you like this framework or the ideas it represent vote for it at:

http://data.uservoice.com/forums/72025-entity-framework-feature-suggestions/suggestions/3929780-waqs

SDP (day 2)

the second day of the SDP was over and I want to thanks all the attendants of my sessions.

SDP

at day 1 I was giving a full day on TPL, I was covering lot of API, tools and techniques.

the demo codes for this session can be found at http://sdrv.ms/1283vDW

the second day session was about .net 4.5 / VS 2012 new features, tolling and overloads for parallel programming.

the code sample for this session can be found at http://sdrv.ms/18NeNje

tomorrow I will talk about the new C# 5 parallel syntax (async / await) and
on Wednesday I will provide a full day on Rx (reactive Extension) and TDF (TPL Dataflow).

I general we are having a successful conference with a local and foreign speakers.

Posted: May 06 2013, 11:53 AM by bnaya
תגים:, ,

EF 6: Async

EF 6: Async

this post is the first in a series about what's new in EF 6.

great improvements are about to come with Entity Framework 6.
it is a major release and the first one since EF become an open source.
each post in the series will be dedicate to a single feature.

this post will focus on a new EF a-sync features.

EF, Parallel, async, await, task, api, entity

the first question that should be asked is, why do we need parallel data access?
moreover why do we need a dedicate parallel data access API, rather then using the TPL Task.Run(…) in order to introduce parallelism?

the answer is related both to thread safety and ThreadPool optimization.

a dedicate data access API for parallel programming, ensured a thread safe access, which may not be a trivial task.

furthermore, when the database is executing a query the .NET thread is idle (waiting for the execution result).
if this idle thread has taken from ThreadPool (either directly or through high level API like TPL Task.Run(…)) it could lead to a ThreadPool starvation.

assuming that you have a 2 second query, Task.Run(…) will hold the ThreadPool's thread for 2 second even though it idle while the database is executing the query.

what you really want is an API that operate over IOCP (IO completion port). IOCP API will release the ThreadPool's thread back to the pool and re-claim a thread at the completion time.

at the API level you can find a-sync operations everywhere.
I will present some of the main API.

the following snippets, will use the following entities (code first):

Code Snippet
  1. public class Author
  2. {
  3.     public Author()
  4.     {
  5.         Blogs = new List<Blog>();
  6.     }
  7.     public int Id { get; set; }
  8.     public string Name { get; set; }
  9.     public IList<Blog> Blogs { get; set; }
  10. }
  11.  
  12. public class Blog
  13. {
  14.     public int Id { get; set; }
  15.     public string Title { get; set; }
  16.     public string Content { get; set; }
  17.     public int AuthorId { get; set; }
  18.  
  19. }

and the following context:

Code Snippet
  1. public class Context : DbContext
  2. {
  3.     public DbSet<Author> Authors { get; set; }
  4.     public DbSet<Blog> Blogs { get; set; }
  5. }
a-sync save
Code Snippet
  1. using (var context = new Context())
  2. {
  3.     context.Authors.Add(author);
  4.     int affected = await context.SaveChangesAsync();
  5. }

notice the await at line 4.

a-sync read
Code Snippet
  1. using (var context = new Context())
  2. {
  3.     Author[] authors = await context.Authors
  4.         .Include(a => a.Blogs)
  5.         .ToArrayAsync();
  6.     return authors;
  7. }

line 5, is where the magic happens.

execute command
Code Snippet
  1. using (var context = new Context())
  2. {
  3.     int affected = await
  4.         context.Database.ExecuteSqlCommandAsync("DELETE FROM BLOGS");
  5. }
summary

EF 6 is having a great parallel API, which respect IOCP and compatible with the new async / await syntax.

 

EF, Parallel, async, await, task, api, entity,sdp

Shout it

Parallel and The C# Memory Model

Parallel and The C# Memory Model

Parallel programming can be tricky, both compiler and CPU's optimization can lead into a twilight zone's debugging.

lets take the following code snippet snippet:

Code Snippet
  1. class Program
  2. {
  3.     static void Main(string[] args)
  4.     {
  5.         Console.WriteLine("Start");
  6.         var u = new Util();
  7.         u.Exec();
  8.         Console.ReadKey();
  9.     }
  10. }
  11.  
  12. public class Util
  13. {
  14.     private bool _stop = true;
  15.     public void Exec()
  16.     {
  17.         Task t = Task.Run(() =>
  18.             {
  19.                 bool b = true;
  20.                 while (_stop)
  21.                 {
  22.                     b = !b;
  23.                 }
  24.                 Console.WriteLine("Complete {0}", b);
  25.             });
  26.         Thread.Sleep(30);
  27.         _stop = false;
  28.     }
  29. }

can you predict the outcome?
will it ever reach the completion at line 24?

you can download the snippet from here.

now download the snippet and try to execute it in the following order:

  • Compiled in Debug mode and double click on the exe file (without an attached debugger).
  • Compiled in Release mode and double click on the exe file (without an attached debugger).
  • Compiled in Release mode and run it F5 (with an attached debugger).

Parallel, Task, Memory model, MemoryBarrier, rase, optimization, reordering

so why does is execute predictably in Debug mode, while behaving quit strange in Release mode?
even stranger why does it execute predictably in Release mode when a debugger attached?

what happening is a matter of a single thread optimization that can occurs at the compiler (JIT) or CPU level which is taking assumption which is not acceptable for parallel execution.

it can be fixed up using different API like Thread.MemoryBarrier and other.

instead of explaining this out, I will offer you a reading of part 1 and part 2 of a great article on those matters.

Summary

the optimization world is still rely on a single thread assumption (with some special instruction for parallel execution).
while the computing world becoming more parallel each day, this single thread assumption priority may have to be change in the future.

in order to avoid pitfalls, you should be aware o it and use a parallel dedicated APIs.


Shout it

Immutable Collections

Immutable Collections

Immutability is a pattern which is suit well parallel programming,
but you have to be aware of a potential memory pressure risk when it's not implemented right or used wisely.

this post will cover a new BCL library (still in its preview stage) which is targeting immutable collections.

Immutable, Parallel, Task, BCL, collection, Thread-safe

.NET is already having Concurrent implementation for Queue, Stack, Bug and Dictionary, which is thread-safe, but other type of collection like List is missing.
another type of collection are read only collection, but those type of collection are not truly thread-safe because it is just a wrapper upon other collection which can be mutate by it's owner.
so iterating on a read only collection doesn't guarantee thread-safety because the collection can be mutate during the iteration by it's original owner.
currently we do have one option to create an immutable collection by using the ToArray() extension method, but as I mentioned before it can lead to a memory pressure while using a large collection.

the BCL team was targeting this issue and are working on a new kind of collections which are immutable.
those collection is having a cool concept which guarantee that an immutable sequence will not changed, but it can be part of another immutable sequence. this mean that you can take an immutable collection and add an item. this will keep the immutable part as is and you will get a new collection that include your addition and the original collection, but if someone is using the original collection he won't be affected by your mutation.
as you can see the BCL immutable collection can be mutate but the mutation will return a new collection and leave the original collection untouched.

Start using the immutable collection

you can start using it though NuGet.
on the current preview version it includes:

  • ImmutableStack<T>
  • ImmutableQueue<T>
  • ImmutableList<T>
  • ImmutableHashSet<T>
  • ImmutableSortedSet<T>
  • ImmutableDictionary<K, V>
  • ImmutableSortedDictionary<K, V>
Fluent API

I will speak about 3 APIs:

  • Add
  • AddRange
  • Builder
Add
Code Snippet
  1. [Test]
  2. public void List_AddItem_Test()
  3. {
  4.     var col = ImmutableList<int>.Empty;
  5.     var col1 = col.Add(1);
  6.     var col2 = col1.Add(2);
  7.  
  8.     Assert.AreEqual(0, col.Count);
  9.     Assert.AreEqual(1, col1.Count);
  10.     Assert.AreEqual(2, col2.Count);
  11. }

whenever you want an empty instance of immutable collection you should use the static Empty property (there is no public constructor available).

you should always remember that Adding an item will not affect the original collection therefore you must handle the return value (is is the same concept as string.Replace).

finally you can notice at lines 8-10 that the above code was actually construct 3 different immutable collections.

AddRange

both from performance and usability it is better using the AddRange API when you intend to add multiple items.

Code Snippet
  1. [Test]
  2. public void List_AddRange_Test()
  3. {
  4.     var col = ImmutableList<int>.Empty;
  5.     var col1 = col.AddRange(Enumerable.Range(0, 1000));
  6.  
  7.     Assert.AreEqual(0, col.Count);
  8.     Assert.AreEqual(1000, col1.Count);
  9. }
Builder

if you want to efficiently add multiple items and AddRange doesn't do the job for you, you can use a Builder (the concept is quit similar to StringBuilder).
you should remember that unlike the immutable collection a builder is not a thread-safe structure, therefore you shouldn't share its state between threads.
when you complete the mutation, you can extract an immutable collection from the builder.

Code Snippet
  1. [Test]
  2. public void List_Builder_Test()
  3. {
  4.     var col = Enumerable.Range(0, 1000).ToImmutableList();
  5.     var builder = col.ToBuilder();
  6.     for (int i = 0; i < 100; i++)
  7.     {
  8.         builder.Remove(i * 10);
  9.     }
  10.     var col1 = builder.ToImmutable();
  11.     Assert.AreEqual(1000, col.Count);
  12.     Assert.AreEqual(900, col1.Count);
  13. }

line 5: getting a builder from immutable collection.
line 10: getting an immutable back from the builder (after the mutation).

Migration

if you are using a read only collection though the IReadOnlyCollection, IReadOnlyList or IReadOnlyDictionary, migration will be easy because the immutable collections are implementing those interfaces.

Optimization

we already spoke about the memory pressure optimization, but what is the characterize of those collection in compare with the well known BCL mutable collection?
as the BCL team argue that immutable collections have been heavily tuned for maximum performance and minimum GC pressure.

the following table was published by the BCL team.
it compare the performance behavior of the BCL immutable and immutable collection (and remember that this is only a preview version which mean that further optimization can be applied).

Immutable, Parallel, Task, BCL, collection, Thread-safe

you can read more on this at this post.

summary

Immutability is a great pattern for parallelism.
the BCL team are giving us a robust and well design immutable collection.
the potential memory pressure has reduced by the internal data-structure design.
so it is definitely something to wait for its release.

you can read more about it at here.
and if you want more background and comparison to the read only collection, you can check out this post.


Shout it

MEF 2.0 - mini series: part 8 (Composition options and exception handling)

MEF 2.0 - mini series: part 8 (Composition options and exception handling)

this is the last post in the MEF 2.0 mini series.
you can see other posts of this series in here.

this post will wrap-up the series with a quick survey to to the to some changes made for the underline composition process.

MEF, SELA, Export, Import, CompositionContainer, RegistrationBuilder, Catalog

Exception

one of the most painful experience of MEF 1 was its misleading exception's description. some time it was really hard to figure out the exception roots.  you can read more about MEF 1 issues in here.

MEF 2 was doing a much better job, but still some composition failure are misleading.
my recommendation is to use the IPartImportsSatisfiedNotification which can give you an interception point where you can figure out what went wrong, before MEF is throwing a composition exception.

the pattern that I'm using is to allow default on any imports and validate it on the OnImportsSatisfied method, see the next snippet:

Code Snippet
  1. public class Foo : IPartImportsSatisfiedNotification
  2. {
  3.     [Import(AllowDefault=true)]
  4.     public IIdentity Identity { get; private set; }
  5.  
  6.     [ImportMany]
  7.     public IReflect[] Reflects { get; private set; }
  8.  
  9.     public void OnImportsSatisfied()
  10.     {
  11.         if (Identity == null)
  12.         {
  13.             Trace.WriteLine("Composition failure (Identity)");
  14.             throw new ArgumentNullException("Identity");
  15.         }
  16.  
  17.         if (Reflects == null || Reflects.Length == 0)
  18.         {
  19.             Trace.WriteLine("Composition failure (Reflects)");
  20.             throw new ArgumentNullException("Reflects");
  21.         }
  22.     }
  23. }
Composition options

MEF 2 is exposing some composition tuning options that wasn't available on MEF 1.
there was some hidden composition assumptions which you couldn't modified.
I will focus on the DisableSilentRejection options.
silent rejection was added into MEF composition in order to support robust composition of ill functioning plug-ins. the requirement for this feature came from the Visual Studio team which is using MEF for their plug-in model.
anyway you might want to alter this behavior. MEF 2 doe's let you the option to disable it.
the original behavior will exclude the ill functioning plug-in from the composition (for import many) without throwing an exception.
this led to scenarios when you had a real hard time to figure out why some of the plug-in wasn't loading and still everything seem to be working.

the following snippet is demonstrating this scenario:

Code Snippet
  1. class Program
  2. {
  3.     static void Main(string[] args)
  4.     {
  5.         var p = new Program();
  6.  
  7.         var cat = new AssemblyCatalog(Assembly.GetExecutingAssembly());
  8.         var container = new CompositionContainer(cat, CompositionOptions.DisableSilentRejection);
  9.  
  10.         try
  11.         {
  12.             container.ComposeParts(p);
  13.             Console.WriteLine("{0} Plug-ins loaded", p.Plugins.Length);
  14.         }
  15.         catch (CompositionException)
  16.         {
  17.             Console.WriteLine("Composition error");
  18.         }
  19.     }
  20.  
  21.     [ImportMany]
  22.     public Iplugin[] Plugins { get; set; }
  23. }
  24.  
  25. [InheritedExport]
  26. public interface Iplugin { }
  27.  
  28. public class BadPlugin : Iplugin
  29. {
  30.     [Import(typeof(IIdentity))]
  31.     public IIdentity MissingDependency { get; set; }
  32. }
  33.  
  34. public class FinePlugin : Iplugin { }

line 8: disabling the silent rejection behavior.
line 28 - 32: a class that is having a missing dependencies, therefore cannot be construct. on silent rejection mode (which is the default) it will be simply removed from the composition and the Plugins property at line 22 will get a single plug-in rather then 2.

Summary

MEF 2 is having improvements both in the engine and the API level.
you do have more control over the composition and better descriptive exception.


Shout it

The problem of animals and foods

The problem of animals and foods

this is a short post that is dealing with a classic riddle.

I was thinking on this riddle when I was trying to figure out a Scala feature. we are having (at Sela Group) a small Scala study group led by Israel Tabadi and while we were going over Scala's abstract type (which is by the way a cool implementation) I was thinking about the .NET equivalent solution.

I will use the problem of "animals and foods" (taken from here) as an anchor's point.

The problem: assuming an Animal with a method, Eat, which eats some food. and the riddle is, how can you restrict a Cow (which derived from an Animal), to eat nothing but Grass (at compile time). A Cow can't eat an arbitrary food like Fish.

the idea is to restrict a Cow to a specialize type of food which is Grass.

the Scala version is available here.

now take your time and think of how can you solve it using .NET and Generics?

Scala, Generics, Abstract, Type, animals and foods, .NET

the solution is to assign the Cow's type to Grass (as simple as it is, sometimes it can slip away).

the solution is present on the following snippet:

Code Snippet
  1. public abstract class Food { }
  2. public class Grass : Food { }
  3. public class Club : Grass { }
  4. public class DogFood : Food { }
  5.  
  6. public abstract class Animal<T>
  7.     where T : Food
  8. {
  9.     public void eat(T food)
  10.     {
  11.         Console.WriteLine("{0} eat {1}", this.GetType().Name, food.GetType().Name);
  12.     }
  13. }
  14.  
  15. public class Cow: Animal<Grass>
  16. {
  17. }
  18.  
  19. public class Dog : Animal<DogFood>
  20. {
  21. }

the following snippet is the main method part:

Code Snippet
  1. var dogFood = new DogFood();
  2. var grass = new Grass();
  3. var club = new Club();
  4.     
  5. var dog = new Dog();
  6. var cow = new Cow();
  7.     
  8. dog.eat(dogFood);
  9. //dog.eat(grass) // not compiled
  10. cow.eat(grass);
  11. cow.eat(club);    

and the result is:

Scala, Generics, Abstract, Type, animals and foods, .NET


Shout it

MEF 2.0 - mini series: Part 7 (Catalog filter and Deep hierarchic scoping)

MEF 2.0 - mini series: Part 7 (Catalog filter and Deep hierarchic scoping)

this is the 7th post in the MEF 2.0 mini series.
you can see the following TOC for other posts in this series.

in the previous post I was talking about composition scoping and lifetime management.
on this one, I will extend the composition scoping topic toward hierarchic along with catalog filtering capability.

MEF, extension, Composition, ImportMany, Export, Import, .NET 4.5, egistrationBuilder, Catalog

hierarchic scoping is not trivial, you must understand the hierarchic behavior and what it was design for.

MEF hierarchic was design to enable parts from higher scope to access lower scope's part without re-instantiation, lower part do not target higher scope's part directly.

each hierarchic scope construct by importing a ExportFactory<T> (which we saw in the previous post).

I will try to make it as simple as I can.

the following diagram demonstrate the idea of sub scope that interact with its parent scope's items.

MEF, extension, Composition, ImportMany, Export, Import, .NET 4.5, egistrationBuilder, Catalog

let try to have a similar code structures.

I will use a plug-in base class to trace the instantiations:

Code Snippet
  1. public abstract class PluginBase
  2. {
  3.     private static int _globalId = 0;
  4.     protected int _id;
  5.  
  6.     public PluginBase()
  7.     {
  8.         _id = _globalId++; // not thread-safe
  9.     }
  10. }

derived class's ids will be incremented per instantiation.

the following snippet include the classes which will be used for our hierarchic demonstration:

Code Snippet
  1. public class Level1ScopeRoot : PluginBase
  2. {
  3.     [Import]
  4.     public Level1ItemA ItemA { get; private set; }
  5.     [Import]
  6.     public Level1ItemB ItemB { get; private set; }
  7. }
  8.  
  9. public class Level1ItemShared : PluginBase { }
  10.  
  11. public class Level1ItemA : PluginBase
  12. {
  13.     [Import]
  14.     public Level1ItemShared ItemZ { get; private set; }
  15. }
  16.  
  17. public class Level1ItemB : PluginBase
  18. {
  19.     [Import]
  20.     public Level1ItemShared SharedItem { get; private set; }
  21.  
  22.     [Import]
  23.     public ExportFactory<Level2SubScopeRoot> SubScope { get; private set; }
  24. }
  25.  
  26. public class Level2SubScopeRoot : PluginBase
  27. {
  28.     [Import]
  29.     public Level2SubItem1 SubItem1 { get; private set; }
  30.  
  31.     [Import]
  32.     public Level2SubItem2 SubItem2 { get; private set; }
  33. }
  34.  
  35. public class Level2SubItem1 : PluginBase
  36. {
  37.     [Import]
  38.     public Level1ItemShared ItemShared { get; private set; }
  39.     [Import]
  40.     public Level2SubItemShared SubItemShared { get; private set; }
  41. }
  42.  
  43. public class Level2SubItem2 : PluginBase
  44. {
  45.     [Import]
  46.     public Level2SubItemShared SubItem { get; private set; }
  47. }
  48.  
  49. public class Level2SubItemShared : PluginBase { }

all the classes prefixed with the scoping 'Level'.

referring a sub-scope required using of ExportFactory<T> (line 23).

the following diagram is showing their hierarchic and dependencies graph:

MEF, extension, Composition, ImportMany, Export, Import, .NET 4.5, egistrationBuilder, Catalog

 

our final step is to define a catalog per scope's hierarchic and to set hierarchic structure.

Code Snippet
  1. var picker = new RegistrationBuilder();
  2. picker.ForTypesDerivedFrom<PluginBase>()
  3.     // add ScopeLevel metadata
  4.     .AddMetadata("ScopeLevel", t => t.Name.StartsWith("Level1") ? 1 : 2)
  5.     .Export<PluginBase>();
  6.  
  7. var catalog = new AssemblyCatalog(typeof(Program).Assembly, picker);
  8.  
  9. var catalogL0 = catalog.Filter(p => p.ContainsPartMetadata("ScopeLevel", 1));
  10. var catalogL1 = catalog.Filter(p => p.ContainsPartMetadata("ScopeLevel", 2));
  11.  
  12. var scopeL1 = new CompositionScopeDefinition(catalogL1, null);
  13. var scopeL0 = new CompositionScopeDefinition(catalogL0, new[] { scopeL1 });
  14.  
  15. var container = new CompositionContainer(scopeL0);

at line 2-5, I export everything inherits from 'PluginBase' and adding a 'ScopeLevel' metadata with the right scope leveling value.

line 7, define a general assembly catalog (which will be the base for the filtered catalogs).

lines 9,10, are defining a filtered catalog for each scoping level (filtering an existing assembly catalog).

lines 12,13, defines the hierarchic between the scoped catalogs.

line 15, create a container with the top level scope.

now we're ready for a composition.

here you can download a sample code.

Summary

I consider this feature as MEF 2's most complex one.

Hierarchic can become complex, so use it with caution.


Shout it

This Week On Channel 9

This Week On Channel 9

my blog was manage to get on Channel 9 week's top developer news.

ch9, MEF, bnaya, news, ,net 4.5

MEF 2.0 - mini series: part 6 (Composition scoping and lifetime management)

MEF 2.0 - mini series: part 6

(Composition scoping and lifetime management)

this is the 6th post in the MEF 2.0 mini series.
you can see the following TOC for other posts in this series.

in this post I will cover a new concept of scoping and part lifetime management, which is a great improvement over MEF 1.

DEV, MEF, SELA, Export, Import, CompositionContainer, RegistrationBuilder, Catalog

MEF 1 was coming with a fairly naïve lifetime management.
part's lifetime could be either shared or non-shared (you could also apply 'any' but eventually 'any' will be created as shared or non-shared).
shared is a singleton instantiation, while non-shared will create a new instance each time.

MEF 1's instantiation model doesn't support a complex scenario where some dependency's lifetime should be dictate by the lifetime of other unites.
you can conceder a UI window that is having plug-ins that should be dispose while the UI window is closing.

for example consider you have the following components (parts) dependency flow:

DEV, MEF, SELA, Export, Import, CompositionContainer, RegistrationBuilder, Catalog

the application can have multiple processes.
each process is having some plugins, but each view should have different instantiation of the plugins.
until now you can argue that a non-shared instantiation will do the job.
the next dependency level (DAL) should be shared under the boundary of a process but shouldn't be shared across processes
this one can neither handle by the share nor by non-shared instantiation.
single instance of DAL should be created under each view scope.

MEF 2 added a scoped lifetime management.
I will use the attribute based model for this sample, but it will work in the same way in the fluent model.

Code Snippet
  1. [Export]
  2. public class Application
  3. {
  4.     [Import]
  5.     private ExportFactory<ProcessA> ProcAFactory { get; set;}
  6.     [Import]
  7.     private ExportFactory<ProcessB> ProcBFactory { get; set;}
  8. }
  9.  
  10. [Export]
  11. public class ProcessA
  12. {
  13.     [Import]
  14.     public PluginA PlugA { get; private set; }
  15.     [Import]
  16.     public PluginB PlugB { get; private set; }
  17. }
  18.  
  19. [Export]
  20. public class ProcessB
  21. {
  22.     [Import]
  23.     public PluginA PlugA { get; private set; }
  24.     [Import]
  25.     public PluginB PlugB { get; private set; }
  26. }
  27.  
  28. [Export]
  29. public class PluginA
  30. {
  31.     [Import]
  32.     public DAL Dal { get; private set; }
  33. }
  34.  
  35. [Export]
  36. public class PluginB
  37. {
  38.     [Import]
  39.     public DAL Dal { get; private set; }
  40. }
  41.  
  42. [Export]
  43. public class DAL
  44. {
  45. }

at line 4-7 you can see a new type of importing target called ExportFactory<T> this type will initialize the scope.
It is having a CreateExport method that return a ExportLifetimeContext<T> can control the part life time.

the full implementation of the Application class is:

Code Snippet
  1.   [Export]
  2.   public class Application
  3.   {
  4.       [Import]
  5.       private ExportFactory<ProcessA> ProcAFactory { get; set;}
  6.       [Import]
  7.       private ExportFactory<ProcessB> ProcBFactory { get; set;}
  8.  
  9.       public void WriteLayoutA()
  10.       {
  11.           using (ExportLifetimeContext<ProcessA> lifeOfA = ProcAFactory.CreateExport())
  12.           {
  13.               ProcessA a = lifeOfA.Value;
  14.               Console.WriteLine("Proc A");
  15.               Console.WriteLine("\tPlug A: {0}", a.PlugA.GetHashCode());
  16.               Console.WriteLine("\t\tDal: {0}", a.PlugA.Dal.GetHashCode());
  17.               Console.WriteLine("\tPlug B: {0}", a.PlugB.GetHashCode());
  18.               Console.WriteLine("\t\tDal: {0}", a.PlugB.Dal.GetHashCode());
  19.           }
  20.       }
  21.  
  22.       public void WriteLayoutB()
  23.       {
  24.           using (ExportLifetimeContext<ProcessB> lifeOfB = ProcBFactory.CreateExport())
  25.           {
  26.                 ProcessBb = lifeOfB.Value;
  27.               Console.WriteLine("Proc B");
  28.               Console.WriteLine("\tPlug A: {0}", b.PlugA.GetHashCode());
  29.               Console.WriteLine("\t\tDal: {0}", b.PlugA.Dal.GetHashCode());
  30.               Console.WriteLine("\tPlug B: {0}", b.PlugB.GetHashCode());
  31.               Console.WriteLine("\t\tDal: {0}", b.PlugB.Dal.GetHashCode());
  32.           }
  33.       }
  34.   }

you can see the usage of ExportFactory<T> at lines 11 and 24.

having a lifetime handled part is the first step for the scoping.
now we can define which part will be managed by the scope.
for each scope we except to find a single instantiation of each scoped part.
as you can see in the diagram below, the Plugins and the DAL should be instantiate once for each scope.

Presentation1

in order to define what's goes within the scope we should use the CompositionScopeDefinition as shown in the following code snippet:

Code Snippet
  1. static void Main(string[] args)
  2. {
  3.     var scopeDependentCatalog = new TypeCatalog(
  4.         typeof(ProcessA),
  5.         typeof(ProcessB),
  6.         typeof(PluginA),
  7.         typeof(PluginB),
  8.         typeof(DAL));
  9.     var scopeDefDependent = new CompositionScopeDefinition(scopeDependentCatalog, null);
  10.  
  11.     var appCatalog = new TypeCatalog(typeof(Application));
  12.     var scopeDefRoot = new CompositionScopeDefinition(appCatalog, new[] { scopeDefDependent });
  13.  
  14.  
  15.     var container = new CompositionContainer(scopeDefRoot);
  16.  
  17.     var app = container.GetExportedValue<Application>();
  18.  
  19.     app.WriteLayoutA();
  20.     Console.WriteLine("------------------------------------");
  21.     app.WriteLayoutB();
  22. }

you can see the scope dependent definition at line 9
and the hierarchic between the scope and the application at line 12.

as you may notice it is possible to have deeper hierarchic, but this will be shown in future posts.

the output is:

scopeoutput

remember that we was printing the part's hash code.
you can easily see that each process is having different plugin and DAL's instantiations, but even those the DAL was consumed by both plugins it has a single instantiation per scope.

Summary

scoping is a very powerful instantiation model which can solve some of the real-life scenario which wasn't fall into the shared or non-shared models.

in future post I will present a deeper hierarchic and the catalog filtering capability.


Shout it

MEF 2.0 - mini series: part 5 (Fluent export properties)

MEF 2.0 - mini series: part 5 (Fluent export properties)

this is the 5th post in the MEF 2.0 mini series.
you can see the following TOC for other posts in this series.

in this post I will cover the fluent property's export.

MEF, Import, Export, CompositionContainer, RegistrationBuilder, Catalog

Exporting properties is a less known feature of MEF.
MEF 1 was supporting this feature by using the attribute model.
you could decorate a property with a [Export] attribute and then it become available for imports.

the following code demonstrate property exporting in MEF 1:

the Foo class is importing multiple SymmetricAlgorithm:

Code Snippet
  1. [Export]
  2. public class Foo
  3. {
  4.     [ImportMany]
  5.     public SymmetricAlgorithm[] CryptoProviders { get; private set; }
  6. }

and the CryptoComposer class is exporting a few symmetric algorithms:

Code Snippet
  1. [Export]
  2. public class CryptoComposer
  3. {
  4.     [Export]
  5.     public SymmetricAlgorithm Aes { get { return new AesManaged(); } }
  6.     [Export]
  7.     public SymmetricAlgorithm TripleDES { get { return new TripleDESCryptoServiceProvider(); } }
  8.     [Export]
  9.     public SymmetricAlgorithm RC2 { get { return new RC2CryptoServiceProvider(); } }
  10. }

with the following composition the Foo instance will have the CryptoComposer's symmetric algorithms:

Code Snippet
  1. var catalog = new AssemblyCatalog(typeof(Program).Assembly);
  2. var container = new CompositionContainer(catalog);
  3.  
  4. var foo = container.GetExportedValue<Foo>();
so, how do we do it with the fluent export API?

to keep it simple I will leave the Foo class with the attribute model (we learned how to use fluent import in the previous post):

Code Snippet
  1. [Export]
  2. public class Foo
  3. {
  4.     [ImportMany]
  5.     public SymmetricAlgorithm[] CryptoProviders { get; private set; }
  6. }
  7.  
  8. public class CryptoComposer
  9. {
  10.     public SymmetricAlgorithm Aes { get { return new AesManaged(); } }
  11.     public SymmetricAlgorithm TripleDES { get { return new TripleDESCryptoServiceProvider(); } }
  12.     public SymmetricAlgorithm RC2 { get { return new RC2CryptoServiceProvider(); } }
  13. }

in order to export the CryptoComposer's properties you should use the following code:

Code Snippet
  1. var picker = new RegistrationBuilder();
  2. picker.ForType<CryptoComposer>()
  3.     .ExportProperties(p => p.PropertyType == typeof(SymmetricAlgorithm));
  4.  
  5. var catalog = new AssemblyCatalog(typeof(Program).Assembly, picker);
  6. var container = new CompositionContainer(catalog);
  7.  
  8. var foo = container.GetExportedValue<Foo>();
summary

exporting properties using the fluent API is fairly straightforward.


Shout it

Open House at Microsoft

Open House at Microsoft

yesterday I was lecturing at Microsoft about VS 2012, .NET 4.5, async/await, Rx and TPL Dataflow.

there was 90 people attended and I hope that everybody has learn something new.

the code sample for this lecture available here.

Parallel,tdf, dataflow, rx, reactive, task, .net 4., vs 2012

Async and AggregateException

Async and AggregateException

this post is a complementary to Eran Stiller's post.

I was reading Eran Stiller's post about exception handling using async methods and I want to add a few side notes.

async, await, parallel, task, tpl, tap,.net4.5,c#5, exception

1) the exception handling behavior decisions is well documented in this post (by the TPL team), it decided after they had consider different options.

2) I was suggesting that compiler will check whether the a catch of AggregateException is implemented and if so to avoid the unwrapping behavior.
in this case the it can be assumed that programmer do expect multiple exceptions.
unfortunately, for the current version, I was  speaking with them after the release of .NET 4.5.
I hope that we will see this mitigation in future release.

3) for the current version you can use the following work around if you do want to catch aggregate exception:

Code Snippet
  1. await Task.WhenAll(t1, t2)
  2.     .ContinueWith(t => { if (t.Exception != null) throw t.Exception; });

by re-throwing the exception the unwrapped exception will be AggregateException.
it may be wise to wrap this pattern in an extension method because it can be a very repetitive one.

Proper Disclosure

Eran is one of my colleague and definitely one of the more talent Architect I was happened to work with.


Shout it
More Posts Next page »