Using extension methods to handle Xml

by Mikael Henriksson 16. February 2010 08:42

I have created a few very useful extension methods for handling the System.Xml.Linq namespace that I use all the time. I’ve had positive feedback on this before (and it showed up in this post) and now I had to create a couple of extra extensions just to make my life even easier. The way I work with these extensions is when I don’t care to handle null’s and just want to return the value of an element or an attribute who’s name I know.  If the value is an empty string you can bet I have a test that catches those scenarios. Like let’s say I want to read a configuration xml that I have created myself before hand. Or someone else’s xml that is very unlikely to change without my knowledge. I will put all this code up on git hub.

 

To show what I mean I’ll start with what I used to be doing.

var value = enumerableElements.Single(x => x.Name == "foo").Attribute("bar").Value;

With a bunch of the above statements in your code it becomes completely impossible to understand what the hell I was thinking / not thinking when I wrote the code. I write code for myself mostly and I am extremely picky with my code for that specific reason. What is really going on in Linq to Xml is like in most of the things that ships with .NET. It’s a very generic framework that you can build on top of and That’s the whole fun of it all. :)

So what we want to do above is to get the attribute value easily and we don’t care about checking for nulls, it should return the default value if we can’t find the attribute. Let’s start with from the end with getting the attribute value. That could (and will) look like:

public static string GetAttributeValue(this XElement element, string attributeName)
{
	XAttribute attribute = element.Attribute(attributeName);

	return attribute != null ? attribute.Value : string.Empty;
}

The above is the core, it should never throw any exceptions unless the situation of course is exceptional but in that case I don’t mind so much. The next part is debatable if it should be done but I do it anyway.

 

public static string GetAttributeValueFromElement(this IEnumerable<XElement> elements,
												  string elementName, string attributeName)
{
	var element = elements
		.Descendants(elementName)
		.FirstOrDefault(x => x.Name == elementName);

	return element.GetAttributeValue(attributeName);
}

Great stuff I just made my own life a bit easier but I believe I can take it one step further without breaking too many things. What if we easily could get rid of needed conversions after retrieving the string value and skip to getting the converted value? Imagine this code, it’s pretty common:

foo.ValidatedAt = DateTime.Parse(validation.GetElementValue("timestamp"));

We can semi-easily simply GetElementValue even more using generics. The above can be shortened to:

public static T GetAttributeValue<T>(this XElement element, string attributeName)
{
	XAttribute attribute = element.Attribute(attributeName);

	return attribute != null 
		? Converts.To<T>(attribute.Value) 
		: Converts.To<T>(string.Empty);
}

Now the call to GetAttributeValue would be shortened to.

foo.ValidatedAt = validation.GetElementValue<DateTime>("timestamp");

Hiding behind Converts.To is what I found in a comment from Tuna Tuksoz in this blog post. I’ll be honest with you I haven’t used the Type Descriptor before so I am not going to make a fool out of myself explaining it in detail but obviously it holds information about types and registers / gets converters for the types it knows about.

public static class Converts
{
	public static T To<T>(object value)
	{
		return (T)To(typeof(T), value);
	}

	public static object To(Type t, object value)
	{
		TypeConverter tc = TypeDescriptor.GetConverter(t);
		return tc.ConvertFrom(value);
	}

	public static void RegisterTypeConverter<T, TC>() where TC : TypeConverter
	{
		TypeDescriptor.AddAttributes(typeof(T), new TypeConverterAttribute(typeof(TC)));
	}
}

I would have named my class Convert but then I would have to use System.Convert in all other places. Maybe I should just switch to this generic one instead :)

Tags: , ,

C# | LINQ | Xml

Copying a specific subfolder of a parent folder

by Mikael Henriksson 14. February 2010 01:23

I have a service that sends our application to customers based on various criteria's. I want to verify the most important parameters and to do that I of course need to copy the whole darn folder structure to the directory the tests are run from to simulate the web client.

To please both Team City and the R# test runner I had to create something of my own and I’d rather not lock that down to visual studio nor NAnt. I found this great class on MSDN that seems to do the trick.

 

