利用委托实现充血实体类

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

        }
    }

}

希望听听大家的意见

转载于:https://www.cnblogs.com/mikel/archive/2008/12/19/1357994.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值