Click here to Skip to main content
15,891,762 members
Articles / Web Development / ASP.NET

An Object Level Validation Framework

Rate me:
Please Sign up or sign in to vote.
4.94/5 (28 votes)
10 Nov 20065 min read 111K   492   85  
Provides a basic/advanced object level framework for validation.
using System;
using System.Collections.Generic;
using System.Text;

using Cobalt.Validation;
using Cobalt.Validation.Validators;

using NUnit.Framework;

namespace Cobalt.UnitTests.Validation
{
    [TestFixture]
    public class InjectionTest
    {

        #region Instance Test

        private class InstanceTestClass
        {
            private string testProperty;

            public string TestProperty
            {
                get { return this.testProperty; }
                set { this.testProperty = value; }
            }
        }

        [Test]
        public void InstanceTest()
        {
            InstanceTestClass tc = new InstanceTestClass();
            ValidationManager vm = new ValidationManager(tc);

            vm.AddValidator(new RequiredValidator(tc.GetType().GetProperty("TestProperty")));
            
            Assert.IsFalse(vm.IsValid());

            tc.TestProperty = "something";

            Assert.IsTrue(vm.IsValid());
        }

        #endregion

        #region Global Test

        private class GlobalTestClass
        {
            private string testProperty;

            public string TestProperty
            {
                get { return this.testProperty; }
                set { this.testProperty = value; }
            }
        }

        [Test]
        public void GlobalTest()
        {
            GlobalTestClass tc = new GlobalTestClass();
            ValidationManager.AddGlobalValidator<GlobalTestClass>(new RequiredValidator(tc.GetType().GetProperty("TestProperty")));
            ValidationManager vm = new ValidationManager(tc);

            Assert.IsFalse(vm.IsValid());

            tc.TestProperty = "something";

            Assert.IsTrue(vm.IsValid());

            Assert.IsTrue(ValidationManager.IsValid(tc));
        }

        #endregion

        #region Global/Instance Test

        private class GlobalInstanceClass
        {
            private string testProperty;

            public string TestProperty
            {
                get { return this.testProperty; }
                set { this.testProperty = value; }
            }
        }

        [Test]
        public void GlobalInstance()
        {
            GlobalInstanceClass tc = new GlobalInstanceClass();
            ValidationManager vm = new ValidationManager(tc);

            vm.AddValidator(RequiredValidator.CreateValidator<GlobalInstanceClass>("TestProperty"));

            Assert.IsFalse(vm.IsValid());

            tc.TestProperty = "something";

            Assert.IsTrue(vm.IsValid());

            ValidationManager.AddGlobalValidator<GlobalInstanceClass>(LengthValidator.CreateValidator<GlobalInstanceClass>("TestProperty", 10, uint.MaxValue));

            Assert.IsFalse(vm.IsValid());

            tc.TestProperty = "somethinglonger";

            Assert.IsTrue(vm.IsValid());

        }

        #endregion

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
I work at a financial services firm using mostly .NET. I enjoy my job immensely and love teaching, training, and mentoring younger developers.

Comments and Discussions