public class CopyDir
{
	public static void Copy(string sourceDirectory, string targetDirectory)
	{
		var diSource = new DirectoryInfo(sourceDirectory);
		var diTarget = new DirectoryInfo(targetDirectory);

		CopyAll(diSource, diTarget);
	}

	public static void CopyAll(DirectoryInfo source, DirectoryInfo target)
	{
		if (source.Name.Contains(".svn"))
			return;
	   
		// Check if the target directory exists, if not, create it.
		if (Directory.Exists(target.FullName) == false)
		{
			Directory.CreateDirectory(target.FullName);
		}

		// Copy each file into it's new directory.
		foreach (FileInfo fi in source.GetFiles())
		{
			Console.WriteLine(@"Copying {0}\{1}", target.FullName, fi.Name);
			fi.CopyTo(Path.Combine(target.ToString(), fi.Name), true);
		}

		// Copy each subdirectory using recursion.
		foreach (DirectoryInfo diSourceSubDir in source.GetDirectories())
		{
			DirectoryInfo nextTargetSubDir = target.CreateSubdirectory(diSourceSubDir.Name);
			CopyAll(diSourceSubDir, nextTargetSubDir);
		}
	}
}

All I did was add a filter for “.svn” folders that I really don’t want to keep. I do want to delete the whole thing after the tests are run. I also added a method for recursively navigating the parents to find the desired folder.

public DirectoryInfo RecursivelySearchParents(DirectoryInfo startDir, string folder)
{
	var resultDir = startDir.GetDirectories().FirstOrDefault(x => x.Name == "sw");
	if (resultDir != null)
		if (resultDir.Exists)
			return resultDir;
			
	return RecursivelySearchParents(startDir.Parent, folder);
}

And then the SetUpFixture with it’s SetUp and TearDown attributes.

[SetUpFixture]
public class TestSetUp
{
	public static string BinaryTargetDir = 
		string.Intern(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "sw"));
	public static string SourceDirectory;

	[SetUp]
	public void Setup()
	{
		var dir = new DirectoryInfo(Directory.GetCurrentDirectory())
		SourceDirectory = CopyDir.RecursivelySearchParents(dir, "sw").FullName;
		CopyDir.Copy(SourceDirectory, BinaryTargetDir);
	}
	
	[TearDown]
	public void TearDown()
	{
		var dir = new DirectoryInfo(BinaryTargetDir);
		dir.Delete(true);
	}
}

Tags:

C#

How should I use AutoMapper?

by Mikael Henriksson 13. February 2010 16:12

It is actually really simple and I have to thank my boss, DBA and friend Fridthjof for that. If you ever thought it is difficult to work with a DBA then imagine having one as your boss! *joke*  

He’s a great guy and as a DBA he is actually very understanding and I have been able to use pretty much any tool I want to get the job done as long as I don’t use CamelCase names in the database. Since we are using whatever tools we feel might get us there the quickest/easiest we have gone down different paths where I have spent a bit more time setting things up with NHibernate whereas my colleague has chosen to move quicker initially with LINQ to SQL  and use Fridthjof to do the queries in form of stored procedures for him. It’s worked out great for both of us though for me it took a while longer but yesterday when we where going to synchronize  our works I found that in the other projects he ran in to having to do all the work two times or so because of the different naming standards in the database and C#. I instantly came to think of AutoMapper so I suggested it, helped him set it up for what I needed from him (took a couple of minutes until it was deployed) and I am pretty sure that I will have his eternal gratitude for showing him AutoMapper. Question is how do I setup AutoMapper? Example below:

 

[Test]
public void AutoMapper_configuration_should_be_valid()
{
	Mapper.CreateMap<ContactQuery, ContactInfo>()
		.ForMember(dest => dest.SomeProperty, opt => opt.Ignore());

	Mapper.CreateMap<ContactInfo, ContactQuery>()
		.ForMember(dest => dest.FirstVersionAt, opt => opt.Ignore())
		.ForMember(dest => dest.LastVersionAt, opt => opt.Ignore())
		.ForMember(dest => dest.MaxResult, opt => opt.Ignore());
	
	Mapper.AssertConfigurationIsValid();
}

 

