Mysterious experiences in the world of reflection using TypeDescriptor
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.
Software development philosophy and agile practises
This post also got deleted in hacker attack. It was mainly about my talk in a Nice tuesday event.
Documentation is not just another phase
Documentation is often considered a distinct phase of software development. Very few admits enjoying documentation. It’s become the dark side of development which is most often tried to be left undone. The excuses are many, misunderstood agile software development processes being one the most recent. “We use Extreme Programming – we don’t make any documents!”
If documentation is kept as a separate phase, performed after coding, we will face similar problems as we do if testing is postponed until coding is finished:
- When we run out of time, the phase in question won’t get done
- The phase will be an enormous effort and unpleasant as such, we are coders after all. Documents are easier to write in smaller parts. For example, writing down what you have accomplished this day isn’t such a big deal. It’s easier to write when you still remember what you were thinking.
- The task is easily given to someone else. It’s most efficient when the same person wo wrote the code, also writes the documentation.
- The outcome of the phase are of no use to the previous phase: if documents are written/tests done along with coding, it will be profitable immediately. You’ll think about your code from a different point of view and you may even find a major flaw in your design!
I hear often “I’ll document the code when it’s finished”. But as often the code will get taken to production before it’s ready. And at that point there’s no documentation.
So, always document your code as you make progress!
Estimating and Planning
Christmas is wonderful time for reading. You don’t have anything more meaningful to do anyway
Mike Cohn’s Agile Estimating and Planning happened to be on top of my reading stack. What a wonderful book it is! It stroke right into my thinking about conscious development. He provided excellent advice on estimating projects, prioritizing features, planning projects, planning iterations, calculating benefits, communicating progress, scheduling etc.
Most often these things are either done hastily or not at all. But you really have to do them, and do them consciously.
Most projects contain a tremendous amount of uncertainty. This uncertainty is often not fully reflected in the schedules and deadlines that project teams create. There are times when this uncertainty is so large or significant that extra steps should be taken when estimating the duration of a project.
The bottomline is this:
- You have to be able to justify any user story / project
- Do the planning, make the plans visible, and change them as needed
- Admit the uncertainty there is and plan for it
- Make wise estimates don’t just have a guess
Highly recommended to everyone! Really, I can’t say is this book for developers or managers. It gives excellent advice for project managers and project owners on prioritizing features, on the other hand, it says a lot about estimating the size of a user story – an activity best done by a developer.
It matches my thinking: there should be no project managers, developers and customer – there should be unified teams. And this book is excellent reading for a team!
Telerik r.a.d controls Q4 2006 is out!
Telerik just released Q4 of r.a.d controls. Have a look at them!
Rebirth
Time to make a facelift. Both technical and philosophical. Before, I tried to make myself as much as possible. Also, I thought I could write about everything in the world. This just doesn’t make sense.
Why not use one of those great blog engines out there? Why not use one readymade theme that those guys – who really know how to do the job – have done? Content is more important than representation.
In this newly born blog I will write about things that are closely related to software development. My point of view will be a bit philosophical, as I’m currently quite a bit interested in different software methodologies. In addition, Telerik will get some air as I’m one of their MVPs.
Finally, why “Conscious Development”? That’s a different story.