【ITOO】--搭建底層框架

ZeroWM發表於2015-01-18


前言

  開始學習的時候,搭建底層架構非常吃力。看文件,感覺像天書的似的,腦子裡面一團漿糊,就看著專案組長的F12按的特別歡,具體跳到哪裡了,不知道。

  不過硬著頭皮做了一段時間的專案後,因為要如果不分析裡面的呼叫關係,根本就不知道一條線怎麼下來,所以一開始模仿,漸漸地開始有了自己的思路,感覺整個框架在腦海裡面越來越清晰。

 

 
底層框架

  .Net的框架十分的強大,跟之前的永和豆漿小系統比,真的是從原始社會直接奔向了小康社會。

廢話不多說的,趕緊進入正題,整個框架主要分成以下這幾塊,簡單的介紹一下它的呼叫關係:



   客戶端:MVC框架,View層主要負責介面顯示,裡面的控制元件主要採用EasyUI,少量不可修改的屬性也可以手寫JS來修改樣式,是門面;Controller裡面主要呼叫WCF裡面的方法,也可以配置View層的動態表頭資訊等;Model層這裡其實不是我們說的底層類庫,與之對應的是WCF層裡面的資料契約,資料契約與頁面上顯示的控制元件資訊相對應,不一定是跟實體裡面的欄位一致。

  WCF:主要是溝通客戶端和服務端。建立WCFService具體類的時候,會與之產生一個對應的介面,我們把它刪掉,在ServiceContracts裡面重新建立一個與具體類對應的介面。這樣做的目的是考慮到多個介面釋出的效率會不如釋出一個介面的效率。

  服務端:BLL/DAL層都是採用的傳統的抽象工廠建立介面來實現具體類,使介面和BLL、DAL解耦,提高了系統的靈活性。BLL/DAL之間存在DbSession,BLL呼叫Dbsession的工廠建立與之對應的介面,這個介面具體實現是呼叫DAL層的工廠建立DAL層的介面,實現DAL層類。這繞一下,目的是保證執行緒的唯一。BLL/DAL的介面都有三種:ICoreService/IBaseService/IUserInfoService,這三種依次是底層類庫的介面、各系統服務總介面、各個系統具體的介面。通過部分介面和部分類,來實現多個類、介面實現統一的介面,然後進行釋出。


總結

回眸一笑百媚生,重點要回顧。



2015年12月4日

  今天給同事們講框架,心情很激動,準備這個用了差不多3個小時左右的時間,感覺自己緊張了一下,但是講的還算很盡興的。


整個框架主要分成兩部分:客戶端、服務端。

    客戶端主要用來對外發布頁面,服務端主要為客戶端提供邏輯和資料。客戶端配置檔案,主要是WCF配置:包含服務端的釋出終結點,和其他系統WCF釋出終結點。服務端的配置檔案,主要包括B/D/DbSession的IOC容器配置,及屬性的注入。

    IOC(Inverse of Control)控制反轉,控制反轉是一種將元件的依賴關係的建立和管理置於程式外部的技術,由容器控制程式之間的關係,而不是由程式碼直接控制,控制權從程式碼轉向了容器,所以稱為反轉。

    一個需要特定的依賴的元件一般會涉及一個依賴物件,在IOC的概念叫做目標(target)。IOC提供了這樣的服務,使一個元件能夠在它的整個生命週期中訪問它的依賴和服務,用這種方法與它的依賴進行互動。


包括依賴查詢,依賴注入,在程式碼中都有體現。

什麼是依賴查詢呢?

查詢dbSession,從快取中獲得資料庫上下文資訊,從SpringHelper處獲得DbSession的依賴關係。

   public override void SetDbSession()
        {

            //從快取中取出上下文資訊
            ICoreDbSession dbSession = CallContext.GetData("DbSession") as ICoreDbSession;

            if (dbSession == null)
            {
                //從快取中拿到這個資訊,<span style="font-family:KaiTi_GB2312;">依賴查詢</span>
                dbSession = SpringHelper.GetObject<ICoreDbSession>("DBSession");
                //TODO:DbContext,執行緒內快取,不適用於叢集,後期分散式快取進行處理,key為guid
                CallContext.SetData("DbSession", dbSession);
            }

            this.MyBasedbSession = dbSession;
            this.DbSession = (IDbSession)this.MyBasedbSession;
        }

