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

  • Home
  • SEARCH
  • 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 1040055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:10:36+00:00 2026-05-16T15:10:36+00:00

I’ve created some classes that will be used to provide data to stored procedures

  • 0

I’ve created some classes that will be used to provide data to stored procedures in my database. The varchar parameters in the stored procs have length specifications (e.g. varchar(6) and I’d like to validate the length of all string properties before passing them on to the stored procedures.

Is there a simple, declarative way to do this?


I have two conceptual ideas so far:

Attributes

public class MyDataClass
{
     [MaxStringLength = 50]
     public string CompanyName { get; set; }
}

I’m not sure what assemblies/namespaces I would need to use to implement this kind of declarative markup. I think this already exists, but I’m not sure where and if it’s the best way to go.

Validation in Properties

public class MyDataClass
{
     private string _CompanyName;
     public string CompanyName
     {
         get {return _CompanyName;}
         set
         {
              if (value.Length > 50)
                  throw new InvalidOperationException();
              _CompanyName = value;
         }
     }
}

This seems like a lot of work and will really make my currently-simple classes look pretty ugly, but I suppose it will get the job done. It will also take a lot of copying and pasting to get this right.

  • 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-05-16T15:10:36+00:00Added an answer on May 16, 2026 at 3:10 pm

    I’ll post this as a different answer, because it is characteristically different than Code Contracts.

    One approach you can use to have declarative validation is to use a dictionary or hash table as the property store, and share a utility method to perform validation.

    For example:

    // Example attribute class for MaxStringLength
    public class MaxStringLengthAttribute : Attribute
    {
        public int MaxLength { get; set; }
        public MaxStringLengthAttribute(int length) { this.MaxLength = length; }
    }
    
    // Class using the dictionary store and shared validation routine.
    public class MyDataClass
    {
        private Hashtable properties = new Hashtable();
    
        public string CompanyName
        {
            get { return GetValue<string>("CompanyName"); }
    
            [MaxStringLength(50)]
            set { SetValue<string>("CompanyName", value); }
        }
    
        public TResult GetValue<TResult>(string key)
        {
            return (TResult)(properties[key] ?? default(TResult));
        }
    
        public void SetValue<TValue>(string key, TValue value)
        {
            // Example retrieving attribute:
            var attributes = new StackTrace()
                                 .GetFrame(1)
                                 .GetMethod()
                                 .GetCustomAttributes(typeof(MaxStringLengthAttribute), true);
            // With the attribute in hand, perform validation here...
    
            properties[key] = value;
        }
    }
    

    You can get at the calling property using reflection by working up your stack trace as demonstrated here. Reflect the property attributes, run your validation, and voila! One-liner getter/setters that share a common validation routine.

    On an aside, this pattern is also convenient because you can design a class to use alternative dictionary-like property stores, such as ViewState or Session (in ASP.NET), by updating only GetValue and SetValue.

    One additional note is, should you use this approach, you might consider refactoring validation logic into a validation utility class for shared use among all your types. That should help prevent your data class from getting too bulky in the SetValue method.

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

Sidebar

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.