You can of course do much more than this with AutoMapper but it’s a good start the above is a test verifying that the maps I just setup is valid. don’t worry. AutoMapper will tell you exactly what your mapping problems are. This type of mapping should of course not be confused with NHibernate mappings but they are basically the same thing between two objects instead of between one object and one table.

So how do I use this map then?

var contact = new ContactInfo { ContactId = 1 };
var query = Mapper.Map<ContactInfo, ContactQuery>(contact);

So far so good BUT! This is doing the same thing / writing the same code in two places and you should really not repeat yourself. Time to DRY up and place that in a Profile of the project using it. (I know the documentation is blank =:])  That way it’s really easy to add a test for it and reset the test afterwards so that we at least keep the Assertions to just validating the bare minimum.

public class ContactProfile : Profile
{
	protected override void Configure()
	{
		CreateMap<ContactInfo, ContactQuery>()
			.ForMember(dest => dest.FirstVersionAt, opt => opt.Ignore())
			.ForMember(dest => dest.LastVersionAt, opt => opt.Ignore())
			.ForMember(dest => dest.MaxResult, opt => opt.Ignore());

		CreateMap<ContactQuery, ContactInfo>()
			.ForMember(dest => dest.SomeProperty, opt => opt.Ignore());
	}

	protected override string ProfileName
	{
		get { return "Sync"; }
	}
}

Now then a test to prove that my mappings are actually valid. I like to clear everything both before the test and after the test to be sure I only validate the necessary mappings:

[TestFixture]
public class Verify_AutoMapper_mappings_for
{
	[Test]
	public void ContactProfile()
	{
		Mapper.Reset();
		Mapper.AddProfile<ContactProfile>();
		Mapper.AssertConfigurationIsValid();
		Mapper.Reset();
	}
}

Tags: , ,

C# | Testing | AutoMapper

Conquering NServiceBus part 2 – Initial configuration

by Mikael Henriksson 9. February 2010 22:14

This post is part of a series:

  1. Conquering NServiceBus part 1 – Getting Started
  2. Conquering NServiceBus part 2 – Initial configuration
  3. Conquering NServiceBus part 3 – A simple Saga
  4. Conquering NServiceBus part 4 – Testing
  5. Conquering NServiceBus part 5 – Troubleshooting DTC

Obviously before starting with sagas and the more advanced things NSB has to offer I need to create some sort of simple way of sending and receiving messages. I’ll start with the receiving end because that is the easiest one. There are some things needed here. I plan on using the NServiceBus.Host.exe for simplicity and before we initialize the bus we need a couple of queues for the messages (SQLite can be used as well). Since I am just testing things out I won’t be very thorough with the config file. A really simple config for the receiving endpoint could look like:

<configSections>
	<section name="MsmqTransportConfig" 
			 type="NServiceBus.Config.MsmqTransportConfig, NServiceBus.Core" />
	<section name="UnicastBusConfig" 
			 type="NServiceBus.Config.UnicastBusConfig, NServiceBus.Core" />
</configSections>

<MsmqTransportConfig
	InputQueue="ReceivingEndpointQueue"
	ErrorQueue="Error"
	NumberOfWorkerThreads="1"
	MaxRetries="5" />

<UnicastBusConfig>
	<MessageEndpointMappings>
		<!-- This is the receiving endpoint and it does not need any mappings yet -->
	</MessageEndpointMappings>
</UnicastBusConfig>

This is where I had some minor problems understanding things so I’ll go through them briefly. In the sample projects that come with NServiceBus it is named things like “SomeInputQueue” and if you haven’t had the NSB or SOA course with Udi it’s maybe not that easy to follow (or maybe it was just me). The queue that is specified in the MsmqTransportConfig is the queue this endpoint is assigned so any messages sent to a MessageHandler in this project will be sent to this message queue.  Then of course the next step is to connect the sender to this queue somehow and in the config file this is done like follows:

<configSections>
	<section name="MsmqTransportConfig" 
			 type="NServiceBus.Config.MsmqTransportConfig, NServiceBus.Core" />
	<section name="UnicastBusConfig" 
			 type="NServiceBus.Config.UnicastBusConfig, NServiceBus.Core" />
</configSections>

