跟项目经理学搭建框架

来源:互联网 发布:java后端开发要学什么 编辑:程序博客网 时间:2024/05/19 23:55

        项目原来的框架是经典三层,传统的U,D,B耦合性太强。上周项目经理跟带着我们一起搭建框架。设想了几种方案。最后决定通过通过三层+接口+反射+特性让系统更为灵活实现解耦的目的。主要是利用反射在运行时动态的加载依赖项。总的框架图如下


      B、D层主要通过抽象出接口来实现解耦,具备更好的扩展性,层与层之间使用特性+反射的方法在运行时更具类的特性名称以及内部对应的方法名称查找依赖项。为了各层直接更好的调用,抽象出上下文Context,定义B,D层的入口IBeanManager和IDalManager,以及数据操作IOption。需要调用依赖项时通过上下文Context可以查找。

    小编认为这个系统的亮点在于B和D层的Manager,抽象出了各种业务方法,同时为调用层提供给了唯一的入口点。即如下的方法

 /// <summary>        /// 加载全部的业务对象        /// </summary>        private void LoadAllBeanObject()        {            try            {                //获取本程序集下实现IBean接口的全部业务对象实例。                Assembly BeanBuilderAbly = Assembly.GetExecutingAssembly();                Type[] types = BeanBuilderAbly.GetTypes();                foreach (Type type in types)                {                    //如果当前类型实现了IBean接口                    if (type.GetInterface("IBean") != null)                    {                        //检索应用与指定成员的指定类型的自定义特性                        BeanAttribute theBeanAttri = type.GetCustomAttribute(typeof(BeanAttribute)) as BeanAttribute;                        //并且定义了业务标记                        if (theBeanAttri != null)                        {                            //添加到业务构造器实例字典                            IBean beanObj = Activator.CreateInstance(type) as IBean;                            if (beanObj != null)                            {                                //1.调用业务对象的初始化方法,完成初始化                                beanObj.Init();                                //2.将初始化完毕的业务对象,添加到业务对象字典中                                beanObjcetDic.Add(theBeanAttri.BeanName, beanObj);                            }                        }                    }                }            }            catch (Exception e)            {                loger.WriteLog("BeanManager.LoadAllBeanObject", e, LogLevel.Error, typeof(BeanManager));            }        }        /// <summary>        /// 根据业务对象名称获取业务对象实例        /// </summary>        /// <param name="beanName"></param>        /// <returns></returns>        public IBean GetBeanByName(string beanName)        {            if (string.IsNullOrEmpty(beanName))                return null;            if (beanObjcetDic != null && beanObjcetDic.ContainsKey(beanName))                return beanObjcetDic[beanName];            return null;        }        /// <summary>        /// 指定业务对象执行指定的动作        /// </summary>        /// <param name="beanName">业务对象名称</param>        /// <param name="actionName">动作名称</param>        /// <param name="pars">动作依赖的参数</param>        /// <returns>动作执行的结果</returns>        public object Action(string beanName, string actionName, params object[] pars)        {            try            {                if (string.IsNullOrEmpty(beanName) || string.IsNullOrEmpty(actionName))                    return null;                IBean findBean = GetBeanByName(beanName);                if (findBean == null)                    return null;                else                    return findBean.Action(actionName, pars);            }            catch (Exception e)            {                loger.WriteLog("BeanManager.Action", e, LogLevel.Error, typeof(BeanManager));                return null;            }        }
在具体的调用层又是如何呢?

    DataTable dt = cssimp_utils.CSSIMPContext._CurrContext.BeanManager.Action("bean_alarm_manager", "QueryDeviceBasicInfo",deviceId) as DataTable;
红色部分分别是需要调用的类的签名,及其方法,参数。

       关键的代码就是上述了,只为说明一种框架实现的方式以及如何通过抽象使系统更加具有扩展性,灵活性,高内聚低耦合。上述的框架还在继续完善中,也希望路过的可以提出宝贵意见。

      另外还想让大家看另外一个系统的框架图,也许会发现什么。  

   
   B层跟D层这两个框架很相似吧。so.....框架的基层搭建也都会经历这样的基础。一层层的抽象和解耦。其实在这次搭建框架的过程中更多的是对抽象的又一次深刻的理解。面向对象的三大特征。抽象,封装,继承在框架设计当中越来越明显的体现。


          

0 0
原创粉丝点击