zoukankan      html  css  js  c++  java
  • 利用委托实现充血实体类

    最近一直在想实体类应不应该具有操作,还是如以往的一样是缺血模式的实体类呢,目前KiWing框架用的是缺血模式的实体类(缺血实体类指那些只有属性而没有方法的实体类),于是将现有的实体类利用委托进行了改写,代码如下:

    using System;

    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using KiWing.CustomAttribute;
    using KiWing.Helper.Attribute;


    namespace KiWing.Test
    {
        class Program
        {
            [Table("Table1","Identifier","table1Insert","table1update","table1delete","table1select")]
            public class Test
            {
                /// <summary>
                /// 委托保存方法列表
                /// </summary>
                private SaveMethod saveList;
                
                public int Identifier { get; set; }

                public string TestName { get; set; }

                /// <summary>
                /// 委托保存方法
                /// </summary>
                /// <param name="test"></param>
                /// <returns></returns>
                public delegate int SaveMethod(Test test);
                /// <summary>
                /// 注册保存委托方法
                /// </summary>
                /// <param name="method"></param>
                public void AddSaveMethod(SaveMethod method)
                {
                    saveList += method;
                }
                /// <summary>
                /// 删除已经注册的委托方法
                /// </summary>
                /// <param name="method"></param>
                public void RemoveSaveMethod(SaveMethod method)
                {
                    saveList -= method;
                }
              
                /// <summary>
                /// 保存方法
                /// </summary>
                /// <returns></returns>
                public int Save()
                {
                    if (saveList != null)
                       return saveList(this);
                    return 1;
                }

            }

            public class Area
            {
                
            }
            /// <summary>
            /// 业务处理类
            /// </summary>
            public class Business
            {
                public int Save<T>(T obj) where T : class
                {
                    Console.Write("Business Save {0}!",typeof(T).Name);

                    return 1;
                }
            
            }

            static void Main(string[] args)
            {
                //实例化实体类
                Test test = new Test();
                //属性赋值
                test.Identifier = 1;
                test.TestName="Test";
                //创建业务逻辑对象
                Business business=new Business();
                //注册委托方法
                test.AddSaveMethod(business.Save<Test>);
                //保存数据
                test.Save();

                Console.Read();
               
                

            }
        }

    }

    希望听听大家的意见


    作者:mikel
    出处:http://www.cnblogs.com/mikel/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
    该文章也同时发布在我的独立博客中-www.mikel.cn

  • 相关阅读:
    01-NoSQL概述
    SSM快速整合
    C语言指针传参与C++引用传参,以及尾插法建立单链表使用到的引用
    IP地址相关
    二叉树的先序遍历、中序遍历、后序遍历-C语言描述
    华为5G认证练习题2
    华为5G认证练习题
    华为ICT学堂获取练习题及答案
    C++ cin对象的一些方法
    webpack学习笔记2:新建工程
  • 原文地址:https://www.cnblogs.com/mikel/p/1357994.html
Copyright © 2011-2022 走看看