<MsmqTransportConfig
	InputQueue="SendingEndpointQueue"
	ErrorQueue="Error"
	NumberOfWorkerThreads="1"
	MaxRetries="5" />

<UnicastBusConfig>
	<MessageEndpointMappings>
		<add Messages="MyProject.Messages" 
		     Endpoint="ReceivingEndpointQueue" />
	</MessageEndpointMappings>
</UnicastBusConfig>

I hope it makes more sense now so on to the MessageEndpointMappings. There are a few ways to configure this. You can for instance configure a whole assembly like above. NServiceBus will search for all assemblies that contains ”MyProject.Messages” in the assembly name so there is not really any need to be more specific than that. If you on the other hand like in the saga scenario want to configure just one specific message to an endpoint you can do that easily with a fully (assembly) qualified name.

<add Messages="NServiceBus.Saga.TimeoutMessage, NServiceBus"
     Endpoint="TimeoutManager"/>

Next up is the initialization of the endpoint and there is a lot going on here. First of all I want full control over what is going on. Not because I don’t trust the developers on the NSB project but like Jeremy stated : “Don’t copy code you don’t understand”. I am not copying code here but I refuse to use code without fully understanding what it does if I am to use it in production eventually and this means dissecting NServiceBus and learning the darn thing! It all comes down to something extremely simple really. I can shorten everything to the following:

public class ReceivingEndpointConfig : 
	IConfigureThisEndpoint, 
	AsA_Server   { }

When the host starts it will look for any class that implements the interface IConfigureThisEndpoint and AsA_Server tells the host that it should use MsmqTransport transactionally, XmlSerialization, load all message handlers and it will NOT “purge” (clear) the queues at startup and I might be forgetting something important. Yes!! NServiceBus ships with a bunch of IoC containers. The default one is the spring container so this will be used for inversion of control. I don’t care much about Spring so I’ll switch this to StructureMap later. With the simple code above I can create an equally simple handler like below.

public class MyMessageHandler : IMessageHandler<OneMessage>
{
	public void Handle(OneMessage message)
	{
		//Do something with arrived message
	}
}

As soon as a message of type OneMessage is sent to the queue ReceivingEndpointQueue the message will end up in the above method all your message class needs to do is implement IMessage.

public class OneMessage : IMessage
{
	public string Title { get; set; }
}

That’s the receiving end. What about the sender? For this silly tutorial I will be using the web. In my website I am (for now) happy with a global static IBus that can send necessary OneMessage messages when needed. When the application starts up I make sure to start up a bus and then store it in Global.asax.

public static IBus Bus { get; private set; }

protected void Application_Start(object sender, EventArgs e)
{
	Bus = Configure.WithWeb()
		.Log4Net()
		.SpringBuilder()
		.XmlSerializer()
		.MsmqTransport()
			.IsTransactional(true)
		.UnicastBus()
			.LoadMessageHandlers()
		.CreateBus()
		.Start();
}

A few new things here, earlier I discussed how AsA_Server did most of this configuration for us. Right now I am not using a server, nor a publisher. What I want in the web application is a bus to send messages to other endpoints. Later on we will probably have some sort of response (error code) back to the web server. It is extremely important to send error codes between the endpoints. When a message has been received and as soon as possible a simple integer should be returned. This is sometimes the only way to confirm that the message was actually received and it is a dead easy way of setting up your tests (more about testing in a later). Something more is new and that’s the Log4Net. Let’s add that to the configuration.  Since most people seem to be using log4net I leave that config out. We all have different preferences but your app.config or web.config should look a bit like:

<configSections>
	<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
</configSections>

<log4net configSource="log4net.config" />

The previous entries are still needed so don’t remove it! Now I should have a pretty much working example at least on my machine. works-on-my-machine-starburst

 

 

 

 

 

Now I can send messages from the web to my pages.

Global.Bus.Send(new OneMessage { Title = "This is a title" });

Tags:

NServiceBus

Conquering NServiceBus part 1 – Getting Started

by Mikael Henriksson 8. February 2010 21:37

This post is part of a series:

  1. Conquering NServiceBus part 1 – Getting Started
  2. Conquering NServiceBus part 2 – Initial configuration
  3. Conquering NServiceBus part 3 – A simple Saga
  4. Conquering NServiceBus part 4 – Testing
  5. Conquering NServiceBus part 5 – Troubleshooting DTC

