Validation doesn't work if reference is 'object'

Topics: Validation Application Block
Feb 23, 2008 at 12:26 AM
Edited Feb 23, 2008 at 7:01 AM
This test (below) fails on the second test, which makes no sense to me.
Is there a good reason why it fails here? Or is there any way, without having to use reflection, to get it to work as expected?

private class Foo
     public Foo(string name) { Name = name; }
    public string Name;
public void DoesValidationWorkWithObjects()
    Foo realFoo = new Foo("invalid");
    Assert.That(Validation.Validate(realFoo).IsValid, Is.False);
    object objectFoo = new Foo("invalid");
    Assert.That(Validation.Validate(objectFoo).IsValid, Is.False);    // this fails, IsValid is true - why?

Mar 1, 2008 at 8:49 AM
Edited Mar 1, 2008 at 10:01 PM
Yes, maybe it's a trap for young players. To get this test working, I needed to use something like this (see code below).
Because, the facade (used above - that is Validation.Validate(objectFoo)) is almost secretly using generics and hence foiling the validation process - which relies on knowing the 'type'.

object objectFoo = new Foo("invalid");
ValidationResults results = ValidationFactory.CreateValidator(objectFoo.GetType()).Validate(entity);

Aside from me just making a mistake, does anyone think that the facade is basically asking for people to make this mistake? Is it a good idea/design? and/or was i just too stupid ;-)