Print Page | Close Window

Generic Emtpy Guid Verifier

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2010
Forum Discription: For .NET 4.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=2115
Printed Date: 07-May-2024 at 10:59am


Topic: Generic Emtpy Guid Verifier
Posted By: BringerOD
Subject: Generic Emtpy Guid Verifier
Date Posted: 01-Sep-2010 at 9:39pm
Can someone help me make a generic verifier that checks a Guid to see if its equal to Guid.Empty?
 
I am not getting how to write these yet.  They don't make alot of sense to me.
 
I can see how to make one for a specific entity type.
 
The whole process is very complicated for someone who is new to DevForce. 
 
I have read the documentation 3 times now.
 
Bryan



Replies:
Posted By: BringerOD
Date Posted: 01-Sep-2010 at 9:42pm
This is how far I got.
 
Just not sure how to get the property to validate. 
 
 

public static class VerifierHelper

{

private static Verifier GuidCannotBeEmpty(EntityProperty pEntityProperty)

{

string description = pEntityProperty.PropertyInfo.Name + " cannot be blank.";

var verifier = new DelegateVerifier<EntityBase>(description, GuidCannotBeEmptyCondition);

verifier.AddTriggers(pEntityProperty.Name);

verifier.VerifierOptions.ExecutionModes = VerifierExecutionModes.InstanceAndOnAfterSetTriggers;

return verifier;

}

private static VerifierResult GuidCannotBeEmptyCondition(

EntityBase entityBase, TriggerContext pTriggerContext, VerifierContext pVerifierContext)

{

if (pTriggerContext != null &&

pTriggerContext.Timing == TriggerTiming.BeforeSet)

{

throw new VerifierException("Verifier not implemented for Preset");

}

return new VerifierResult(?????);

}

}



Posted By: DenisK
Date Posted: 02-Sep-2010 at 5:49pm
Hi Bryan;

I agree that the Verification chapter needs some work. We have made that a priority to improve the Wiki in the next few months.

As for your question, instead of creating a DelegateVerifier, we should use a PropertyValueVerifier abstract class to create the generic GUID.Empty verifier. The PropertyValueVerifier abstract class is useful when you need to create a custom verifier when comparing objects or values. You can refer to our API Documentation --> IdeaBlade.Validation namespace for more useful Verifier classes.

Here's the code to create your generic GUID.Empty verifier:

private static Verifier CheckForGUIDEmptyVerifier(Type entityType, string propertyName) {
      Verifier v = new GUIDEmptyVerifier(entityType, propertyName);
      v.VerifierArgs.ErrorMessageInfo.ErrorMessage = 
        String.Format("{0} cannot be blank", propertyName);

      return v;
    }
    
    public class GUIDEmptyVerifier : PropertyValueVerifier {

      public GUIDEmptyVerifier(Type pType, String propertyName, String displayName = null, bool? shouldTreatEmptyStringAsNull = null)
        : base(new PropertyValueVerifierArgs(pType, propertyName, false, displayName, shouldTreatEmptyStringAsNull)) {
      }

      protected override VerifierResult VerifyValue(object itemToVerify, object valueToVerify,
        TriggerContext triggerContext, VerifierContext verifierContext) {

        var aGUID = (Guid)valueToVerify;
        var result = (aGUID != Guid.Empty);

        return new VerifierResult(result);
      }
    } 


Posted By: BringerOD
Date Posted: 03-Sep-2010 at 6:53am
Thanks!
 
That really helped.



Print Page | Close Window