Before we get started with coding there are a whole new language with terminology to learn so let’s start there.

  • Enterprise Service Bushttp://en.wikipedia.org/wiki/Enterprise_service_bus
  • NServiceBus  – What you use to build an ESB
  • MSMQ – Microsoft Message Queuing used by NServiceBus as transport system
  • (IoC) Container – used to inject dependencies when needed. You get to chose your favorite one to work with (mine is StructureMap)
  • Unicast Bus – The messaging system that NServiceBus ships with. (Currently the only one that works to my knowledge).
  • Serializationhttp://en.wikipedia.org/wiki/Serialization  XmlSerialization based on interfaces preferred for performance. NServiceBus ships with BinarySerialization and XmlSerialization. You can also easily create your own Serializer and use.
  • Subscription – NServiceBus makes publish/subscribe extremely easy. You can tell your message handler to subscribe to a certain type of message.
  • Message Handler – The actual handler of a given message (implementation detail goes inside the handle method)
  • Endpoint Config – The programmatic way of initializing a “bus”.
  • Saga – Long running process with the need for temporary storage before the final result can be processed.
  • Profile – When installing your service you can explicitly tell it to handle only a certain profile which is good if you have multiple environments and want different settings on different machines.

Let’s start from the beginning. I know from start I have a long running process where the client will be generating a lot of hits on a WCF REST service. The exchange will not be data heavy but it will be many requests at a time. That’s why I want to use NServiceBus and the Saga functionality. I also want to invalidate the saga after a given time, something that is supported by the Saga. When the saga completes I want to do expensive (time consuming) work and there for I don’t want the saga to do this which means I need to pass the whole thing on to another sort of service with main purpose of doing this heavy work. That means I can focus on threading and what not in one place only should I ever need to and it will also be extremely easy to measure performance of the various parts of the system and at the same time it will allow me to scale out pretty easily.

Question is, where do I start? Some of you might consider starting with writing tests. Myself I have no idea what kind of tests to write yet or how the application should be created. All I can do now is to try and get the high-level parts of me brain to cooperate with the low-level parts.

Tags:

NServiceBus

Collecting all constant values of a class using reflection

by Mikael Henriksson 6. February 2010 16:43

I sometimes have to test outgoing messages before we send them out. Since we work in the mobile industry we need to communicate with mobile phones and alas most countries have legal requirements that we much abide to. Because of some Swedish and Norwegian characters like “åäö” message length etc varies. So before we change any messages in production everyone get’s to have a say on the message. To do this I have created a simple unit test where I send the SMS to everyone. though there are quite a few places where this is done and I got bored with copy pasting the numbers or using them one and one to concatenate them so I decided to use reflection for this instead.

 

public class Numbers
{
	public const string EmployeeOne = "xxxxxxxxxx";
	public const string EmployeeTwo = "xxxxxxxxxx";
	public const string EmployeeThree = "xxxxxxxxxx";
	public const string EmployeeFour = "xxxxxxxxxx";
	public const string EmployeeFive = "xxxxxxxxxx";
	public const string EmployeeSix = "xxxxxxxxxx";
	public const string EmployeeSeven = "xxxxxxxxxx";
	public const string EmployeeEight = "xxxxxxxxxx";
	public const string EmployeeNine = "xxxxxxxxxx";
	public const string EmployeeTen = "xxxxxxxxxx";
	public const string EmployeeEleven = "xxxxxxxxxx";
	public const string EmployeeTwelve = "xxxxxxxxxx";
}

 

Wow a class of constants… how beautiful! ;) Well they have their place and this is the place for a class of constants.

Instead of manually handling all the numbers when I need them all is to make good use of reflection. Most reflection starts with a type somehow and in this case we are looking for fields that are public, static and flatten hierarchy means that inherited classes protected and public constants should be searched as well.

We want the Literal (compiled values) and they should not be init (initialized using constructor only).

private const BindingFlags Flags = BindingFlags.Public | 
            BindingFlags.Static | BindingFlags.FlattenHierarchy;

