Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8353235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T09:11:53+00:00 2026-06-09T09:11:53+00:00

I’m going to write a validation component to use it on different projects. I’m

  • 0

I’m going to write a validation component to use it on different projects. I’m not really familiar with any of validation frameworks like Enterprise Library VAB, Fluent, CuttingEdge.Conditions and so many others, however I don’t have time to work with all of them to see which is better for my purpose.

I want this component to provide 2 different functionalities for me:

First, I want to have some validators like EmailValidator, StringLengthValidator, MyCustomValidator and so on, that I can use them whenever I want in the code, like below:

public class EmailValidator : RegexValidator // or StringValidator or whatever!
{
    public EmailValidator() : base("emailRegexHere")
    {
    }
public bool override DoValidate(string value)
    {
        return base.DoValidate(value);
    }
}
...

public void MyMethod(string email)
{
    EmailValidator validator = new EmailValidator();
    if(!validator.Validate(email))
        throw new NotValidatedException("email is invalid.");
    ...
}

Second, I need to validate parameters by applying something like DataAnnotations to any method parameter that I want without any extra coding. One possible way I know is writing Aspects using PostSharp to inject code where method starts (OnMethodEntry). I’ve done Logging with Postsharp and it works great.

Also Microsoft introduces IParameterInspector to Perform Input Validation in WCF which provide 2 methods BeforCall and AfterCall, but I think it only works for WCF.

To wrap up, I need to do validation in my WCF or WebService like this:

[System.Web.Script.Services.ScriptService]
public class MyServiceClass : System.Web.Services.WebService
{
    [Aspects.Validate]
    [WebMethod(EnableSession = true)]
    public string SubmitComment([Validation.Required]string content,[Validation.Guid] string userId,[Validation.Required] [Validation.Name]string name, [Validation.Email]string email, string ipAddress)
    {
        ...
    }
}

Note: this is just a sample code to demonstrate the behavior that I need, any other suggestions is well appreciated. Also is it a good idea that Validation.* annotations be changed to one annotation like ValidateParam(typeof(EmailValidator)) ?

Thanks in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T09:11:54+00:00Added an answer on June 9, 2026 at 9:11 am

    Yep, I would just look at PostSharp for this. An OnMethodBoundaryAspect or MethodInterceptionAspect that examines the method’s parameters (for the attributes that say how to validate) and the arguments (the values you want validate).

    Since you’ve used OnMethodBoundaryAspect before, take a look at the documentation for MethodExecutionArgs. You can get the method information with args.Method (returns a MethodBase, which is in System.Reflection). Call GetParameters() on that, which returns an array of ParameterInfo. On each ParameterInfo object, you can get information about the attributes with the Attributes property.

    Once you know the attributes, you then know which validation methods to use (or if you’re creating your own attributes, the validation method can be in those attribute classes themselves). Then you just need to use args.Arguments to get the values of the arguments.

    Here’s some (psuedo)code:

    public interface IValidator
    {
        void Validate(object value);
    }
    public class ValidationEmailAttribute : Attribute, IValidator
    {
        public Validate(string emailAddress)
        {
            // throw exception or whatever if invalid
        }
    }
    
    public class ValidateParametersAspect : OnMethodBoundaryAspect
    {
        public override OnEntry(args)
        {
            foreach(i = 0 to args.Method.GetParameters().Count)
            {
                var parameter = args.Method.GetParameters()[i];
                var argument = args.Argument[i]; // get the corresponding argument value
                foreach(attribute in parameter.Attributes)
                {
                    var attributeInstance = Activator.CreateType(attribute.Type);
                    var validator = (IValidator)attributeInstance;
                    validator.Validate(argument);
                }
            }
         }
    }
    
    public class MyClass
    {
        [ValidateParametersAspect]
        public void SaveEmail([ValidationEmail] string email)
        {
            // ...
        }
    }
    

    That’s not real code: the details you’ll have to work out yourself.

    I like the idea of putting the validation code in the attributes themselves, because you won’t have to change the aspect in order to add additional validation types.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I would like to count the length of a string with PHP. The string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I've got a string that has curly quotes in it. I'd like to replace
I want use html5's new tag to play a wav file (currently only supported
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.