zoukankan      html  css  js  c++  java
  • [Validation] include rules in the domain that may vary from instance to instance

    using System;
    using System.Collections.Generic;
    using System.Linq;
    
    namespace Validation // include rules in the domain that may vary from instance to instance
    {
        class Program
        {
            static void Main(string[] args)
            {
                var memberEoo = new Member { Name = "EooName", Area = "Eoo", Age = 15, Email = string.Empty }; // Eoo member must be at least 18
                foreach (var item in memberEoo.Validate())
                {
                    Console.WriteLine(item);
                }
    
                var memberFoo = new Member { Name = "FooName", Area = "Foo", Age = 15, Email = string.Empty }; // Foo member must have an email
                foreach (var item in memberFoo.Validate())
                {
                    Console.WriteLine(item);
                }
            }
        }
    
        public class Member // domain model
        {
            private readonly List<string> _ruleItems = new List<string>(); // broken rules
    
            public string Name { get; set; }
    
            public string Area { get; set; }
    
            public int Age { get; set; }
    
            public string Email { get; set; }
    
            public List<string> Validate()
            {
                if (string.IsNullOrEmpty(Name))
                {
                    _ruleItems.Add("member must have a name");
                }
    
                if (string.IsNullOrEmpty(Area))
                {
                    _ruleItems.Add("member must have an area");
                }
                else
                {
                    _ruleItems.AddRange(RuleFactory.GetRule(Area).GetRuleItems(this)); // specific rules
                }
    
                return _ruleItems;
            }
        }
    
        public abstract class Rule
        {
            public abstract string Area { get; }
    
            public abstract IEnumerable<string> GetRuleItems(Member member);
        }
    
        public class EooRule : Rule
        {
            public override string Area => "Eoo";
    
            public override IEnumerable<string> GetRuleItems(Member member)
            {
                var ruleItems = new List<string>();
                if (member.Age < 18)
                {
                    ruleItems.Add("Eoo member must be at least 18");
                }
                return ruleItems;
            }
        }
    
        public class FooRule : Rule
        {
            public override string Area => "Foo";
    
            public override IEnumerable<string> GetRuleItems(Member member)
            {
                var ruleItems = new List<string>();
                if (string.IsNullOrEmpty(member.Email))
                {
                    ruleItems.Add("Foo member must have an email");
                }
                return ruleItems;
            }
        }
    
        public class RuleFactory
        {
            private static readonly List<Rule> _rules = new List<Rule>
            {
                new EooRule(),
                new FooRule()
            };
    
            public static Rule GetRule(string area)
            {
                return _rules.FirstOrDefault(v => v.Area == area);
            }
        }
    }
  • 相关阅读:
    TSYS2.0 碎片工作原理
    回旋。悲哉、哀哉
    Sql高级操作
    你是我最愛的人
    TSYS2.0标签说明
    TSYS:Tkl_TemplateClass 类调用详解
    CMS设计和CMS选型(内容管理系统)
    TSYS2.0 Beta与Tsys 1.1等众多版本下载
    TsysV1.1 系统文件清单介绍
    伪装成Google Bot突破收费页面
  • 原文地址:https://www.cnblogs.com/xiaowangzhi/p/10923512.html
Copyright © 2011-2022 走看看