web.config進行依賴注入,兩個物件間的依賴關係在程式執行時由外部容器動態的注入依賴行為方式稱為依賴注入(DI).

 <objects xmlns="http://www.springframework.net">
      <object id="entitys" type="ITOO.BasicChooseManager.Model.Entities,ITOO.BasicChooseManager.Model" singleton="false" />
      <!--DbSession層的的註解-->
      <object id="DBSession" type="ITOO.BasicChooseManager.DAL.DbSession,ITOO.BasicChooseManager.DAL" singleton="false">
        <!--加入屬性注入,指向D層的注入-->
        <property name="ChoseCourseCourseDal" ref="ChoseCourseCourseDal" />
        <!--<property name="ChoseCouseDal" ref="ChoseCouseDal" />-->
        <property name="ChoseCouseRoundDal" ref="ChoseCouseRoundDal" />
        <!--<property name="ChoseCourseRoundCollegeDal" ref="ChoseCourseRoundCollegeDal" />-->
        <property name="ChoseCourseRoundCourseDal" ref="ChoseCourseRoundCourseDal" />
        <!--<property name="ChoseCourseRoundGradeDal" ref="ChoseCourseRoundGradeDal" />
        <property name="ChoseCourseRoundLevelsDal" ref="ChoseCourseRoundLevelsDal" />-->
        <property name="CalendarDal" ref="CalendarDal" />
        <property name="RoundGradeCollegeDal" ref="RoundGradeCollegeDal" />

        <property name="CreateCourseDal" ref="CreateCourseDal" />
        <property name="CourseCollegeTreeDal" ref="CourseCollegeTreeDal"/>
        <property name="CouldChooseCourseDal" ref="CouldChooseCourseDal"/>
        <property name="OnClassStudentDal" ref="OnClassStudentDal"/>
        <property name="ChoosedCourseDal" ref="ChoosedCourseDal"/>
        <property name="StudentDal" ref="StudentDal"/>
        <property name="GradeLevelDal" ref="GradeLevelDal"></property>
        <property name="CourseRoundGradeCollegesDal" ref="CourseRoundGradeCollegesDal"></property>
    </object>
      <!--D層的的註解-->
      <object id="ChoseCourseCourseDal" type="ITOO.BasicChooseManager.DAL.ChoseCourseCourseDal,ITOO.BasicChooseManager.DAL" singleton="false" />
    
      <object id="ChoseCouseRoundDal" type="ITOO.BasicChooseManager.DAL.ChoseCouseRoundDal,ITOO.BasicChooseManager.DAL" singleton="false" />

      <object id="ChoseCourseRoundCourseDal" type="ITOO.BasicChooseManager.DAL.ChoseCourseRoundCourseDal,ITOO.BasicChooseManager.DAL" singleton="false" />

      <object id="CalendarDal" type="ITOO.BasicChooseManager.DAL.CalendarDal,ITOO.BasicChooseManager.DAL" singleton="false" />
		
      <object id="RoundGradeCollegeDal" type="ITOO.BasicChooseManager.DAL.RoundGradeCollegeDal,ITOO.BasicChooseManager.DAL" singleton="false" />

      <object id="CreateCourseDal" type="ITOO.BasicChooseManager.DAL.CreateCourseDal,ITOO.BasicChooseManager.DAL" singleton="false" />
      <object id="CourseCollegeTreeDal" type="ITOO.BasicChooseManager.DAL.CourseCollegeTreeDal,ITOO.BasicChooseManager.DAL" singleton="false" />
      <object id="CouldChooseCourseDal" type="ITOO.BasicChooseManager.DAL.CouldChooseCourseDal,ITOO.BasicChooseManager.DAL" singleton="false" />
      <object id="OnClassStudentDal" type="ITOO.BasicChooseManager.DAL.OnClassStudentDal,ITOO.BasicChooseManager.DAL" singleton="false" />
      <object id="ChoosedCourseDal" type="ITOO.BasicChooseManager.DAL.ChoosedCourseDal,ITOO.BasicChooseManager.DAL" singleton="false" />
      <object id="StudentDal" type="ITOO.BasicChooseManager.DAL.StudentDal,ITOO.BasicChooseManager.DAL" singleton="false" />
      <object id="GradeLevelDal" type="ITOO.BasicChooseManager.DAL.GradeLevelDal,ITOO.BasicChooseManager.DAL" singleton="false"></object>
      <object id="CourseRoundGradeCollegesDal" type="ITOO.BasicChooseManager.DAL.CourseRoundGradeCollegesDal,ITOO.BasicChooseManager.DAL" singleton="false" ></object>
      
      <!--D層的的註解-->
      
      <!--B層的的註解-->
      <object id="CalendarBll" type="ITOO.BasicChooseManager.BLL.CalendarBll,ITOO.BasicChooseManager.BLL" singleton="false" />

      <object id="CreateCourseBll" type="ITOO.BasicChooseManager.BLL.CreateCourseBll,ITOO.BasicChooseManager.BLL" singleton="false" />

      <object id="CourseBll" type="ITOO.BasicChooseManager.BLL.CourseBll,ITOO.BasicChooseManager.BLL" singleton="false" />
      <object id="RoundBll" type="ITOO.BasicChooseManager.BLL.RoundBll,ITOO.BasicChooseManager.BLL" singleton="false" />
  
      <object id="RoundCourseBll" type="ITOO.BasicChooseManager.BLL.RoundCourseBll,ITOO.BasicChooseManager.BLL" singleton="false" />
    
      <object id="GetServiceBll" type="ITOO.BasicChooseManager.BLL.GetServiceBll,ITOO.BasicChooseManager.BLL" singleton="false" />
      <object id="RoundGradeCollegeBll" type="ITOO.BasicChooseManager.BLL.RoundGradeCollegeBll,ITOO.BasicChooseManager.BLL" singleton="false" />
      <object id="CourseCollegeTreeBll" type="ITOO.BasicChooseManager.BLL.CourseCollegeTreeBll,ITOO.BasicChooseManager.BLL" singleton="false" />
      <object id="CouldChooseCourseBll" type="ITOO.BasicChooseManager.BLL.CouldChooseCourseBll,ITOO.BasicChooseManager.BLL" singleton="false" />
      <object id="ChoosedCourseBll" type="ITOO.BasicChooseManager.BLL.ChoosedCourseBll,ITOO.BasicChooseManager.BLL" singleton="false" />
      
      <object id="GradeLevelBll" type="ITOO.BasicChooseManager.BLL.GradeLevelBll,ITOO.BasicChooseManager.BLL" singleton="false" />
      <object id="CourseRoundGradeCollegesBll" type="ITOO.BasicChooseManager.BLL.CourseRoundGradeCollegesBll,ITOO.BasicChooseManager.BLL" singleton="false" />
      <!--B層的的註解-->
    </objects>


IOC的優點是,讓例項化變的更加簡單,程式碼簡潔,可配置,是通過工廠建立介面,獲得具體實現類的升級版。




也可以分成三部分MVC,View-Controller-Model(WCF+B+D+EF)。

View主要負責前臺頁面的顯示,

Controller從檢視讀取資料,控制使用者輸入,並向模型傳送資料,

Model就是WCF以及往下的所有的程式碼,主要用來處理程式資料邏輯部分。

架構主要通過工廠.介面,來獲得WCF的例項;或者通過IOC容器,建立B/D/DbSession的例項。B繼承BaseService,所以每個B繼承DbSession,這樣B就通過IOC,型別為IDbSession,獲取DbSession的例項;同理,再通過IOC獲得DbSession獲得DAL的例項。

ViewModel是頁面實體,Model是資料實體。頁面實體對應頁面欄位,資料實體對應資料庫實體。




相關文章