自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

分享一個Mvc的多層架構,歡迎大家拍磚斧正

開發(fā) 架構
隨之Web2.0的到來單一頁面展示的信息也更加的豐富,Ajax,js的流行也使得Ui端的操作也愈加變重,于是大家有期望以一種工程的思想去擁 抱這種變化,于是MVVM,js的Mvc框架陸續(xù)出現(xiàn)。同時隨著移動互聯(lián)網的興起,不同終端對于系統(tǒng)的對接也非常重要,于是我們考慮在Ui與Logic之 間引入Application或Service層應對不同終端配置。

多層架構是什么?

多層架構是開發(fā)人員在開發(fā)過程當中面對復雜且易變的需求采取的一種以隔離控制為主的應對策略,關于多層架構的標準,我認為有一句話是比較有代表性的“每一層都可以單獨部署”,最傳統(tǒng),最簡單的就是從三層開始的:

 

 

將整個項目自下而上的分為:數(shù)據(jù)持久(數(shù)據(jù)訪問)層,邏輯(業(yè)務)層,UI(展現(xiàn))層。

數(shù)據(jù)訪問層:負責將數(shù)據(jù)持久化響應的數(shù)據(jù)存儲設備上,如DataBase,Txt,Excel等。

業(yè)務邏輯層:負責處理為滿足軟件需求而訂制的一系列的邏輯與業(yè)務,如用戶在前端下訂單之后,整個業(yè)務流可能涉及 到,獲取用戶信息,獲取商品信息,獲取購物車信息,驗證商品可購買數(shù)量是否滿足本次購買,針對用戶身份產生不同的優(yōu)惠策略,同時會驗證 Cookie,Session等端產生數(shù)據(jù)的有效性,最終才會產生訂單,而訂單產生之后會涉及到倉儲物流等一系列的Erp系統(tǒng)業(yè)務,所有的這一套都屬于“下訂單”這一需求的業(yè)務邏輯。

展示層:負責與用戶交互的界面,良好的用戶體驗多是使用在這里。

學習過Petshop的話,對于三層都不會陌生:

 

 

但是隨著業(yè)務的復雜每一層都會有自己的進化,最終有了無數(shù)附加在三層之上的框架與開發(fā)思想。

Mvc與MVP:

首先我一直認為這兩種事屬于展現(xiàn)層的,“展現(xiàn)層MCV”,“展現(xiàn)層MVP”。

然后我們站在展現(xiàn)層的角度思考一下“Mvc”與“MVP”。

Mvc:分為model,Controller,View,相信大家對于他已經很熟悉了,在此不再累述。

 

 

MVP:MVP有Model-Presenter-View三個層次

 

 

其實在樓主最開始接觸Mvc的時候,就在想如果直接通過Controller與Model交互是不是顯得有一些“不干凈”,因為在樓主眼里“展現(xiàn)層 的Controller”,做得最多的應該就是對于請求路由的不同響應與調用,但是很多的例子會將一些數(shù)據(jù)驗證,去糟的操作過程放在Controller 中,顯得不倫不類。當MVP出現(xiàn)的時候,一切滿足了樓主的幻想,P的過程就是滿足了這一需求,P起到中介的作用,負責接收視圖請求,再把結果映射到 view上,P可以不對View做強引用,可通過IView適配多個view。當然我也會在這里做一些針對于終端數(shù)據(jù)的驗證與過濾。

業(yè)務邏輯:

從描述上可以看的很清楚,整個自上而下的結構,最復雜,最可能失控的就是業(yè)務邏輯層,因為其中包含著許多的不可控因素,每個行業(yè)領域的需求都有可能包含自身的領域知識。于是在之后的多層架構發(fā)展構成當中,更多的變化與智慧是體現(xiàn)在這里。

領域驅動:限于本人才學不能在這里分享太多,以防誤導大家,想了解更多可參考園子里的其他大牛,其實沒有3,5 年相關經驗是很難理解的,個人感覺如果你不理解的話也不會對你有什么影響,因為領域驅動是建立在良好的面相對象分析,邊界劃分基礎之上的,在學習的過程當 中已經能幫助你去學習到足夠多的知識了,最終到不到山巔其實已經無所謂了。

簡單的說,這個思想最重要的是以業(yè)務領域為核心進行發(fā)散,期望在變更程序的其他部分,不會影響到領域模型,也就是那句話為了“復雜的系統(tǒng)應用程序中業(yè)務規(guī)則行為方式(就是“領域邏輯”)是會經常變化的,我們要去擁抱這種變化”。結構圖:

 

 

