Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 3 Current »

The following sample code shows how one can take advantage of all the built-in Rules that are offered in PLINQO.

using System;
using System.ComponentModel.DataAnnotations;
using CodeSmith.Data.Attributes;
using CodeSmith.Data.Rules;
using CodeSmith.Data.Rules.Assign;
using CodeSmith.Data.Rules.Security;
using CodeSmith.Data.Rules.Validation;

namespace Sample.Data
{
    public partial class User
    {
        static partial void AddSharedRules()
        {
            //validation rules
            RuleManager.AddShared<User>(new CompareRule<int>("Age", 21, ComparisonOperator.GreaterThanOrEqual));
            RuleManager.AddShared<User>(new CustomRule<string>("UserName", "UserName must be unique.", UniqueUserName));
            RuleManager.AddShared<User>(new LengthRule("FirstName", 100));
            RuleManager.AddShared<User>(new RangeRule<int>("Age", 18, 21));
            RuleManager.AddShared<User>(new RegexRule("Email", @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"));
            RuleManager.AddShared<User>(new RequiredRule("Email"));

            //assignment rules.  These rules are to help in assigning default values
            RuleManager.AddShared<User>(new GuidRule("UserId", EntityState.New));
            RuleManager.AddShared<User>(new NowRule("CreatedDate", EntityState.New));
            RuleManager.AddShared<User>(new NowRule("ModifiedDate", EntityState.Dirty));
            RuleManager.AddShared<User>(new UserNameRule("CreatedBy", EntityState.New));
            RuleManager.AddShared<User>(new UserNameRule("ModifiedBy", EntityState.Dirty));
            RuleManager.AddShared<User>(new IpAddressRule("IpAddress", EntityState.Dirty));
            RuleManager.AddShared<User>(new DefaultValueRule<int>("Score", 100, EntityState.New));

            //security rules
            RuleManager.AddShared<User>(new CreateRule("Administrators", "Users"));
            RuleManager.AddShared<User>(new DeleteRule("Administrators"));
            RuleManager.AddShared<User>(new UpdateRule("Administrators"));
        }

        //This is called by the custom rule. The first argument is the property value.
        private static bool UniqueUserName(string username)
        {
            //check user name is unique;
            return true;
        }

        #region Metadata

        private class Metadata
        {
            // Only Attributes in the class will be preserved.

            [Guid(EntityState.New)]
            public Guid UserID { get; set; }

            [Required]
            public string UserName { get; set; }

            [IpAddress(EntityState.Dirty)]
            public string IpAddress { get; set; }

            [Required]
            [DataType(DataType.Password)]
            public string Password { get; set; }

            [DataType(DataType.EmailAddress)]
            [RegularExpression(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*")]
            public string Email { get; set; }

            [StringLength(100)]
            public string LastName { get; set; }

            [Range(18, 21)]
            public int Age { get; set; }

            [Now(EntityState.New)]
            public DateTime CreatedDate { get; set; }

            [Now(EntityState.Dirty)]
            public DateTime ModifiedDate { get; set; }

            [UserName(EntityState.New)]
            public string CreatedBy { get; set; }

            [UserName(EntityState.Dirty)]
            public string ModifiedBy { get; set; }
        }

        #endregion
    }
}

Next: Entity Enhancements

  • No labels