快速开发框架第一弹:枚举啊,配置什么的,最讨厌了

引言

  写这个系列是一是为对自己工作至今的一个总结,二是这块一直是心里想做的东西,现在做好一部分,以后可以慢慢完善,最重要的是了个心愿可以学习其它的东西.

  这个系列主要是介绍目前为止我个人开发模式。包括基础框架(ORM+通用类库+通用控件库+通用权限系统)+灵活的代码生成器,这里面很多东西来自开源程序或者其它软件的思想。由于经验不是很多,所以可能分享的内容质量也不会高,目的还是希望把这个东西拿出来与喜欢这种开发方式的朋友一起讨论,或者其中一些想法和做法能给你提供一个思路,最主要的是个人也分享中得到了升华. 

配置表

  字典表和配置表基本上是项目不可缺少的2张表.先上表结构:

sys_Setting表主要来做项目一些单项设置,比如,我们一些配置在webconfig中appsetting中的配置,网站的标题,基础分页大小等一切配置啥的.另外,项目中如果没有规范的配置的话,维护的人是非常头疼的。这个做法来源于BlogEngine这个开源项目。实现非常简单

public class GlobalConfig
    {
        private static GlobalConfig projectConfigsSingleton;

        private GlobalConfig()
        {
            Load();
        }

        #region Instance
        public static GlobalConfig Instance
        {
            get
            {
                if (projectConfigsSingleton == null)
                {
                    projectConfigsSingleton = new GlobalConfig();
                }
                return projectConfigsSingleton;
            }
        }
        #endregion

        #region "Properties"
        public bool EnableRating { get; set; }
        public string Name { get; set; }
        #endregion

        #region "Load"
        private void Load()
        {
            Type settingsType = this.GetType();


            Dictionary<string, string> dic = SettingService.Get_Settings();

            foreach (string key in dic.Keys)
            {

                string name = key;
                string value = dic[key];


                foreach (PropertyInfo propertyInformation in settingsType.GetProperties())
                {
                    if (propertyInformation.Name.Equals(name, StringComparison.OrdinalIgnoreCase))
                    {
                        try
                        {
                            if (propertyInformation.CanWrite)
                            {
                                propertyInformation.SetValue(this, Convert.ChangeType(value, propertyInformation.PropertyType, CultureInfo.CurrentCulture), null);
                            }
                        }
                        catch
                        {
                            throw new Exception("赋值失败");
                        }
                        break;
                    }
                }
            }

        }
        #endregion

        #region "Refresh"
        public void Refresh()
        {
            projectConfigsSingleton = null;
        }

        #endregion
    }

用法也很简单:

 var siteName  = GlobalConfig.Instance.Name;
 var EnableRating = GlobalConfig.Instance.EnableRating;

介绍:1.表的设计,这个表和字典表是一样不允许被删除记录的,每次删除只是通过IsDelete字段标示字段已经被删除,如果需要多国语言,可在加上语言ID字段。

  2.代码实现:通过反射把数据库的配置给字段赋值,好处在于,所有的全局配置集中在一个地方,使用一个类集中控制,所有的配置都是强类型的,可以轻松读取。

字典表的设计

   public class GlobalDict
    {
        #region "Member"
        private static GlobalDict GlobalDictSingleton;
        private static List<Dict> listGT = new List<Dict>();

        /// <summary>
        /// 数据库类型
        /// </summary>
        public const int DBType = 1;

        /// <summary>
        /// 员工类型
        /// </summary>
        public const int EmployeeType = 6;

        /// <summary>
        /// 员工状态
        /// </summary>
        public const int EmployeeStus = 9;
        #endregion

        #region Instance
        public static GlobalDict Instance
        {
            get
            {
                if (GlobalDictSingleton == null)
                {
                    GlobalDictSingleton = new GlobalDict();
                }
                return GlobalDictSingleton;
            }
        }
        #endregion

        #region "ctor"
        static GlobalDict()
        {
            if (listGT.IsNullOrCountLTE0())
                Inint_Bind();
        }
        static void Inint_Bind()
        {
            listGT = BaseDao<Dict>.Get_Entity_byWhere(Dict._.IsDelete==0,null);
        }
        #endregion

        #region "绑定字典到控件"
        /// <summary>
        /// 绑定字典 通过传入的字典ID
        /// </summary>
        /// <param name="listControl"></param>
        /// <param name="parentID"></param>
        /// <param name="isNeedDefaultValue"></param>
        public void Bind_Dict(ListControl listControl, int parentID, bool isNeedDefaultValue)
        {
            if (listControl == null) throw new Exception("传入的绑定控件为空");
            if (listGT == null || listGT.Count <= 0) throw new Exception("数据集合为空");

            listControl.DataSource = listGT.Where(o => o.D_ParentID == parentID);
            listControl.DataTextField = "D_Name";
            listControl.DataValueField = "DictID";
            listControl.DataBind();
            if (isNeedDefaultValue)
                ControlHelper.AddDefaultToDDl("-- 请选择 --", listControl);
        }
        #endregion

        #region "通过传入的父ID 得到字典集合"
        /// <summary>
        /// 通过传入的父ID 得到字典集合
        /// </summary>
        /// <param name="parentID"></param>
        /// <returns></returns>
        public List<Dict> Get_Dict(int parentID)
        {
            if (parentID == 0) throw new Exception("传入的父ID不能为空");
            if (listGT == null || listGT.Count <= 0) throw new Exception("数据集合为空");

            return listGT.Where(o => o.D_ParentID == parentID).ToList();
        }
        #endregion

        #region "Refresh"
        public void Refresh()
        {
            listGT = null;
        }
        #endregion
    }

使用方法如下:

  //通过父ID得到一个组的集合
  var List = GlobalDict.Instance.Get_Dict(GlobalDict.DBType);
   //通过父ID 绑定传入的List控件
   GlobalDict.Instance.Bind_Dict(ddl_SupplierID, GlobalDict.DBType,true);

字典表的引入,我更愿意用它来解决一般的枚举的处理(请自行斟酌项目情况,考虑是否适合项目结构和特殊的用途),.如果你看看这个类的用法,就能看到,我们得到一个枚举或者枚举绑定到控件都是很简单的,枚举在一个地方保存,大家一看遍知,添加枚举的选项,修改枚举的文本字段都很方便,同样,这个表的设计也是不允许删除的,只能通过条件来设定字段是否被删除。

原文地址:https://www.cnblogs.com/mmmjiang13/p/1926926.html