public IEnumerable<string> GetAllNumbers(Type type)
{
	FieldInfo[] fieldInfos = type.GetFields(Flags);
	return fieldInfos.Where(fi => fi.IsLiteral && !fi.IsInitOnly)
		.Select(fi => fi.GetValue(null).ToString());
}

If I wanted to create a tool / library around this I would probably wrap the above method with some generic.

public IEnumerable<string> GetAllNumbers<T>()
{
	return GetAllNumbers(typeof (T));
}

Tags: ,

C# | Reflection

Logging WCF messages generically

by Mikael Henriksson 6. February 2010 15:43

I recently felt the need to log incoming and outgoing messages from WCF without cluttering my operation contract implementations. I believe that this type of “debug” logging in the early stages of a project should if possible be done generically and externally. First of all I started out with following Andreas Öhlund’s post about how to setup WCFusing StructureMap. With this as a ground to stand on it wasn’t too much problem adding the logging part. To begin with I have a “MessageBase” that all my messages inherit from. Any message that inherits from this class does automatically use override ToString() from it’s base class. ToString just used the Serialize method to get a valid xml document. I have a certain logger for this so that I can turn it on and off easily for debugging purposes. Since this is the entry point towards the clients and this communication cant be durable (more info here)  though the rest of the backend is this is a great resource for tracking down potential future problems and something I need myself during integration tests. Anyway enough of the small talk. Let’s get down to business.

 

public interface IMessageBase<T> where T : class
{
    String Serialize(T data);
    T Deserialize(string text);
}

public abstract class MessageBase<T> : IMessageBase<T> 
	where T : class
{
	public String Serialize(T data)
	{
		using (var stringWriter = new StringWriter())
		{
			var settings = new XmlWriterSettings
						   {
							   Encoding = Encoding.UTF8,
							   OmitXmlDeclaration = true
						   };
			using 
			(var writer = XmlWriter.Create(stringWriter, settings))
			{
				var xmlSerializer = new XmlSerializer(typeof (T));
				xmlSerializer.Serialize(writer, data);
			}

			return stringWriter.ToString();
		}
	}

	public T Deserialize(string text)
	{
		T data;

		var settings = new XmlReaderSettings
						   {
							   IgnoreWhitespace = true
						   };

		using (var reader = XmlReader.Create(
			new MemoryStream(
				Encoding.UTF8.GetBytes(text)), 
				settings))
		{
			var xmlSerializer = new XmlSerializer(typeof (T));
			data = (T) xmlSerializer.Deserialize(reader);
		}

		return data;
	}

	public override string ToString()
	{
		return Serialize(this as T);
	}
}

 

Since I already have a UnitOfWorkMessageInspector (thank you Andreas) adding another one for the logging is not a hard thing to do.

Let’s start with the MessageInspector, just like with the UoW one Andreas created we implement the interface IDispatchMessageInspector.

public class LoggingMessageInspector : IDispatchMessageInspector
{
	public object AfterReceiveRequest(ref Message request,
		IClientChannel channel,
		InstanceContext instanceContext)
	{
		LogMessage(ref request);

		return null;
	}

	public void BeforeSendReply(ref Message reply, 
		object correlationState)
	{
		LogMessage(ref reply);
	}

	private void LogMessage(ref Message message)
	{
		LogManager.GetLogger("Messages").Debug(message.ToString());
	}
}

Dead easy right? I have two concerns now:

  1. I have no idea why message.ToString() gives me the actual xml string because Message is a WCF class. I suppose it wraps my MessageBase implementation and use that ToString() I actually didn’t check this yet.
  2. This works very well for messages BUT I have a few scenarios when the message is a Stream and in these cases only …. Stream …. is logged to the text file.

To wire this up in StructureMap is not a big problem. What we need to do now is to replace “Use” (which sets the default instance to use) to “Add“ which adds instances to use in the order they are added to StructureMap.

For<IDispatchMessageInspector>()
	.AddInstances(x =>
			{
				x.Type(typeof (LoggingMessageInspector));
				x.Type(typeof (UnitOfWorkMessageInspector));
			});

It’s not complete yet as I mentioned I need to figure out how to log the stream parameter.

Tags:

WCF