核心:将方法移到更加合适的位置,即放在更加合适的类中,
就像JSON转换,图片处理,要单独放在一个帮助类中,而不是直接放在需要调用该函数的类中,这样可以让代码组织结构更加清晰,合理,方便以后维护。
场景:根据上端传入的枚举值修改查询数据中的单位字段同枚举描述一致
代码演示:
1、移动前
1-1、DAL层代码
using MoveMethod.Common; using MoveMethod.Model; using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Reflection; namespace MoveMethod { public class DAL { ProductDbContext prodDbContext = new ProductDbContext(); /// <summary> /// 获取数据集合 /// </summary> /// <returns></returns> public IEnumerable<Product> GetProductList() { return prodDbContext.Database.SqlQuery<Product>("select * from product").AsEnumerable(); } /// <summary> /// 处理单位转换逻辑 /// </summary> /// <param name="moneyType"></param> /// <returns></returns> public IEnumerable<Product> GetProductListChangePriceUnit(MoneyType moneyType, IEnumerable<Product> list) { string unitName = GetEnumDescription(moneyType); //获取枚举的描述值 return ChangeUnit(unitName, list.ToList()); //根据枚举描述值修改单位名称 } /// <summary> /// 单位转换函数 /// </summary> /// <param name="unitName"></param> /// <param name="list"></param> /// <returns></returns> private IEnumerable<Product> ChangeUnit(string unitName, IList<Product> list) { foreach (var item in list) { item.Unit = unitName; } return list.AsEnumerable(); } /// <summary> /// 获取枚举值上的Description特性的说明 /// </summary> /// <typeparam name="T">枚举类型</typeparam> /// <param name="obj">枚举值</param> /// <returns>特性的说明</returns> private string GetEnumDescription<T>(T obj) { var type = obj.GetType(); FieldInfo field = type.GetField(Enum.GetName(type, obj)); DescriptionAttribute descAttr = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute; if (descAttr == null) { return string.Empty; } return descAttr.Description; } } }
1-2、BLL层代码
using MoveMethod.Common; using MoveMethod.Model; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace MoveMethod { public class BLL { private DAL dal = new DAL(); public IEnumerable<Product> Query(MoneyType monyeType) { return dal.GetProductListChangePriceUnit(monyeType,dal.GetProductList()); } } }
1-3、客户端
using MoveMethod.Model; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; /// <summary> /// 移动方法 /// </summary> namespace MoveMethod { class Program { static void Main(string[] args) { IEnumerable<Product> list = new BLL().Query(MoneyType.Dollar); foreach (var item in list) { Console.WriteLine("{0} {1} {2}",item.FullName,item.Price,item.Unit); } Console.Read(); } } }
在DAL层,数据就只负责数据的查询(满足面向对象设计的单一职责原则),而不需要进行业务的处理(根据枚举的描述值修改单位字段),关于业务的处理的方法应该放在BLL层中,另外,获取枚举的描述值是一个通用的函数,其他地方可能使用到,不应放在产品实体的DAL层,而是应该封在成工具类
2、移动后
2-1、DAL层代码
using MoveMethod.Common; using MoveMethod.Model; using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Reflection; namespace MoveMethod { public class DAL { ProductDbContext prodDbContext = new ProductDbContext(); /// <summary> /// 获取数据集合 /// </summary> /// <returns></returns> public IEnumerable<Product> GetProductList() { return prodDbContext.Database.SqlQuery<Product>("select * from product").AsEnumerable(); } } }
2-2、BLL层代码
using MoveMethod.Common; using MoveMethod.Model; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace MoveMethod { public class BLL { private DAL dal = new DAL(); public IEnumerable<Product> QueryWithMoveFethod(MoneyType monyeType) { return GetProductListChangePriceUnit(monyeType, dal.GetProductList()); } /// <summary> /// 根据传入的不同枚举值修改单位并返回结果 /// </summary> /// <param name="moneyType"></param> /// <param name="list"></param> /// <returns></returns> public IEnumerable<Product> GetProductListChangePriceUnit(MoneyType moneyType, IEnumerable<Product> list) { string unitName = EnumHelper.GetEnumDescription(moneyType); //调用工具类中的获取枚举描述值的函数 return ChangeUnit(unitName, list.ToList()); } /// <summary> /// 改变单位函数 /// </summary> /// <param name="unitName"></param> /// <param name="list"></param> /// <returns></returns> private IEnumerable<Product> ChangeUnit(string unitName, IList<Product> list) { foreach (var item in list) { item.Unit = unitName; } return list.AsEnumerable(); } } }
2-3、工具类代码
using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; namespace MoveMethod.Common { public class EnumHelper { /// <summary> /// 获取枚举值上的Description特性的说明 /// </summary> /// <typeparam name="T">枚举类型</typeparam> /// <param name="obj">枚举值</param> /// <returns>特性的说明</returns> public static string GetEnumDescription<T>(T obj) { var type = obj.GetType(); FieldInfo field = type.GetField(Enum.GetName(type, obj)); DescriptionAttribute descAttr = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute; if (descAttr == null) { return string.Empty; } return descAttr.Description; } } }
2-4、客户端
using MoveMethod.Model; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; /// <summary> /// 移动方法 /// </summary> namespace MoveMethod { class Program { static void Main(string[] args) { Console.WriteLine("==================方法转移之前====================="); IEnumerable<Product> list = new BLL().Query(MoneyType.Dollar); foreach (var item in list) { Console.WriteLine("{0} {1} {2}",item.FullName,item.Price,item.Unit); } Console.WriteLine("==================方法转移之后====================="); list = new BLL().QueryWithMoveFethod(MoneyType.Dollar); foreach (var item in list) { Console.WriteLine("{0} {1} {2}", item.FullName, item.Price, item.Unit); } Console.Read(); } } }
结果展示:
写写博客,方便自己也方便有需的人~~