CQRS:是指命令查詢職責的分離,是一個小的模式形態(tài),該模式的關鍵在于:“一個方法要么是用來改變某個對象的狀態(tài)的,要么就是返回一個結果,這兩者不會同時并存”。將整個系統(tǒng)分拆為兩個部分:

  • Commands(命令) - 改變某一個對象或整個系統(tǒng)的狀態(tài)(有時也叫做modifiers或者mutators)。

  • Queries(查詢) - 返回值并且不會改變對象的狀態(tài)。

架構圖:

 

 

不管DDD也好,CQRS也好,其實這兩種都不會100%適合所有的項目架構的,這就需要架構師結合項目本身特點及需求有所選擇,但是其中的思想我們可以運用在項目的任何地方。

基于消息的分布式:

其實不管使用怎樣的架構,加入怎樣的架構思想(soa),核心或者是開發(fā)者最想達到的就是層次,系統(tǒng)之間的解耦,復雜的東西沒人會喜歡。

隨著系統(tǒng)的發(fā)展,我們的程序會涉及到多臺服務器,多種終端,同時為了解耦我們引入了基于消息的分布式架構。

首先,所以系統(tǒng)的通信基于消息,邏輯聯(lián)系不會涉及到具體的業(yè)務實現(xiàn),同時消息的傳遞更加的廉價可適配多種終端。

其次,由于所用邏輯只是基于消息實現(xiàn),迭代的成本也會相對于其他耦合項目更快更方便。

 

 

 

展示層:

隨之Web2.0的到來單一頁面展示的信息也更加的豐富,Ajax,js的流行也使得Ui端的操作也愈加變重,于是大家有期望以一種工程的思想去擁 抱這種變化,于是MVVM,js的Mvc框架陸續(xù)出現(xiàn)。同時隨著移動互聯(lián)網的興起,不同終端對于系統(tǒng)的對接也非常重要,于是我們考慮在Ui與Logic之 間引入Application或Service層應對不同終端配置。

 

 

如:我們在Client Presenter Layer 上加入WCF適配多種終端提交的訂單,都是建立在消息基礎之上的,樓主之前做電商系統(tǒng)是針對于來自淘寶,天貓,亞馬遜訂單時,為避免出現(xiàn)對庫中訂單并發(fā), 產生“超買”情況,采用了在上層Ui與logic層之間引入了OrderChannel層,將不同終端訂單進行排隊的解決方案。 

以上是架設一個能夠適配不同需求的架構過程,但是真正的真理是需要大家在實踐中,錯誤中汲取的。

下面是樓主簡單的小分層架構,不妥,不足之處希望大家指導斧正。

層次劃分:

 

 

為了實現(xiàn)單獨部署,層次解耦所以層次之間是基于接口實現(xiàn)的。

DataAccess層引入倉儲實現(xiàn)統(tǒng)一DTO操作,實現(xiàn)基于Ef:

IRepository:

 

  1. public interface IRepository<T> where T:class 
  2.    { 
  3.        IEnumerable<T> FindAll(Expression<Func<T,bool>> exp); 
  4.        void Add(T entity); 
  5.        void Delete(T entity); 
  6.        void Submit(); 
  7.    } 

 

引入RepositoryBase實現(xiàn)接口定義:

  1. public class RepositoryBase<T>:IRepository<T> where T:class 
  2.     { 
  3.         DbContext context; 
  4.         public RepositoryBase(DbContext _context) 
  5.         { 
  6.             context = _context; 
  7.         } 
  8.  
  9.         public RepositoryBase() { 
  10.             this.context = new TestDBEntities(); 
  11.         } 
  12.  
  13.         public IEnumerable<T> FindAll(Expression<Func<T, bool>> exp) 
  14.         { 
  15.             return context.Set<T>().Where(exp); 
  16.         } 
  17.  
  18.         public void Add(T entity) 
  19.         { 
  20.             context.Set<T>().Add(entity); 
  21.         } 
  22.  
  23.         public void Delete(T entity) 
  24.         { 
  25.             context.Set<T>().Remove(entity); 
  26.         } 
  27.  
  28.         public void Submit() 
  29.         { 
  30.             context.SaveChanges(); 
  31.         } 
  32.     } 

