Ent Lib 3.1 Self Validation

Topics: Validation Application Block
Apr 16, 2010 at 9:59 AM

I have a class which is marked for self-validation:  

[HasSelfValidation]
public class SomeClass
{
  [SelfValidation(Ruleset="SomeRuleset")]
  public CustomValidate(ValidationResults results)
  {
  }
}  

Now, if I call enterprise library validation (specifying the type) everything works ok: 

 SomeClass sc = new SomeClass();
ValidationResults entLibResults = Validation.Validate<SomeClass>(sc, "SomeRuleset");

 

 If I omit the generic type parameter:

 

 ValidationResults entLibResults = Validation.Validate(sc, "SomeRuleset");

 

 The call succeeds (as the compiler infers the type from the object), but the self-validation does not fire.

 

 This is causing a problem for me and it is influencing my design of a common base class for my business objects. I am having to create my base business object as
e.g.

 

 public class BaseBusinessObject<T>
where T : BaseBusinessObject<T>, new()

 

 and then 

 

 ValidationResults entLibResults = Validation.Validate<T>(this, ruleSet);

 

 when what I'd prefer to do is

 

 public class BaseBusinessObject 

and

 

 ValidationResults entLibResults = Validation.Validate(this, ruleSet);

 

 Is this a bug in EntLib 3.1 validation?

Thanks in advance,
Crowhill

 

 

Apr 16, 2010 at 10:28 AM

I'll need to verify this and get back to you.  But I'll also need to confirm if the 2 validation approaches you did was applied to the SomeClass which doesn't have a base class yet, am I right?

 

Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@Avanade.com

Apr 16, 2010 at 10:40 AM

Unfortunately, I don't have the machine right now to install the 3.1 version.  I don't have the source code either.  If you could provide me with the 3.1 version of the Validation assembly (and just to be sure the Common as well), I could help you out faster. 

 

Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@Avanade.com

Apr 16, 2010 at 10:45 AM
Thanks for the prompt response.
SomeClass does have a base class:
public SomeClass : BaseBusinessObject<SomeClass>
BaseBusinessObject has a validate method:

public class BaseBusinessObject<T>
where T : BaseBusinessObject<T>, new()
{
public bool Validate(string ruleSet)
{
Validation.Validate<T>(this, ruleSet);
}
}

This works fine and selfvalidation methods of SomeClass get called.
public bool Validate(string ruleSet)
{
Validation.Validate(this, ruleSet);
}
This does not work.
Hope this clarifies the issue.
Thanks again,
Jason.

From: AvanadeSupport [mailto:notifications@codeplex.com]
Sent: 16 April 2010 10:28
To: Stevens, Jason (I&I SD&T Distributed Developements)
Subject: Re: Ent Lib 3.1 Self Validation [entlib:209543]

From: AvanadeSupport

I'll need to verify this and get back to you. But I'll also need to confirm if the 2 validation approaches you did was applied to the SomeClass which doesn't have a base class yet, am I right?

Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@Avanade.com

 
-----------------------------------------------------------------------------------------------------
Lloyds TSB Bank plc. Registered Office: 25 Gresham Street, London EC2V 7HN. Registered in England and Wales, number 2065. Telephone: 020 7626 1500.

Bank of Scotland plc. Registered Office: The Mound, Edinburgh EH1 1YZ. Registered in Scotland, number 327000. Telephone: 0870 600 5000

Lloyds TSB Scotland plc. Registered Office: Henry Duncan House, 120 George Street, Edinburgh EH2 4LH. Registered in Scotland, number 95237. Telephone: 0131 225 4555. Cheltenham & Gloucester plc. Registered Office: Barnett Way, Gloucester GL4 3RL. Registered in England and Wales, number 2299428. Telephone: 01452 372372.

Lloyds TSB Bank plc, Lloyds TSB Scotland plc, Bank of Scotland plc and Cheltenham & Gloucester plc are authorised and regulated by the Financial Services Authority. Halifax is a division of Bank of Scotland plc. Cheltenham & Gloucester Savings is a division of Lloyds TSB Bank plc. HBOS plc. Registered Office: The Mound, Edinburgh EH1 1YZ. Registered in Scotland, number 218813. Telephone: 0870 600 5000

Lloyds Banking Group plc. Registered Office: The Mound, Edinburgh EH1 1YZ. Registered in Scotland, number 95000. Telephone: 0131 225 4555

This e-mail (including any attachments) is private and confidential and may contain privileged material. If you have received this e-mail in error, please notify the sender and delete it (including any attachments) immediately. You must not copy, distribute, disclose or use any of the information in it or any attachments.

