Archive for July, 2010
Overriding ToString() on your objects using reflection
4Just a very quick one, more as a reminder to myself on something I’d setup, though as per, would love any feedback.
On a current project we’re using exception driven development, and upon an exception, we’re throwing our own custom business exceptions, and building up the Exception.Data collection with the properties in the objects as they were at the point of exception.
We have a lot of DTO objects down at the dal layer, some with a lot of properties, and we didn’t want to keep having to do:
UserNotFoundException ex = new UserNotFoundException(); ex.Data.Add(“Username”, userDto.Username); ex.Data.Add(“UserId”, userDto.UserId); ex.Data.Add(“AccountStatus”, userDto.AccountStatus); ...
With that in mind, I started working on what reflection could bring to the table and perhaps giving our Dto objects a base type to derive from.
Here’s the initial stab at what I’ve arrived at:
public class BaseDto{
public override string ToString() {
PropertyInfo[] propertyInfos = this.GetType().GetProperties();
Array.Sort(propertyInfos, (propertyInfo1, propertyInfo2) => propertyInfo1.Name.CompareTo(propertyInfo2.Name));
StringBuilder output = new StringBuilder();
foreach (PropertyInfo propertyInfo in propertyInfos)
{
output.AppendFormat("{0}: {1}\n", propertyInfo.Name, propertyInfo.GetValue(this, null)); }
return output.ToString();
}
}
Jobs a good ‘un. I can now just use:
UserNotFoundException ex = new UserNotFoundException(); ex.Data.Add(“userDto”, userDto.ToString());
and all properties will be enumerated and documented in that one property of the .Data dictionary.
<meerkat>simples!</meerkat>
I hasten to add, this seems to work a treat in testing – I’ve got more rigorous investigation to do to make sure it presents a way forward for us, but thought I’d post it anyway in case anyone found it useful.
Chrome – are you sanitising my inputs without my permission?
6I had to write this as I’m going mad, and I can’t really work out if it’s me, or if Chrome is indeed utterly fecking with my inputs.
I’m creating a form that takes (as a hidden variable) a string like this:
Double pipes at start and end are put there by me to denote where the carriage returns occur. In particular, you can see there is a carriage return after the last character.
Browsers that work
When I render this out in a hidden field in firefox (or indeed any browser other than chrome), I get the following when viewing source:
Notice in particular that the form field ends with the correct carriage returns.
When posting this to the third party provider (this is a 3D Secure transaction, letters have been changed to protect the wealthy!), jobs a good un, works no problem at all.
What happens in Chrome
When I view the same source in Google Chrome (5.0.375.99), I get the following:
Erm… Chrome – where did you put those carriage returns?
I’ve tried deliberately placing carriage returns on the hidden field, adding them to the variable, etc. and still, it removes them.
It’s almost like the value has had a .Trim() applied before being output?
This transaction fails (oddly enough, invalid paReq), and although I can’t prove it, my guess is that the carriage returns are significant in this.
Help!
Am I going mad here? Am I missing something obvious? Is this a bug or indeed a feature?
Update
This has now been confirmed by a few people – terrifying though that is. If whitespace is important to your form inputs (well, trailing whitespace), then the cry is ‘be careful!’.
Someone suggested a workaround on stackoverflow (http://stackoverflow.com/questions/3246351/bug-in-chrome-or-stupidity-in-user-sanitising-inputs-on-forms) which works a treat, and out of all solutions I can think of, is the most elegant.
Thanks for the feedback from all – it’s been a really useful exercise!