這對于單一的某個倉儲我們單獨引入其自身的倉儲接口:

  1. public interface IUserRepository:IRepository<UserTest> 
  2.     { 
  3.         IList<UserTest> GetAllById(int id); 
  4.  
  5.         bool CheckUserExist(UserTest u); 
  6.     } 

 

特定倉儲實現(xiàn):

  1. public class UserRepository : RepositoryBase<UserTest>,IUserRepository 
  2.     { 
  3.         public IList<UserTest> GetAllById(int id) 
  4.         { 
  5.             using (TestDBEntities entities=new TestDBEntities()) 
  6.             { 
  7.                 var users = from u in entities.UserTests 
  8.                             where u.ID == id 
  9.                             select u; 
  10.                 return users.ToList(); 
  11.             } 
  12.         } 
  13.  
  14.         public bool CheckUserExist(UserTest u) 
  15.         { 
  16.             using (TestDBEntities entities = new TestDBEntities()) 
  17.             { 
  18.                 List<UserTest> users = entities.UserTests.Where(ut => ut.UserName == u.UserName && ut.UserPassword==u.UserPassword).ToList<UserTest>(); 
  19.                 return users.Count==0 ? false : true
  20.             } 
  21.         } 
  22.     } 

在Service層同樣建立相關接口適配特種服務:

IUserCore:

  1. public interface IUserCore 
  2.     { 
  3.         CommandStatueEnum UserLogin(IModel model); 
  4.         CommandStatueEnum UserRegister(IModel model); 
  5.  
  6.         List<UserTest> GetUsers(Expression<Func<UserTest, bool>> expr); 
  7.     } 

