Monday, November 5, 2012

Adding reactivity to NetMonitor

It was long time ago when I last time wrote about updates in the NetMonitor project. I was just using it from time to time to diagnose my network connection and it worked pretty well. However, I wasn't happy with its internals, especially with PingService implementation as it was rather complex for such simple job - it had to deal with entities from System.Threading namespace (e.g. Timer) to get called from time to time in background thread and this only added complexity without adding any business value.

Some time ago I tried playing with Task Parallel Library, but eventually it appeared that TPL is not applicable in this scenario.

This time I've found Rx Extensions and I really liked the approach. My task with sequential background execution is solved with the help of the extensions quite gracefully (here is simplified version):

var service = new PingService("google.com");

// Every second the ping service will be called from background thread, producing a sequence of metrics
var metrics = Observable.Interval(TimeSpan.FromSeconds(1))
    .Select(i => service.Execute());

// Consuming the sequence of metrics and updating UI
subscription = metrics.Subscribe(
    metric => Console.WriteLine(metric.RoundtripTime),
    exception => Console.WriteLine(exception.Message));

Applying Rx Extensions drastically simplified the service itself and program environment as additionally I got rid of events and handlers, I am satisfied with the changes it brought. In my scenario it looks like a simple messaging bus between services and their consumers.

I'm pretty confident that Reactive Extensions can change your attitude to composing asynchronous and event-based programs (as it once happened with invention of LINQ for data querying). More on Rx can be found on MSDN, 101 Samples, IntroToRx.

Sunday, November 4, 2012

Dependency injection for smarties

Till the last week I was thinking that dependency injection is a rather simple thing and mostly is about resolving a component by its interface/base class with some nuances like providing additional information as name, constructor signature, transient/singleton instance, etc. Almost everyone among my friends wrote their own implementation of IoC container, that's not big deal. They differ one from another by name of main method (Get/Resolve/Whatever) and internal implementation, but all they were about was simple component resolving and no more.

However, last week I was diving deep in autofac IoC container and one thing in their documentation just made me rethink all that I knew about the relationships between the components (actually, I didn't think of anything except the first type from the list). Here is what they call relationships:

Relationship Constructor signature Meaning
A needs B A(B b)Dependency
A needs B at some point in the future A(Lazy<B> b)Delayed instantiation
A needs B until some point in the future A(Owned<B> b)Controlled lifetime
A needs to create instances of B A(Func<B> b)Dynamic instantiation
A provides parameters of types X and Y to B A(Func<X,Y,B> b)Parameterisation
A needs all the kinds of B A(IEnumerable<B> b)Enumeration
A needs to know X about B A(Meta<T> b) and A(Meta<B,X> b)Metadata interrogation

More on the relationships you can read in the dedicated article Relationship Zoo, highly recommended. I hope it will make you rethink what DI was meant for you.

Friday, November 2, 2012

Visual Studio 2010, 2012 vs. Inconsolata font, part 3

Earlier, I promised to update the status of the fight "Visual Studio 2010 vs. Inconsolata" as soon as I have a chance to try the font in Visual Studio 2012. However, I noticed that the font has been updated also and the latest version of Inconsolata is available from September, 2012.

So first, I tried it in Visual Studio 2010, silently hoping that the troubles are gone. And yes, the extra boldness of some symbols is no more the case. Here is how it looks:

Inconsolata in Visual Studio 2010

After that I switched to Visual Studio 2012. No surprises, the font is rendered perfectly:

Inconsolata in Visual Studio 2012

So I guess, the confrontation is over. If you love Inconsolata font, just get its latest version from google code repository and enjoy!