Telephone calls may be monitored or recorded.


==============================================================================

Apr 16, 2010 at 10:47 AM
Thanks,
Jason


From: AvanadeSupport [mailto:notifications@codeplex.com]
Sent: 16 April 2010 10:41
To: Stevens, Jason (I&I SD&T Distributed Developements)
Subject: Re: Ent Lib 3.1 Self Validation [entlib:209543]

From: AvanadeSupport

Unfortunately, I don't have the machine right now to install the 3.1 version. I don't have the source code either. If you could provide me with the 3.1 version of the Validation assembly (and just to be sure the Common as well), I could help you out faster.

Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@Avanade.com

 
-----------------------------------------------------------------------------------------------------
Lloyds TSB Bank plc. Registered Office: 25 Gresham Street, London EC2V 7HN. Registered in England and Wales, number 2065. Telephone: 020 7626 1500.

Bank of Scotland plc. Registered Office: The Mound, Edinburgh EH1 1YZ. Registered in Scotland, number 327000. Telephone: 0870 600 5000

Lloyds TSB Scotland plc. Registered Office: Henry Duncan House, 120 George Street, Edinburgh EH2 4LH. Registered in Scotland, number 95237. Telephone: 0131 225 4555. Cheltenham & Gloucester plc. Registered Office: Barnett Way, Gloucester GL4 3RL. Registered in England and Wales, number 2299428. Telephone: 01452 372372.

Lloyds TSB Bank plc, Lloyds TSB Scotland plc, Bank of Scotland plc and Cheltenham & Gloucester plc are authorised and regulated by the Financial Services Authority. Halifax is a division of Bank of Scotland plc. Cheltenham & Gloucester Savings is a division of Lloyds TSB Bank plc. HBOS plc. Registered Office: The Mound, Edinburgh EH1 1YZ. Registered in Scotland, number 218813. Telephone: 0870 600 5000

Lloyds Banking Group plc. Registered Office: The Mound, Edinburgh EH1 1YZ. Registered in Scotland, number 95000. Telephone: 0131 225 4555

This e-mail (including any attachments) is private and confidential and may contain privileged material. If you have received this e-mail in error, please notify the sender and delete it (including any attachments) immediately. You must not copy, distribute, disclose or use any of the information in it or any attachments.

Telephone calls may be monitored or recorded.


==============================================================================

Apr 16, 2010 at 10:57 AM
Edited Apr 16, 2010 at 11:28 AM

Could you post the definition of your base class and subclass together with the self validation methods?  I'm confused.  How exactly are you validating your object?  In your initial post, you have this:

 ValidationResults entLibResults = Validation.Validate(sc, "SomeRuleset");

and

ValidationResults entLibResults = Validation.Validate<SomeClass>(sc, "SomeRuleset");

In your reply, you included a Validate method in the base class.  Are you calling this to validate your object or are  you using the code above?

 

Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@Avanade.com

Apr 16, 2010 at 12:04 PM

Hi, only the derived class has self-validation methods.

The base class allows us to call ent lib validation without client code needing a reference to ent lib. Each derived class could implement its own Validate method, but I have 80 derived classes and each one would need to do exactly the same thing, ie. call ent lib validation and then adapt the results.

What I'm not clear about is why the type parameter ("T") needs to be explicitly defined in the Validation.Validate call for self validation to work.

If I didn't need to specify "T" my base business object wouldn't need to be generic.

public class BaseBusinessObject<T>
where T : BaseBusinessObject<T>, new()
{

public virtual BusinessObjectValidationResultCollection Validate(string ruleSet)
{
// Validate it
ValidationResults entLibResults = Validation.Validate<T>(this, ruleSet);

// 'Adapt' the results into our non-EntLib results
BusinessObjectValidationResultCollection adaptedResults = new BusinessObjectValidationResultCollection(entLibResults);
return adaptedResults;
}
}

[HasSelfValidation]
public class SomeClass
BaseBusinessObject<SomeClass>
{

[SelfValidation(Ruleset="SomeRuleset")]
public void SelfValidateMethod(ValidationResults results)
{
ValidationResult result = new ValidationResult("Error...", this, "", "", null);
results.AddResult(result);
}
}
}

CLIENT CODE:

SomeClass sc = new SomeClass();
sc.Validate("SomeRuleset");

Hope this clarifies my issue.
Thanks,
Jason


From: AvanadeSupport [mailto:notifications@codeplex.com]
Sent: 16 April 2010 10:57
To: Stevens, Jason (I&I SD&T Distributed Developements)
Subject: Re: Ent Lib 3.1 Self Validation [entlib:209543]

