Tag Archives: C++

Solving cross-assembly WPF Resource problems once and for all

I wrote several posts about WPF weakness when it comes to sharing resources. Our use case, at my at work, where we load multiple versions of an assembly into a single app domain this problem has been felt most acutely. As I wrote previously, using the most documented way of loading resource dictionaries, unless you specify a version of an assembly when loading a resource dictionary you are running the risk of loading a wrong resource dictionary because you may have multiple versions of same library loaded into an app domain. And such problems are notoriously difficult to catch and understand.

At least now, it’s easy to solve. I am glad to announce that I found a definitive way to share resources across assembly boundaries without running into versioning issues. The key to doing this is to never load resource dictionary from another assembly using cross-assembly Uris. I did not know that there was another way until recently. All the examples of loading resource dictionaries, that I can recall, use the Source=”{PackUri}” method of loading, but there’s a way to reference resource dictionary using code.

A ResourceDictionary, just like any other class that can be defined in Xaml, can also contain the “code behind” portion, a source code file associated with the main Xaml file. If you already have resources defined in a resource dictionary somewhere in your assembly, you don’t have to change it. Instead you’ll create a brand new resource dictionary which you would use to export the resource for sharing. For example, let’s say you have a resource dictionary defining the colors of the stoplight. It lives in an assembly called ResourceSharing, in a namespace ResourceSharing.Resources in a file called Resources.xaml.


<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    
    <SolidColorBrush x:Key="Go" Color="Green"/>
    <SolidColorBrush x:Key="Stop" Color="Red"/>
    <SolidColorBrush x:Key="PrepareToStop" Color="Yellow"/>
    
</ResourceDictionary>

To export this ResourceDictionary, create a new Resource Dictionary for export. Let’s call it ResourceSharing.Resources.ExportResourceDictionary. I’ll consist of the xaml and the C# portions

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
                    x:Class="ResourceSharing.Resources.ExportResourceDictionary"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="Resources.xaml"/>
    </ResourceDictionary.MergedDictionaries>
    
</ResourceDictionary>

Notice the Class definition on line 2.

The code behind class looks like this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ResourceSharing.Resources
{
    public partial class ExportResourceDictionary
    {
        //Expose it as singleton to avoid multiple instances of this dictionary
        private static readonly ExportResourceDictionary _instance = new ExportResourceDictionary();

        public static ExportResourceDictionary Instance
        {
            get { return _instance; }
        }

        public ExportResourceDictionary()
        {
            InitializeComponent();
        }
    }
}

Let’s also create a namespace we can use to make it easier to reference these resources from another assembly using namespaces

using System.Windows.Markup;

[assembly:XmlnsDefinitionAttribute("http://my.schemas.com/web/resources", "ResourceSharing.Resources")]

How to Share

Here’s how to consume the resources exported from the ResourceSharingAssembly. For this example, I had an executable assembly called ResourceSharingClient, where I am referencing exported resource right in the Main Window. Notice the use of the exported namespaces in line 2 and the way that I am referencing a static instance of the ExportResourceDictionary. Such references are version-proof because .NET knows the version of the dependent assemblies. There’s no need to dynamically discover the dependent assembly and the code actually looks a lot neater too. I know that such approach requires a bit more work, but the final result is a lot safer. I only wished that such approach had been advertised by Microsoft WPF evangelists from the beginning.

<Window
        x:Class="ResourceSharingClient.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:rs="http://my.schemas.com/web/resources"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
        mc:Ignorable="d"
        Title="MainWindow" d:DesignWidth="111.5" d:DesignHeight="109.5" Width="120" Height="130">
    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <x:Static Member="rs:ExportResourceDictionary.Instance"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>
    <Border BorderBrush="Black" BorderThickness="2" HorizontalAlignment="Center" VerticalAlignment="Center" Padding="2" CornerRadius="2" Background="Silver">
        <StackPanel Orientation="Vertical">
            <Ellipse Width="20" Height="20" Fill="{DynamicResource Stop}" Stroke="Black" StrokeThickness="1"/>
            <Ellipse Width="20" Height="20" Fill="{DynamicResource PrepareToStop}" Stroke="Black" StrokeThickness="1"/>
            <Ellipse Width="20" Height="20" Fill="{DynamicResource Go}"  Stroke="Black" StrokeThickness="1"/>
        </StackPanel>
    </Border>
</Window>

Launching ClickOnce Application with parameters from a shortcut

ClickOnce is a handy technology from Microsoft that makes it easy to deploy desktop Windows applications using the web browser. IE is naturally supported out of the box, Firefox and Chrome have add-ons and apps respectively to make ClickOnce work.

When installed, ClickOnce created shortcuts for the application under user’s Start Menu (Windows 7 and 10) in the folder whose name matches the name of the company specified in the manifest of the application. But the shortcuts to ClickOnce applications are not your typical *.lnk files, they are .appref-ms files. An appref-ms file is a text file saved in UTF-16 or “UCS2 Little Endian” format. Format is important getting it wrong will make the file unrecognizable to the system.

Here’s a typical text from the appref-ms file.

https://www.application.com/MyApplication.application#MyApplication.app, Culture=neutral, PublicKeyToken=f89fd69c4bd8bc85, processorArchitecture=x86

It consists of 4 parts separated by comas:

  1. Url#Name – I noticed that the URL is not important, but the bame is – it has to match the name of the application in the manifest
  2. Culture – in the format “Culture=”
  3. PublicKeyToken – in the format “PublicKeyToken=”
  4. Target CPU – in the format “processorArchitecture=”
  5. To pass parameters to the clickonce application you must invoke the appref.ms file either from the command line with parameters. I was hoping to create a real shortcut (lnk) file with the appref.ms file as a target along with arguments, but Windows does not make it easy. It’s a lot easier to manage shortcuts to normal executable files, so that’s the approach I would recommend in this solution.

    You can add a second windows executable file to your solution. It’s job would be to generate a temporary appref.ms file on the fly and launch it with parameters using .NET’s Process.Start command. When your real app starts, make sure to copy this helper executable to user’s %appdata%\YourAppName location or ClickOnce’s ApplicationDeployment.CurrentDeployment.DataDirectory (will only be available once properly deployed). Then you can create a shortcut to the executable anywhere you like and even add command line arguments to it. Your executable then will generate the appref.ms file on the fly somewhere in temp directory, and launch it along with the arguments you specified. You can get the parts necessary to generate this file from the ApplocationIdentity object: http://msdn.microsoft.com/en-us/library/system.applicationidentity.fullname%28v=vs.110%29.aspx

    Hope this works for you.

    PS. Don’t forget to delete the dummy appref.ms file you generate after your app launches.

Microsoft mechanism to load WPF resources is broken. (looking for a better title)

I’ve been using WPF along with .NET and have been a staunch supported of the technology for years now. The pattern of separating presentation from logic gained a lot of traction. Technologies such as iOS’s cocoa touch, JavaFX and others follow same model. Even HTML5 forced all the pure presentation/layout markup out from HTML and into CSS. So the idea is sound. But what about execution.

WPF implements this pattern using XAML. XAML markup is used to design the visual this xaml is stored in the xaml file, whose name usually matches the name of the class. For example, MyWindow.xaml. During compilation, an auto-generated file marries the code that defines the visual (MyWindow.xaml.cs) and the xaml file itself (MyWindow.xaml.cs). The autogenerate file would be named MyWindow.g.i.cs and it would contain the implementation of the InitializeComponent() method that’s referenced in the main MyWindow.xaml.cs class. The contents of the method looks something like this:

[System.Diagnostics.DebuggerNonUserCodeAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute(&quot;PresentationBuildTasks&quot;, &quot;4.0.0.0&quot;)]
public void InitializeComponent() {
	if (_contentLoaded) {
		return;
	}
	_contentLoaded = true;
	System.Uri resourceLocater = new System.Uri(&quot;/MyLibrary;component/mainwindow.xaml&quot;, System.UriKind.Relative);
	
	#line 1 &quot;..\..\MainWindow.xaml&quot;
	System.Windows.Application.LoadComponent(this, resourceLocater);
	
	#line default
	#line hidden
}

