Validators in Enterprise Library 5.0

Source: Internet
Author: User

After a series of related technologies of. Net 4.0 were released, windows was reinstalled .. Visual Studio is reinstalled .. I have reinstalled SQL Server and so on. Microsoft has taken the lead to start my whole body. I downloaded the Enterprise Library 5.0 component last night. In the past, I used the validation components of this framework in a large number of projects I developed using 4.1. Its design is indeed very good. However, when I used the new version yesterday, I encountered some garbage compilation errors.

What? All the validator classes in the Microsoft. Practices. enterpriselibrary. validation. validators space have such errors. Why isn't it an attribute class? You can seeSource codeOf course, let's see what happened. First, let's take a look at the definition of this validatorcompositionattribute class:

Code

Namespace Microsoft. Practices. enterpriselibrary. validation. validators
{
/// <Summary>
/// Indicates that the kind of composition to use when multiple <See CREF = "validatorattribute"/> Instances
/// Are bound to a language element.
/// </Summary>
/// <Seealso CREF = "validatorattribute"/>
[Attributeusage (attributetargets. Property
| Attributetargets. Field
| Attributetargets. Method
| Attributetargets. Parameter
| Attributetargets. Class,
Allowmultiple = True ,
Inherited = False )]
Public Sealed Class Validatorcompositionattribute: basevalidationattribute
{
Private Compositiontype;
/// <Summary>
/// Initializes a new instance of <See CREF = "validatorcompositionattribute"/> Class.
/// </Summary>
/// <Param name = "compositiontype"> The <See CREF = "compositiontype"/> To be used. </Param>
Public Validatorcompositionattribute (compositiontype)
{
This . Compositiontype = Compositiontype;
}

InternalCompositiontype
{
Get{ReturnCompositiontype ;}
}
}
}

It inherits from the basevalidationattribute class. It must be an attribute class, because its class definition uses the attributeusage feature that can only be used by classes inherited from the attribute. I have no clue. I can only go to the official website to check some changes to this version. The document address is: http://entlib.codeplex.com/wikipage? Title = entlib5changelog & # VAB

This section describes some changes to the validation framework. In particular, the last point is that the custom validator class must inherit from valuevalidatorattribute instead of validatorattribute. Otherwise, an error may occur. So I ran to see the definition of basevalidationattribute:

Dizzy. The original basevalidationattribute was inherited from the validatorattribute class. Is there a problem here? However, I cannot change it. So I noticed that the validation component of Enterprise Library 5.0 seems to have added support for data annotation, and the related component of Data annotation is system. componentmodel. dataannotations. DLL, with luck, I added this component reference to the project. BINGO ~! All compilation errors disappear without a trace. In the past, data annotation was used to implement the validator class of Enterprise Library 5.0. The problem is finally solved.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.