From: AvanadeSupport

Could you post the definition of your base class and subclass together with the self validation methods? I'm confused if both have self validation methods.

Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@Avanade.com

 
-----------------------------------------------------------------------------------------------------
Lloyds TSB Bank plc. Registered Office: 25 Gresham Street, London EC2V 7HN. Registered in England and Wales, number 2065. Telephone: 020 7626 1500.

Bank of Scotland plc. Registered Office: The Mound, Edinburgh EH1 1YZ. Registered in Scotland, number 327000. Telephone: 0870 600 5000

Lloyds TSB Scotland plc. Registered Office: Henry Duncan House, 120 George Street, Edinburgh EH2 4LH. Registered in Scotland, number 95237. Telephone: 0131 225 4555. Cheltenham & Gloucester plc. Registered Office: Barnett Way, Gloucester GL4 3RL. Registered in England and Wales, number 2299428. Telephone: 01452 372372.

Lloyds TSB Bank plc, Lloyds TSB Scotland plc, Bank of Scotland plc and Cheltenham & Gloucester plc are authorised and regulated by the Financial Services Authority. Halifax is a division of Bank of Scotland plc. Cheltenham & Gloucester Savings is a division of Lloyds TSB Bank plc. HBOS plc. Registered Office: The Mound, Edinburgh EH1 1YZ. Registered in Scotland, number 218813. Telephone: 0870 600 5000

Lloyds Banking Group plc. Registered Office: The Mound, Edinburgh EH1 1YZ. Registered in Scotland, number 95000. Telephone: 0131 225 4555

This e-mail (including any attachments) is private and confidential and may contain privileged material. If you have received this e-mail in error, please notify the sender and delete it (including any attachments) immediately. You must not copy, distribute, disclose or use any of the information in it or any attachments.

Telephone calls may be monitored or recorded.


==============================================================================

Apr 19, 2010 at 1:17 AM

Ah, this is not a bug, it was by design.  What happens is the call to Validation.Validate in the base class only looks for the validation rules defined in the base class.  When you specify the exact type (T), it goes looking for the validation rules defined in that type.  This will be changed in the 5.0 version; the Validation facade will validate based on the validation's target type.

 

Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@Avanade.com

Apr 19, 2010 at 8:41 AM
Ok, thanks for the update.

From: AvanadeSupport [mailto:notifications@codeplex.com]
Sent: 19 April 2010 01:18
To: Stevens, Jason (I&I SD&T Distributed Developements)
Subject: Re: Ent Lib 3.1 Self Validation [entlib:209543]

From: AvanadeSupport

Ah, this is not a bug, it was by design. What happens is the call to Validation.Validate in the base class only looks for the validation rules defined in the base class. When you specify the exact type (T), it goes looking for the validation rules defined in that type. This will be changed in the 5.0 version; the Validation facade will validate based on the validation's target type.

Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@Avanade.com

 
-----------------------------------------------------------------------------------------------------
Lloyds TSB Bank plc. Registered Office: 25 Gresham Street, London EC2V 7HN. Registered in England and Wales, number 2065. Telephone: 020 7626 1500.

Bank of Scotland plc. Registered Office: The Mound, Edinburgh EH1 1YZ. Registered in Scotland, number 327000. Telephone: 0870 600 5000

Lloyds TSB Scotland plc. Registered Office: Henry Duncan House, 120 George Street, Edinburgh EH2 4LH. Registered in Scotland, number 95237. Telephone: 0131 225 4555. Cheltenham & Gloucester plc. Registered Office: Barnett Way, Gloucester GL4 3RL. Registered in England and Wales, number 2299428. Telephone: 01452 372372.

Lloyds TSB Bank plc, Lloyds TSB Scotland plc, Bank of Scotland plc and Cheltenham & Gloucester plc are authorised and regulated by the Financial Services Authority. Halifax is a division of Bank of Scotland plc. Cheltenham & Gloucester Savings is a division of Lloyds TSB Bank plc. HBOS plc. Registered Office: The Mound, Edinburgh EH1 1YZ. Registered in Scotland, number 218813. Telephone: 0870 600 5000

Lloyds Banking Group plc. Registered Office: The Mound, Edinburgh EH1 1YZ. Registered in Scotland, number 95000. Telephone: 0131 225 4555

This e-mail (including any attachments) is private and confidential and may contain privileged material. If you have received this e-mail in error, please notify the sender and delete it (including any attachments) immediately. You must not copy, distribute, disclose or use any of the information in it or any attachments.

Telephone calls may be monitored or recorded.

==============================================================================