So far so good right. OK, let’s talk about some of the advanced uses of .Net. Specifically support for having multiple versions of same assembly (dll) to be loaded in a single AppDomain. Such use case comes handy in a framework use case. A framework supports multiple plug-in modules, each can be developed by other developers who may rely on different versions of common libraries. I am not talking about things like log4net, I am talking about shared assemblies that define common logic and can be reused by each module. If 2 modules are loaded into a single app-domain and both depend on their ‘private’ version of the common assembly, a good framework should not force both modules to share latest version of the library, because

  • ‘latest’ does not mean backwards-compatible
  • ‘latest’ may have bugs that ‘earlier’ version did not
  • …I can go on for a while on this one. It’s 2014, dll hell is well known to software developers of the 20th century.

Now, let’s say that 2 different versions of the shared library called MyLibrary are loaded and one version is used by Module1 and another version is used by Module2. During compilation, assembly name along with the version are recorded in modules’ manifests so that when .NET encounters request to execute code defined in MyLibrary the source module will tell .Net which version of MyLibrary contains the code it wants executed. This is a basic building block of .NET and a very attractive feature.

Now, what about WPF? Well that’s not that clear is it? Look at Line #8 above: System.Uri resourceLocater = new System.Uri(“/MyLibrary;component/mainwindow.xaml”, System.UriKind.Relative). It defines what it calls a resourcelocator which the system will use to find the xaml resouce in an assembly called MyLibrary. I don’t know how WPF resolves the assembly, but if I were to write the logic for resolution, I would not have enough information to find a proper assembly MyLibrary, because I don’t know which version to load. I would have to guess. And as we know in any science guessing is bad. Computer Science is an exact… well… science. Guessing, hoping, holding fingers crossed or other techniques that do not produce determinate result will break at the worst possible moment! I can see how developers armed with inaccurate knowledge or with little experience make mistakes like this, but when Microsoft’s tools generated code that’s faulty, and make it impossible to intercept and fix, there is no other way to call it – they fucked up! How can they expect developers to trust their tooling if they, themselves, auto-generate code that sometimes breaks.

This breaks in a most bizarre way, too. MyLibrary version 2 was loading the MainWindow class, which then was running the InitializeComponent() method above, encountered the LoadComponent call, which asked to load the resource from the MyLibrary assembly without a version, and the code picked the wrong version of the MyLibrary assembly, which did not have the MyWindow class, nor the resource. What a way to blow up!

Such nonsense was reported to them too! This connect ticket marked as “Wont’t Fix” describes the problem: https://connect.microsoft.com/VisualStudio/feedback/details/668914/xaml-generated-code-uses-resource-uri-without-assembly-strong-name. StackOverflow also has references to this problem: http://stackoverflow.com/questions/1453107/how-to-force-wpf-to-use-resource-uris-that-use-assembly-strong-name-argh/6341678#6341678. Luckily one of the suggestions led me to the best solution.

The Fix


