Thank you very much for showing me the way. I guess we are almost there but just a little doubt.
In a WinForm integrated validation scenario, while DoValidate is automatically called on entering data in the Username text box, the 'currentTarget' parameter that is passed will contain a reference to a 'Microsoft.Practices.EnterpriseLibrary.Validation.Integration.WinForms.ValidatedControlItem'
and not the business object.
I guess for the above reason the Delegate.CreateDelegate Method (Type, Object, String) call would not be able to associate the instance method 'void CheckUserNameUnique(ValidationResults results)' by using currentTarget as a parameter. However, when validating
from any call like 'Validation.Validate(objItem)' (that I intend to use on Save button click for double check), the business object instance will be passed and your method should work.
I haven't got a chance to test your solution, just replied by looking at the code, so I may be wrong.
While looking at the docs, I found an overload of CreateDelegate as Delegate.CreateDelegate Method (Type, Type, String) that creates a delegate of the specified type that represents the specified static method of the specified class. Using this overload,
if we change the instance method CheckUserNameUnique to a static method, shall the solution work as -
Delegate.CreateDelegate(typeof(CustomValidatorDelegate), typeOf(User), this.methodToInvoke) as CustomValidatorDelegate
If the alternate approach, using a static method works, then I have the following queries -
1. Though this will not be used in a multi-threaded scenario, still, just for enquiry, will it be thread safe to implement unique check through a static method. I guess two threads can enter the same block at the same time and since both will be checking
with the database, both will return the names as 'valid' while on saving they will generate duplicate names.
2. If the above point can be ignored (since multithreaded environment is out of question in my case), do you feel that the Delegate-Static method approach has any other potential concern areas ? Since you are much more experienced than me, you can visualize