Rx is now an official and supported product
Head over to Channel9 to read more about the official release. The setup package (available here) contains the components for .NET 3.5 SP1, .NET 4, Silverlight 4 and Windows Phone 7.
Head over to Channel9 to read more about the official release. The setup package (available here) contains the components for .NET 3.5 SP1, .NET 4, Silverlight 4 and Windows Phone 7.
[Updated on 02 Jul 11 after the Rx official release: Renamed Run to ForEach]
[If you don’t know what Ix (Interactive Extensions for .NET) is, then I recommend reading my earlier post.]
There are very good resources (like this video) on Ix operators so I am not going to spend too much time on this but I am going to talk about a few of these operators (my top three) to show how the interactive extensions can make your code easier to write and read.
I will use unit tests to express the functionality I am expecting and will then provide potential solutions without using Ix, and finally showing how Ix would simplify the implementation.
For the code snippets, assume that all methods (excluding the test methods) live in a static class named MyEnumerableScrathPad.
You have an object of type T and want an enumerable sequence of T, which includes that object only.
[TestMethod]
public void ReturnTest()
{
var item = new object();
var returnedItem = MyEnumerableScratchPad.Return(item).Single();
Assert.AreSame(item, returnedItem);
}
public static IEnumerable<T> Return<T>(T item)
{
return new T[] { item };
}
public static IEnumerable<T> Return<T>(T item)
{
return EnumerableEx.Return(item);
}
You have an enumerable sequence of T and you want to add an instance of type T to the beginning of that sequence.
[TestMethod]
public void StartWithTest()
{
var count = 10;
var zero = 0;
var originalRange = Enumerable.Range(1, count);
var newRange = MyEnumerableScratchPad.StartWith(originalRange, zero);
Assert.AreEqual(zero, newRange.First());
Assert.AreEqual(count + 1, newRange.Count());
}
public static IEnumerable<T> StartWith<T>(IEnumerable<T> originalSequence,
T first)
{
var list = originalSequence.ToList();
list.Insert(0, first);
return list.AsEnumerable();
}
This will make the test pass but this implementation may force you to iterate through the items unnecessarily because of the call to ToList. We were also lucky that this implementation passed the test. If you change the value of count from 10 to int.MaxValue – 1, the test will horribly fail with an OutOfMemoryException. So the idea of converting the enumerable to a list is not a good one.
public static IEnumerable<T> StartWith<T>(IEnumerable<T> originalSequence,
T first)
{
yield return first;
foreach (var item in originalSequence)
yield return item;
}
This implementation does not suffer from the problem shown above and passes the test after a fairly long delay (around 10 seconds on my laptop), which is expected as a result of the call to Count, which will iterate through the sequence and one can argue that this is not a good test but it fits the purpose here.
public static IEnumerable<T> StartWith<T>(IEnumerable<T> originalSequence,
T first)
{
return originalSequence.StartWith(first);
}
Ix also has another overload for StartWith, which takes a param array of items to be inserted at the beginning of the sequence, which can be handy.
You have an enumerable sequence and you want to perform an action for each item of the sequence.
[TestMethod]
public void ForEachTest()
{
var list = new List<int>();
var range = Enumerable.Range(1, 10);
MyEnumerableScratchPad.ForEach(range, list.Add);
CollectionAssert.AreEqual(range.ToList(), list);
}
public static void ForEach<T>(IEnumerable<T> sequence, Action<T> action)
{ foreach (var item in sequence)
{
action(item);
}
}
Clearly this is a valid solution but we are looking for a more compact solution.
public static void ForEach<T>(IEnumerable<T> sequence, Action<T> action)
{
Array.ForEach(sequence.ToArray(), action);
}
public static void ForEach<T>(IEnumerable<T> sequence, Action<T> action)
{
sequence.ToList().ForEach(action);
}
Implementations #2 and #3 are not ideal as they both force the enumeration of the sequence before the iterating through the items begins. This is not necessarily a problem but there are some cases where you want to perform the action as soon as the sequence yields a new value.
public static void ForEach<T>(IEnumerable<T> sequence, Action<T> action)
{
sequence.ForEach(action);
}
Because most of the Ix operators including ForEach are ported back from the Rx world, there are some interesting additional overloads for ForEach. For example, there is one that takes action of T (the action that needs to be performed on each item) and then another action that will be performed when the iteration over the sequence is completed.
These three operators (Return, StartWith and ForEach) are among the simplest of the long list of Ix operators but nonetheless they are the ones I use most as they make code more concise and easier to read.
[Updated on 02 Jul 11 after the Rx official release]
I have to start this post with a clarification: “Ix” is not the name of an official product. I am just calling it so to highlight the benefits of this less known part of the Reactive Extensions or “Rx” (which is a real product by the way).
Interactive Extensions used to be part of the Rx library but they can now be downloaded separately. Rx is now officially released and the stable version can be downloaded from here. Ix is now packaged separately and its experimental release can be downloaded from here. Both of these components are also available on the main Nuget feed.
Even many developers who are using Rx are unaware of this interactive part of the library and I believe it deserves more attention. But let’s start with Rx, just in case you don’t know what it is.
Rx is a library developed by Microsoft, which allows you to write applications that react to events using a nice and clean implementation of the observable pattern. Rx started off as a Microsoft DevLabs incubation project but it proved to be very popular so its incubation period was ended in April 2011 and since then, it has been moved to the MSDN Data Developer Center.
I refer to the interactive part of the Rx product family as “Ix”, which deals with enumerable sequences (IEnumerable<T>) whereas Rx focuses on the observable sequences (Observable<T>).
If you have watched Inside the Rx video on Channel9 or read Bart De Smet’s blog post on this topic, you already know that enumerable sequences are the duals of the observable sequences. When the Rx team created the operators for the observable sequences, they also back ported some of those operators to the enumerable world and that is how Ix was born.
The following diagram shows the dependencies between major Rx and Ix components. Note that the reactive extensions come with a few more assemblies (related to async support, client profile and more) which are not displayed here.
So like Rx, Ix takes a dependency on:
- CoreEx: Providing the core functionality such as a selection of helpers for disposable objects, schedulers and concurrent data structures.
- System.Observable: Containing IObservable<T> and IObserver<T> interfaces. [Not applicable to .NET 4.0 as it is included in mscorlib 4.0]
The pre-release versions of the Rx product family consisted of various components such as System.Reactive, System.Interactive, CoreEx and System.Observable (for all runtimes except .NET 4.0). In the official release, the component structure is simplified and there are only two assemblies involved: System.Reactive and System.Interactive. All of the types required by those assemblies are now embedded in so for example, the IObservable and IObserver interfaces for runtimes other than .NET 4 now live inside System.Reactive. Same applies to the disposable and scheduler types.
If you are not using Rx family for whatever reason, it is now a good time to do so. I will go as far as saying:
If you are a .NET developer, then you should be using Rx and/or Ix in your projects or you are doing something wrong!
You might say I am taking it too far but I insist! If you are using Rx already, then you know what I am talking about. If you aren’t, you will know it when you start using it. Rx targets various platforms and frameworks including .NET (3.5, 4.0 for both client and full profiles), Silverlight (3, 4, 5), Windows Phone, Xbox and Javascript so you can use it in any .NET project.
These extensions can be downloaded by following the links on the product landing page on MSDN. You can also find a variety of Rx and Ix components on the official Nuget feed.
The primary type in Ix is the EnumerableEx type, which adds a long list of operators (in the form of extension methods) to IEnumerable<T>, plus a few operators for enumerable sequence of numbers (to support average, min, max, etc).When developers look at the list of operators provided by these extensions, most people ask “Why aren’t these included in .NET Framework anyway?”. I think (and hope) that this is just a matter of time before they make their way into the framework. Once you start using these extensions, you will soon find out that you cannot live without them, especially if you use LINQ frequently.
In the next post, I will briefly go through some of the Ix operators to show how it can help you in writing code that is easier to write and read.
This is a link post with links to tutorials, samples and training courses covering the recent releases of Microsoft’s development platform tools and technologies.
Visual Studio 2010 and .NET Framework 4 Training Kit (June 2010)
Covers:
Identity Developer Training Kit (June 2010)
Covers:
Windows Azure Training Kit (June 2010)
Covers:
Windows Server AppFabric Samples (June 2010)
Covers:
Silverlight 4 Training (April 2010)
Covers:
You can download the ADO.NET Entity Framework Beta 3 and ADO.Net Entity Framework Tools Dec 07 CTP.
This release enables the users to work with the ADO.NET Entity Framework using Visual Studio 2008 RTM and .NET Framework 3.5 RTM. It also includes bug fixes, performance improvements as well as new features.
As you may know, amongst other features, the Entity Framework adds support for various data sources so the Entity Framework extends the capabilities of LINQ to work with many back-end databases. Various database providers are developing Entity-Framework enabled versions of their offerings and some of these offerings are scheduled to be publicly available shortly after the RTM of the Entity Framework. Microsoft is updating the provider for SQL Server Compact Edition as part of the SQL Server 2008 release. Microsoft will also provide an Entity Framework enabled version of the .NET Provider for DB2, which will ship in BizTalk Server “6″.
More information can be found on the ADO.NET Team Blog, MSDN Data Platform Developer Center and in the press release.
If you are interested in the Entity Data Model and want to know more about Microsoft’s Data Access strategy in Orcas and .NET Framework, then have a look at this great blog post published by our data access team over the weekend.
“LINQ to Entities” will supplement other LINQ extensions like “LINQ to SQL” and has great and useful features including but not limited to: mapping single class to multiple tables, modelling many to many relationships, working with relational stores other than SQL Server and sharing a single model across the Database Engine, Reporting Services, Analysis Services and Integration Services.
“LINQ to Entities” will be part of the ADO.NET Entity Framework, which is an extension to the Orcas version of .NET Framework and will ship after Orcas but in the first half of 2008.