Click here to Skip to main content
15,881,803 members
Articles / Programming Languages / C#

Frictionless WCF service consumption in Silverlight - Part 3: Benefits of transparent asynchrony with respect to unit testing

Rate me:
Please Sign up or sign in to vote.
4.78/5 (5 votes)
26 May 2011MIT15 min read 25.1K   259   11  
Simplifying unit testing of View Models which use asynchronous WCF service calls.
#region Author

//// Yevhen Bobrov, http://blog.xtalion.com 

#endregion

using System;
using System.Collections.Generic;
using System.Reflection;
using System.Reflection.Emit;

namespace Sample.Silverlight.WCF.Infrastructure.Services
{
	public class AsyncServiceInterfaceFactory
	{
		static readonly Lazy<AsyncServiceInterfaceFactory> singleton =
				new Lazy<AsyncServiceInterfaceFactory>(() => new AsyncServiceInterfaceFactory(), true);

		public static AsyncServiceInterfaceFactory Instance
		{
			get { return singleton.Value; }
		}

		readonly ModuleBuilder module;
		readonly Dictionary<Type, Type> generated = new Dictionary<Type, Type>();

		public AsyncServiceInterfaceFactory()
		{
			var assemblyName = new AssemblyName("Silverlight.WCF.AutoGeneratedAsyncInterfaces");

			AppDomain appDomain = AppDomain.CurrentDomain;
			AssemblyBuilder assemblyBuilder = appDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);

			module = assemblyBuilder.DefineDynamicModule("Silverlight.WCF.AutoGeneratedAsyncInterfaces.dll");
		}

		public Type Generate(Type syncInterface)
		{
			lock (generated)
			{
				if (!generated.ContainsKey(syncInterface))
					generated[syncInterface] = GenerateAsyncInterface(syncInterface);

				return generated[syncInterface];
			}
		}

		Type GenerateAsyncInterface(Type syncInterface)
		{
			var generator = new AsyncServiceInterfaceGenerator(module, syncInterface);
			return generator.Generate();
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The MIT License


Written By
Software Developer http://blog.xtalion.com
Ukraine Ukraine
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions