Validation Delegate Signatures - why use "where T: Type"

Validation Delegate Signatures - why use "where T: Type"

Old forum URL: forums.lhotka.net/forums/t/6345.aspx


DCottle posted on Thursday, February 05, 2009

Ok, so following the example from the book, Rocky has written that the signature of a Validation Delegate should look like:

private static bool StartDateGTEndDate<T>(T target, Csla.Validation.RuleArgs e) where T: Project

if this is an object specific rule, wouldn't it work just as well to write:

private static bool StartDateGTEndDate<Project>(Project target, Csla.Validation.RuleArgs e)

 

Otherwise stated, is there any advantage to his syntax if you know that type of object you are dealing with is a Project?

RockfordLhotka replied on Thursday, February 05, 2009

I don't know - does that actually compile and run?

JoeFallon1 replied on Thursday, February 05, 2009

The signature will compile but the target value will be useless in the method body.

When done the right way you can use target. and get intellisense to list the fields and properties for you.

With this "new" syntax the list does not appear and you lose all the benefits of doing it the right way - you no longer have access to the fields and properties of the target.

Joe

PS - I have this comment in my code:

'Strongly Typed version: This code works because .NET allows Shared methods in a class to access the Private fields of an instance of that type. Since target is an instance of MyClass, and the XYZ() method is implemented in MyClass class, its code is allowed to access the Private fields of the MyClass object.

DCottle replied on Tuesday, February 10, 2009

Compile, yes.

 

Run, no.

 

Well, actually what it is doing is taking <WhateverTextIPutInHere> and treating it as <T>...so the previous comment is correct.  You lose all intellisense and specific object typing.

 

Sorry for the stupid question.  Should have investigated it further before posting.

Copyright (c) Marimer LLC