UserCore:

  1. public class UserCore : IUserCore 
  2.     { 
  3.         #region Structure 
  4.         IUserRepository _repository; 
  5.         public UserCore(IUserRepository repository) { 
  6.             this._repository = repository; 
  7.         } 
  8.         #endregion 
  9.  
  10.         public CommandStatueEnum UserLogin(IModel model) 
  11.         { 
  12.             try 
  13.             { 
  14.                 UserLogin u = model as UserLogin; 
  15.                 UserTest uTest = new UserTest(); 
  16.                 uTest.UserName = u.UserName; 
  17.                 uTest.UserPassword = u.Password; 
  18.  
  19.                 if (_repository.CheckUserExist(uTest)) 
  20.                 { 
  21.                     return CommandStatueEnum.Succeed; 
  22.                 } 
  23.                 else 
  24.                 { 
  25.                     return CommandStatueEnum.Fail; 
  26.                 } 
  27.             } 
  28.             catch (Exception ex) { 
  29.                 throw ex; 
  30.             } 
  31.         } 
  32.  
  33.         public CommandStatueEnum UserRegister(IModel model) 
  34.         { 
  35.             try 
  36.             { 
  37.                 UserLogin u = model as UserLogin; 
  38.                 UserTest uTest = new UserTest() { UserName=u.UserName, UserPassword=u.Password}; 
  39.                 _repository.Add(uTest); 
  40.                 _repository.Submit(); 
  41.                 return CommandStatueEnum.Succeed; 
  42.             } 
  43.             catch (Exception ex) 
  44.             { 
  45.                 throw ex; 
  46.             } 
  47.         } 
  48.  
  49.  
  50.         public List<UserTest> GetUsers(System.Linq.Expressions.Expression<Func<UserTest, bool>> expr=null
  51.         { 
  52.            return _repository.FindAll(expr).ToList<UserTest>(); 
  53.         } 
  54.     } 

Controller:

  1. public class AccountController : Controller 
  2.     { 
  3.         IUserCore userCore; 
  4.         public AccountController(IUserCore _userCore) 
  5.         { 
  6.             this.userCore = _userCore; 
  7.         } 
  8.  
  9.         // 
  10.         // GET: /Account/ 
  11.  
  12.         #region view 
  13.         public ActionResult Home() 
  14.         { 
  15.             ViewBag.Users = userCore.GetUsers(u=>u.IsUse==1); 
  16.             return View(); 
  17.         } 
  18.  
  19.         public ActionResult Login() 
  20.         { 
  21.             return View(); 
  22.         } 
  23.  
  24.         public ActionResult Register() 
  25.         { 
  26.             return View(); 
  27.         } 
  28.         #endregion 
  29.  
  30.         #region Post 
  31.         [HttpPost] 
  32.         public ActionResult Login(UserLogin account) 
  33.         { 
  34.             try 
  35.             { 
  36.                 if (userCore.UserLogin(account) == CommandStatueEnum.Succeed) 
  37.                 { 
  38.                     return RedirectToAction("Home"); 
  39.                 } 
  40.                 else 
  41.                 { 
  42.                     return View(); 
  43.                 } 
  44.             } 
  45.             catch (Exception ex) 
  46.             { 
  47.                 ExceptionModel.IsExcept = true
  48.                 ExceptionModel.Exception = ex.ToString(); 
  49.                 ExceptionModel.CreateTime = DateTime.Now; 
  50.                 return View(); 
  51.             } 
  52.         } 
  53.  
  54.         [HttpPost] 
  55.         public ActionResult Register(UserLogin account) 
  56.         { 
  57.             try 
  58.             { 
  59.                 if (userCore.UserRegister(account) == CommandStatueEnum.Succeed) 
  60.                 { 
  61.                     return RedirectToAction("Home"); 
  62.                 } 
  63.                 else 
  64.                 { 
  65.                     return View(); 
  66.                 } 
  67.             } 
  68.             catch (Exception ex) 
  69.             { 
  70.                 ExceptionModel.IsExcept = true
  71.                 ExceptionModel.Exception = ex.ToString(); 
  72.                 ExceptionModel.CreateTime = DateTime.Now; 
  73.                 return View(); 
  74.             } 
  75.         } 
  76.         #endregion 
  77.     } 

對于接口之間我們通過引入IOC工具解耦:

  1. public class MvcApplication : System.Web.HttpApplication 
  2.     { 
  3.         protected void Application_Start() 
  4.         { 
  5.             AreaRegistration.RegisterAllAreas(); 
  6.  
  7.             WebApiConfig.Register(GlobalConfiguration.Configuration); 
  8.             FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); 
  9.             RouteConfig.RegisterRoutes(RouteTable.Routes); 
  10.             BundleConfig.RegisterBundles(BundleTable.Bundles); 
  11.             AuthConfig.RegisterAuth(); 
  12.  
  13.             #region IOC 
  14.             var builder = new ContainerBuilder(); 
  15.             SetupResolveRules(builder); 
  16.             builder.RegisterControllers(Assembly.GetExecutingAssembly()); 
  17.             var container = builder.Build(); 
  18.             DependencyResolver.SetResolver(new AutofacDependencyResolver(container)); 
  19.             #endregion 
  20.         } 
  21.  
  22.         private void SetupResolveRules(ContainerBuilder builder) 
  23.         { 
  24.             //Components are wired to services using the As() methods on ContainerBuilder 
  25.             builder.RegisterType<UserCore>().As<IUserCore>(); 
  26.             builder.RegisterType<UserRepository>().As<IUserRepository>(); 
  27.         } 
  28.  
  29.     } 

其他基礎類庫我們會結合具體需求進行定制,上面例子多有不妥之處只起演示之用。

綜上所述,本篇只起拋磚引玉之用,還請大牛拍磚指導。

 

責任編輯:王雪燕 來源: 博客園
相關推薦

2014-04-02 10:00:51

MVCMVC架構多層架構

2023-02-26 01:02:22

2024-02-07 08:52:07

VueUI 庫Vue.js

2021-03-18 08:04:54

AQS工具CAS

2014-07-08 09:27:24

SQLSERVER腳本

2023-01-03 12:30:25

架構CPUGPU

2023-11-28 14:22:54

Python音頻

2020-03-06 18:43:58

Spring MVC控制器Java

2020-07-27 07:53:36

高并發(fā)流量系統(tǒng)

2010-03-02 16:33:14

linux數(shù)據(jù)備份

2018-11-16 17:00:05

Python腳本數(shù)據(jù)分析

2022-05-26 08:12:52

debugSpringFeign

2019-08-02 15:39:06

PythonLinuxJSON

2023-04-28 12:01:56

Spring項目編譯

2018-11-22 14:09:45

iOS架構組件開發(fā)

2022-05-09 11:01:18

配置文件數(shù)據(jù)庫

2012-08-27 10:11:43

ASP.NET

2010-01-26 13:15:42

ASP.NET MVC

2011-06-02 10:23:33

程序員

2022-12-28 12:29:45

duf命令
點贊
收藏

51CTO技術棧公眾號