Archive for July, 2009

Linq to SQL is not dead meat

Tuesday, July 21st, 2009

My old WP got hacked, and this post got deleted. Basically, all I said can be founf from Jouni’s post.

Azure is cheap, Google is cheaper – or is it?

Thursday, July 16th, 2009

This post also got deleted. Basically, it was an announcement that there already numerous comparison between different cloud vendors and their prices, like here and here.

Mysterious experiences in the world of reflection using TypeDescriptor

Monday, July 13th, 2009

In the process of implementing server-side validation for an ASP.NET MVC project using great xVal library, I found an interesting..err.. dare I say bug in the .NET framework. I used xVal and dataannotations on Linq2SQL objects, pretty much following the guidance here. Of course, I wanted the error messages localized and luckily, the ValidationAttributes have properties called ErrorMessageResourceName and ErrorMessageResourceType so it shouldn’t be a problem.

Following eXtreme Programming principles, I wanted first to have it work doing the simplest possible solution. Just add a plain required attribute and test. Voilá, it works! Great, so let’s add the localization in place. I added the attribute properties and suddenly the validation didn’t work anymore. Why is that? After close examination with debugger and Reflector I found that:

  • According to reflector the attributes are there and their properties are correctly set
  • According to VS debuggerTypeDescriptor.GetProperties(myModelObject).Cast().ToList()[0].Attributes doesn’t contain the RequiredAttribute!

That’s interesting. No exceptions were raised, no error messages were given. The attribute was plain missing and thus validation didn’t work.

OK, let’s try it another way. Inspect the object using ordinary reflection methods using the following code:

foreach (var pi in instance.GetType().GetProperties())
 Console.WriteLine(“Propertyname: {0}\r\n “, pi.Name);
 foreach (var a in pi.GetCustomAttributes(true))
  Console.WriteLine(” “ + a.GetType().ToString());

This code raised runtime exception “Unhandled Exception: System.Reflection.CustomAttributeFormatException: ‘ErrorMessageResourceName’ property specified was not found.” Whaaat!? I just checked that it is there. Reading the stacktrace more closely, I found ystem.InvalidOperationException: The resource type ‘MetadataTest.ModelValidationResource’ does not have a publicly visible static property named ‘XXX’.

Ok, so the resource should be public? Indeed, resources are generated as internal by default. Luckily the resource editor allows me to set the access modifier as public. Let’s retest. Now the new code goes through correctly. How about the code using TypeDescriptor? Now it works too! So, a few things to remember when using localized model validation

  • Remember to set resources as public
  • Do not trust TypeDescriptor as it silently sucks all exceptions and errors

I guess I need to ask Microsoft whether this is by design. Basically this kind of errors should never be ignored.