利用委托实现充血实体类

最近一直在想实体类应不应该具有操作,还是如以往的一样是缺血模式的实体类呢,目前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();
           
            

        }
    }

}

希望听听大家的意见

posted @   mikel  阅读(2630)  评论(16编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
点击右上角即可分享
微信分享提示