Oren always amazes me - I think he is psychic! Whenever I have a problem, I almost always find Oren blogged about it a few days before - and today's bugbear is a perfect example.
I started the day off with a really weird timeout exception in WCF. The code was just fine apparently, and I spent a very long time debugging code I was pretty sure was working. The error (in typical framework style) was not able to be caught, debugged, or otherwise detected - it just "happened" somewhere in the WCF call.
To cut a long story short ... it turned out an attribute was missing on a DTO class, and instead of WCF giving me a meaningful error, it just gave me a worthless message. One addition of [DataContract] later and all was working fine again.
So, it just happens that Oren (in association with Glenn) had done something similar, and so taking a leaf out of their book, here is a really basic unit test to check that the DTO classes have the right attributes. Some discussion on Oren's blog discussed whether unit tests were the right place for this kind of checking (as it is pretty much static code analysis), and in some ways I can see their point - however, frankly, a unit test is just as valid a way of testing the same thing, fits nicely into CI, and is in a common format to all the other checks.
[Test]
public void Ensure_DTO_classes_have_WCF_attributes()
{
foreach (var type in typeof(ClassInDTOAssembly).Assembly.GetTypes())
{
if (type.IsInterface || type.IsAbstract)
continue;
if (type.Namespace.StartsWith("Interfaces.DTO"))
{
var attributes = type.GetCustomAttributes(
typeof (DataContractAttribute), true);
if (attributes.Length == 0)
Assert.Fail("Unable to identify [DataContract] attribute on type "
+ type.Name + " in DTO namespace");
else
foreach (var memberInfo in type.GetMembers())
{
if (memberInfo != null)
{
var memberAttributes =
memberInfo.GetCustomAttributes(typeof (DataMemberAttribute), true);
if (memberAttributes.Length == 0)
Assert.Fail("Unable to identify [DataMember] attribute on member "
+ memberInfo.Name + " on type "
+ type.Name + " in DTO namespace");
}
}
}
}
}
Posted
05-06-2008 9:38 AM
by
Jak Charlton