We were lucky to have our builds automated via scripts which rely on MSBuild to build the solutions. All I needed to do is to modify the script and pass the /p:AssemblyInfo=$version parameter to MSBuild. (http://stackoverflow.com/a/26689750/195275). This would tell the build system to add assembly version to the Resource reference in line 8. It now looks like the code below. Problem solved, but could have been avoided on the first place!

Shame on you, Microsoft!

[System.Diagnostics.DebuggerNonUserCodeAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute(&quot;PresentationBuildTasks&quot;, &quot;4.0.0.0&quot;)]
public void InitializeComponent() {
	if (_contentLoaded) {
		return;
	}
	_contentLoaded = true;
	System.Uri resourceLocater = new System.Uri(&quot;/MyLibrary;V1.0.23;component/mainwindow.xaml&quot;, System.UriKind.Relative);
	
	#line 1 &quot;..\..\MainWindow.xaml&quot;
	System.Windows.Application.LoadComponent(this, resourceLocater);
	
	#line default
	#line hidden
}

Am I smarter than the fifth grader?

I thought I was, until my 10 year-old brought home a math homework from her school asking her to solve a few problems that look like this:

Replace letters with digits from this list [1, 2, 3, 4, 5, 7, 8, 9]
All letters to digit mapping is unique

   CAN
x  SAY
======
 BRAND

I thought such problem would have a quick-and-easy solution, like any others in a 5th grade math, but I was stumped. I tried several tricks to see if there was a shortcut to solving these, but found nothing. The only way to solve a problem like this was to iteratively try out various number combinations to see if the result would match the constraints of the problem.

Constraints

In a problem above the constraints are as follows:

  1. 2nd digit in the 2nd number must match 2nd digit in the 1st number
  2. 2nd and 3rd digit in the 1st number must match 3rd digit and 4th digits result
  3. Result must have 5 digits
  4. None of the digits of the result except for the 3rd and 4th can be present in the first two numbers

Solving such problem.

I learned that the type of these problems are called cryptarithms or verbal arithmetic. There’s a wikipedia page dedicated to these problems as well as a laundry list of sites hosting examples and explanations to these. There is no simple solution to such problem. The solution to them is defined as having NP-Complete in its difficulty, which according to Wikipedia is

In computational complexity theory, a decision problem is NP-complete when it is both in NP and NP-hard. The set of NP-complete problems is often denoted by NP-C or NPC. The abbreviation NP refers to “nondeterministic polynomial time”.

Although any given solution to an NP-complete problem can be verified quickly (in polynomial time), there is no known efficient way to locate a solution in the first place; indeed, the most notable characteristic of NP-complete problems is that no fast solution to them is known.

My Solution

The only way to solve it was to write a program, which I did below. I figured that I have to start with a basic mapping of letters to digits and “increment” the mapping by “1” at every iteration. The difficult part was to implement the “increment” because no two letters can be mapped to the single digit, I could not completely mimic the way we increment regular numbers in base 10. Plus the universe of digits can also be constrained if the problem creator chooses to do so.

The code is written in C#, and is lazy-coded, where the inputs are hard-coded in the main method, because I own the code and can easily change the definitions. It supports multiplication and addition. It managed to solve all the problems that I found online, so I am fairly confident that it works.

Perhaps I am still smarter than the 5th grader, even if slightly.

namespace WordMultiplication
{
    class Program
    {
        static void Main(string[] args)
        {
            var digits = new int[] { 1, 2, 3, 4, 5,  7, 8, 9 }.OrderBy(n => n).ToArray();
            var word1 = "CAN";
            var word2 = "SAY";
            var result = "BRAND";

            var orderedInputLetters = word1.Union(word2).Distinct().ToArray();
            if (orderedInputLetters.Length > digits.Length)
            {
                Console.WriteLine("More letters than digits!");
                return;
            }

            var numbersToLetterMap = new SortedDictionary<int, char?>();
            var lettersToNumbersMap = new Dictionary<char, int>();

            InitialSetup(orderedInputLetters, digits, numbersToLetterMap, lettersToNumbersMap);

            bool found;
            bool couldIncrement;
            do
            {
                couldIncrement = false;
                int number1 = GetNumberForWord(word1, lettersToNumbersMap);
                int number2 = GetNumberForWord(word2, lettersToNumbersMap);

                var mathResult = number1 * number2;

                found = number1.ToString().Length == word1.Length &&
                        number2.ToString().Length == word2.Length &&
                        Verify(mathResult, result, word1, word2, new Dictionary<char, int>(lettersToNumbersMap), digits);
                if (found)
                    Console.WriteLine("{0} x {1} = {2}", number1, number2, mathResult);
                else
                    couldIncrement = TryIncrement(orderedInputLetters.Length - 1, orderedInputLetters, digits, numbersToLetterMap, lettersToNumbersMap);


            } while (!found && couldIncrement);

            if (!found)
                Console.WriteLine("Result not found");
        }

        private static bool TryIncrement(int index, char[] inputLetters, int[] digits,
            IDictionary<int, char?> numbersToLetterMap, IDictionary<char, int> lettersToNumbersMap)
        {
            if (index == -1)
                return false;

            var currLetter = inputLetters[index];
            var currDigit = lettersToNumbersMap[currLetter];

            int nextDigit;
            //If we reached the end
            if (!numbersToLetterMap.Any(kvp => kvp.Key > currDigit && !kvp.Value.HasValue))
            {
                //put current letter aside
                numbersToLetterMap[currDigit] = null;
                lettersToNumbersMap[currLetter] = -1;

                //Try to increment a digit of higher significance
                if (TryIncrement(index - 1, inputLetters, digits, numbersToLetterMap, lettersToNumbersMap))
                    //If succeeded, find the first available empty spot for the current letter
                    nextDigit = numbersToLetterMap.First(kvp => !kvp.Value.HasValue).Key;
                else
                    return false;
            }
            else
            {
                numbersToLetterMap[currDigit] = null;
                nextDigit = numbersToLetterMap.First(kvp => kvp.Key > currDigit && !kvp.Value.HasValue).Key;
            }

            numbersToLetterMap[nextDigit] = currLetter;
            lettersToNumbersMap[currLetter] = nextDigit;

            return true;
        }


        private static void InitialSetup(char[] inputLetters, int[] digits, IDictionary<int, char?> numbersToLetterMap, IDictionary<char, int> lettersToNumbersMap)
        {
            foreach (var digit in digits)
                numbersToLetterMap[digit] = null;

            foreach (var c in inputLetters)
                lettersToNumbersMap[c] = -1;

            //Place letters in the beginning of the digit line
            for (int i = 0; i < inputLetters.Length; i++)
            {
                lettersToNumbersMap[inputLetters[i]] = digits[i];
                numbersToLetterMap[digits[i]] = inputLetters[i];
            }
        }

        private static bool Verify(int mathResult, string result, string word1, string word2, IDictionary<char, int> allletters, int[] digits)
        {
            if (mathResult.ToString().Length != result.Length)
                return false;

            for (int i = 0; i < result.Length; i++)
            {
                var resultChar = result[i];
                var digitForChar = (mathResult / (int)Math.Pow(10, result.Length - i - 1) % 10);
                
                //Can result be even used?
                if (Array.IndexOf(digits, digitForChar) == -1)
                    return false;

                if (allletters.Keys.Contains(resultChar))
                {
                    if (allletters[resultChar] != digitForChar)
                        return false;
                }
                else if (allletters.Values.Contains(digitForChar))
                {
                    if (allletters.First(kvp => kvp.Value == digitForChar).Key != resultChar)
                        return false;
                }
                else
                {
                    allletters[resultChar] = digitForChar;
                }
            }

            return true;

        }

        private static int GetNumberForWord(string word, IDictionary<char, int> allletters)
        {
            var number = 0;
            for (int i = 0; i < word.Length; i++)
            {
                number += allletters[word[i]] * (int)Math.Pow(10, (word.Length) - i - 1);
            }

            return number;
        }
    }
}

How to use .NET WebClient synchronously and still receive progress updates

Recently, I had to implement our custom package deployment mechanism for our UI framework where packages are hosted on a web-accessible URI. During the download process, the UI should display a progress bar which updates the user on the progress the download. After download is complete, the client performs a few more steps including

  • checking file integrity against its md5 hash
  • expanding the compressed file into the destination
  • cleaning up

The implementation uses TPL; for each package that user chose to upgrade, a task is created which performs the steps above. During this process, the UI displays a progress of each task.

The download portion of the process is implemented using WebClient.DownloadFile method. It’s short and sweet, hiding most of the complexity of opening a connection, getting a response stream and reading it one chunk at a time. But its simplicity comes a shortcoming: during download the thread doing the downloading is blocked an no progress is reported, so downloading a large file would cause the user to think that the system froze, because even though the wait spinner is spinning, the progress bar is stuck. What I want is to get updates on the progress of my download while waiting for download to complete.

The WebClient class has convenient DownloadProgressChanged event which seems like a perfect candidate to help me here, so I attached a delegate to it, but it was never called. Upon careful reading of the documentation, I learned that I must use the asynchronous version of the DownloadFile mehtod, DownloadFileAsync to receive any progress updates.

While I could switch my code around to accommodate new asynchronous calling pattern, I did not want to drastically redesign my existing code. It was already relying on the TPL for asynchrony, adding another asynchronous mechanism would add more complexity to my code. So I decided to try to keep the method that deals with WebClient synchronous, but still receive updates. To do this, I would need to call the DownloadFileAsync method, but then block my thread until download completes or fails. Luckily, WebClient has the DownloadFileCompleted event which would come in handy here. Since I would block my thread after calling the DownloadFileAsync method, when the DownloadFileCompleted fires, I would unblock my calling thread. Here’s the code that implements this solution


public void DownloadFile(Uri uri, string desintaion)
{
  using(var wc = new WebClient())
  {
    wc.DownloadProgressChanged += HandleDownloadProgress;
    wc.DownloadFileCOmpleted += HandleDownloadComplete;

    var syncObj = new Object();
    lock(syncObject)
    {
       wc.DownloadFileAsync(sourceUri, destination, syncObject);
       //This would block the thread until download completes
       Monitor.Wait(syncObject);
    }
  }
 
  //Do more stuff after download was complete
}

public void HandleDownloadComplete(object sender, AsyncCompletedEventArgs args)
{
   lock(e.UserState)
   {  
      //releases blocked thread
      Monitor.Pulse(e.UserState);
   }
}


public void HandleDownloadProgress(object sender, DownloadProgressChangedEventArgs args)
{
  //Process progress updates here
}

At the end, I still use WebClient synchronously without losing the benefits that come with its asynchronous usage.

Happy Downloading!

Calculating Moving Average (or other aggregations) using LINQ

A while ago I wrote about calculating moving average of live data using the Buffer() method in Reactive Extensions. The method creates a buffer of updates whose boundary you, as the developer specify, and provides you this running buffer with every subsequent update. The ‘current’ update would be last in the buffer, which allows you to calculate the inclusive moving average, one where the last factor is included in the results.

Recently, I had to do something similar with static data in an application which was not using RX, bud had a well-defined LINQ-based data provider. To my surprise, I found that LINQ does not provide a buffering method out of the box. To my greater surprise, I found that it was easy to implement one. The code is below.

I did not want to assume that we would only perform average aggregations, therefore, I kept my solution fairly generic. It would be up to the user of my function to code up the aggregation logic. In this post, I would demonstrate average aggregations, but the user of this function would be able to do anything with a given set of items.

First, let’s define a data structure, called Aggregation which would be used to wrap the original and add a new AggregatedValue property (line 16) that we would be populated during aggregation process. Then I define a sample TimedDataPoint which would be used to hold stock prices in my example.

public struct Aggregation<TSource, TValue>
{
	private TSource _dp;

	public Aggregation(TSource dataPoint)
	{
		_dp = dataPoint;
		AggregatedValue = default(TValue);
	}

	public TSource DataPoint
	{
		get { return _dp; }
	}

	public TValue AggregatedValue;
}

public struct TimedDataPoint<T>
{
	public DateTime Date { get; set; }

	public T Value { get; set; }

	public override string ToString()
	{
		return string.Format("{0} @ {1}", 
 			Date.ToShortDateString(), Value);
	}
}

Finally, let’s define a LINQ function that would buffer a set of items in the original set and would return a modified set with fewer items, where each item would contain an aggregation value for the buffered set.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MovingAverage.Linq
{
    public static class Aggregations
    {
        public static IEnumerable<Aggregation<TSource, TValue>> MovingAggregation<TSource, TValue>(
            this IEnumerable<TSource> source,
            Func<TSource, int, bool> shouldBuffer,
            Func<Aggregation<TSource, TValue>, TSource, IEnumerable<TSource>, TSource, int, TValue> projection)
        {
            Queue<TSource> buffer = new Queue<TSource>();
            TSource droppedItem = default(TSource);
            var runningAgg = default(Aggregation<TSource, TValue>);

            foreach (var item in source.TakeWhile(val => shouldBuffer(val, buffer.Count)))
            {
                buffer.Enqueue(item);
            }

            int skipCount = 0;
            foreach (var item in source.SkipWhile(val => shouldBuffer(val, skipCount++)))
            {
                //Add to the buffer for inclusion
                buffer.Enqueue(item);
                runningAgg = new Aggregation<TSource, TValue>(item) { AggregatedValue = projection(runningAgg, item, buffer, droppedItem, buffer.Count) };
                droppedItem = buffer.Dequeue();

                yield return runningAgg;
            }
        }
    }
}

To claim success, I need to test the function. To play with real numbers, let’s get the history of weekly MSFT stock returns from yahoo. Let’s export this data to excel and create set of moving averages of Close price to test our data. Here’s what the spreadsheet should look like:

Moving Average in Excel

We can then create a sample dataset to represent the original closing prices. We can then test our function my creating a moving average calculation on the dataset. The code is below.

First, I create the dataaset, then I iterate through the dataset, piping the data into my aggregator function, which in turn calls me back and asks me to make 2 important decisions:

1. Should it continue buffering? (Lines 60 and 71)
2. Given a buffer provide an aggregation.(Line 61 in the first example and lines 74-82 in the second)

The output of the sample should match the 6-day Avg column in the attached spreadsheet.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MovingAverage.Linq;

namespace MovingAverage
{
    class Program
    {
        static void Main(string[] args)
        {
            var datapoints = new List<TimedDataPoint<decimal>>()
      {
          new TimedDataPoint<decimal> 
          { Date = new DateTime(2014, 1,2), Value = 36.91m},
          new TimedDataPoint<decimal> 
          { Date = new DateTime(2014, 1,6), Value = 36.04m},
          new TimedDataPoint<decimal> 
          { Date = new DateTime(2014, 1,13), Value = 36.38m},
          new TimedDataPoint<decimal> 
          { Date = new DateTime(2014, 1,21), Value = 36.81m},
          new TimedDataPoint<decimal> 
          { Date = new DateTime(2014, 1,27), Value = 37.84m},
          new TimedDataPoint<decimal> 
          { Date = new DateTime(2014, 2,3), Value = 36.56m},
          new TimedDataPoint<decimal> 
          { Date = new DateTime(2014, 2,10), Value = 37.62m},
          new TimedDataPoint<decimal> 
          { Date = new DateTime(2014, 2,18), Value = 37.98m},
          new TimedDataPoint<decimal> 
          { Date = new DateTime(2014, 2,24), Value = 38.31m},
          new TimedDataPoint<decimal> 
          { Date = new DateTime(2014, 3,3), Value = 37.90m},
          new TimedDataPoint<decimal> 
          { Date = new DateTime(2014, 3,10), Value = 37.70m},
          new TimedDataPoint<decimal> 
          { Date = new DateTime(2014, 3,17), Value = 40.16m},
          new TimedDataPoint<decimal> 
          { Date = new DateTime(2014, 3,24), Value = 40.30m},
          new TimedDataPoint<decimal> 
          { Date = new DateTime(2014, 3,31), Value = 39.87m},
          new TimedDataPoint<decimal> 
          { Date = new DateTime(2014, 4,7), Value = 39.21m},
          new TimedDataPoint<decimal> 
          { Date = new DateTime(2014, 4,14), Value = 40.01m},
          new TimedDataPoint<decimal> 
          { Date = new DateTime(2014, 4,21), Value = 39.91m},
          new TimedDataPoint<decimal> 
          { Date = new DateTime(2014, 4,28), Value = 39.69m},
          new TimedDataPoint<decimal> 
          { Date = new DateTime(2014, 5,5), Value = 39.54m},
          new TimedDataPoint<decimal> 
          { Date = new DateTime(2014, 5,12), Value = 39.83m},
      };


            foreach (var item in datapoints.MovingAggregation<TimedDataPoint<decimal>, decimal>(
                    (item, count) => count < 5,
                    (seed, item, buffer, droppedoff, count) => buffer.Average(val => val.Value)))
            {
                Console.WriteLine("Value {0} MovingAverage {1:F3}",
                                    item.DataPoint,
                                    item.AggregatedValue);
            }

            Console.WriteLine("==================================");

            foreach (var item in datapoints.MovingAggregation<TimedDataPoint<decimal>, decimal>(
                    (item, count) => count < 5,
                    (aggSoFar, item, buffer, droppedoff, count) =>
                    {
                        if (aggSoFar.AggregatedValue == 0)
                        {
                            return buffer.Average(val => val.Value);
                        }
                        else
                        {
                            return aggSoFar.AggregatedValue +
                              (item.Value - droppedoff.Value) / count;
                        }
                    }))
            {
                Console.WriteLine("Value {0} MovingAverage {1:F3}",
                                    item.DataPoint,
                                    item.AggregatedValue);
            }
        }
    }
}

I found this aggregator function very useful. Hope you will too.

Unit Testing Custom Assembly Resolution

For my project at work I had to implement custom assembly resolution in a framework that supports dynamically loaded dlls stored at various locations, relative to the main executable. As an aside, this is achieved by listening to the AssemblyResolve event on the Current AppDomain (AppDomain.Current.AssemblyResolve). The handler should figure out the file name based on the assembly name and load the file using the Assembly.LoadFrom() method.


AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(HandleAssemblyResolve);

private static Assembly HandleAssemblyResolve(object sender, ResolveEventArgs args)
{
  //look at args.Name to figure out the filename your assembly would be defined in
  // ... skipping as its business specific
  //
  return LoadAssemblyFrom(foundPath, fileName);
}

private static Assembly LoadAssemblyFrom(string foundPath, string fileName)
{
   string text = Path.Combine(foundPath, fileName);
   Trace.TraceInformation("Found file {0}", text);
   return Assembly.LoadFrom(Path.Combine(foundPath, text));
}

But how does one go about unit testing this? The problem that .NET poses here is that once an assembly loaded into an appdomain, it cannot be unloaded from it. So the only way we can test this is by creating an appdomain to host loaded assemblies during testing and then unloaded it when the test is complete.

To accomplish this, I wrote this utility method called RunInAnotherAppDomain. Notice that it does not use the Action delegate, but instead uses a special CrossAppDomainDelegate class to enable calls across App Domains.


private void RunInAnotherAppDomain(CrossAppDomainDelegate actionToRun)
{
  var dom = AppDomain.CreateDomain("test", AppDomain.CurrentDomain.Evidence, 
                 AppDomain.CurrentDomain.BaseDirectory, string.Empty, false);

  dom.DoCallBack(actionToRun);
  AppDomain.Unload(dom);
}

In order to test this, I needed an assembly to play with. The easiest thing to do is to add a reference to another assembly from the project containing your unit tests. Because of the default Visual studio behavior, your assembly would be copied into the same directory as the unit test dll itself during compilation. I had created Library project, compiled it committed it to the library directory of my project and referenced it from the unit test project. The name of the dll was Dummy.dll and, and the name of the assembly was simply “Dummy”. Because it’s not actually used by the code directly (no code from dummy is actually being used, the assembly will not be loaded automatically by the process, which leaves open for me to play with.

The first test I would want to write would expect the assembly to be loaded normally, just by asking right out of the box.


[Test]
public void ShouldLoadAssembly()
{
  RunInAnotherAppDomain(() => 
  {
     //This method uses default .NET assembly resolution rules to load
     var assembly = Assembly.Load("Dummy");  
     Assert.IsNotNull(assembly);
  }
}

In the next test, I would move the assembly DLL to another location and will attempt to run same test and would expect it to fail:

[Test, ExpectException(ExpectedException = typeof(FileNotFoundException))]
public void ShouldFailLoadAssembly()
{
  MoveDummyAssemblyAway(); 
  try
  {
    ShouldLoadAssembly();
  }
  finally
  { 
    ReturnDummyAssemblyBack();
  } 
}
 
private static void MoveDummyAssemblyAway()
{ 
   if(!Directory.Exists("test"))
   { 
       Directory.CreateDirectory("test");
   }

   File.Copy("Dummy.dll" "test\\Dummy.dll", true);
   File.Delete("Dummy.dll");
}

private static void ReturnDummyAssemblyBack()
{
  if(Directory.Exists("test"))
  {  
      File.Copy("test\\Dummy.dll", "Dummy.dll", true);
      File.Delete("test\\Dummy.dll");
      Directory.Delete("test");
  }
}

I am now ready to test my custom assembly resolution logic.


[Test]
public void ShouldLoadAssemblyWithCustomResolution()
{  
  MoveDummyAssemblyAway(); 
  try
  {
    RunInAnotherAppDomain(() =>
    {
       //This sets up resolution for the current AppDomain.  
       //This lambda runs in the "test" appdomain, so it'll work
       MyCustomAssemblyResolver.Init();
       
       var assembly = Assembly.Load("Dummy");  
       Assert.IsNotNull(assembly); 
    }
  }
  finally
  { 
    ReturnDummyAssemblyBack();
  } 

If my custom resolution logic inside MyCustomAssemblyResolver class works, the test should pass.