博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Validation] include rules in the domain that may vary from instance to instance
阅读量:5317 次
发布时间:2019-06-14

本文共 2603 字,大约阅读时间需要 8 分钟。

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
_ruleItems = new List
(); // broken rules public string Name { get; set; } public string Area { get; set; } public int Age { get; set; } public string Email { get; set; } public List
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
GetRuleItems(Member member); } public class EooRule : Rule { public override string Area => "Eoo"; public override IEnumerable
GetRuleItems(Member member) { var ruleItems = new List
(); 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
GetRuleItems(Member member) { var ruleItems = new List
(); if (string.IsNullOrEmpty(member.Email)) { ruleItems.Add("Foo member must have an email"); } return ruleItems; } } public class RuleFactory { private static readonly List
_rules = new List
{ new EooRule(), new FooRule() }; public static Rule GetRule(string area) { return _rules.FirstOrDefault(v => v.Area == area); } }}

 

转载于:https://www.cnblogs.com/xiaowangzhi/p/10923512.html

你可能感兴趣的文章
线程同步-使用SimaphoreSlim类
查看>>
Spring整合MyBatis
查看>>
Java/Java Web中乱码解决汇总
查看>>
表格操作
查看>>
TortoiseGit使用指南
查看>>
大数据学习——securecrt同时向多个tab窗口发送相同的命令
查看>>
Swift学习笔记(4):字符串
查看>>
Windows下部署多个Tomcat
查看>>
[BZOJ1672][Usaco2005 Dec]Cleaning Shifts 清理牛棚
查看>>
VBoxManage命令速记
查看>>
(转载)我们工作到底为了什么 (HP大中华区总裁孙振耀退休感言)
查看>>
phpstudy + dvws
查看>>
2016/3/10 数据库简单操作( 创建数据库 创建表 数值类型 主键 外键 自动递增 )...
查看>>
Redis客户端连接池问题
查看>>
linux C print
查看>>
Mac-Navicat Premium For Mac 12 破解 - [数据库可视化工具,亲测完美破解]
查看>>
利用Chrome查看网页渲染机制
查看>>
第5章 初始化与清理
查看>>
委托与事件
查看>>
MVC实战之排球计分(七)——软件的具体实现与测试
查看>>