生活随笔
收集整理的這篇文章主要介紹了
转载CSDN (MVC WebAPI 三层分布式框架开发)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
前言:SOA(面向服務(wù)的架構(gòu))是目前企業(yè)應(yīng)用開發(fā)過程中普遍采用的技術(shù),基于MVC WebAPI三層分布式框架開發(fā),以此適用于企業(yè)信息系統(tǒng)的業(yè)務(wù)處理,是本文論述的重點(diǎn)。此外,插件技術(shù)的應(yīng)用,富客戶端JQuery實(shí)現(xiàn)技術(shù),本文也對其具體實(shí)現(xiàn)做以說明。相關(guān)示例解決方案可以參考GitHub資源,在文章結(jié)尾給出。
?
1.?系統(tǒng)分層體系架構(gòu)設(shè)計(jì)
?
分布式三層系統(tǒng)簡單分為數(shù)據(jù)訪問層,業(yè)務(wù)邏輯層和前端展現(xiàn)層。分層架構(gòu)設(shè)計(jì)是構(gòu)建大型分布式系統(tǒng)的必要手段,因?yàn)榭梢允沟孟到y(tǒng)健壯,可擴(kuò)展。
?
SOA即面向服務(wù)的架構(gòu),可以幫助企業(yè)構(gòu)建靈活,擴(kuò)展性強(qiáng)的復(fù)雜業(yè)務(wù)系統(tǒng),按照服務(wù)的理念進(jìn)行功能交付,調(diào)用方也不用去知道實(shí)現(xiàn)一方的具體細(xì)節(jié);雙方是按照統(tǒng)一消息格式,接口方式進(jìn)行交互的。
SOA的實(shí)現(xiàn)是基于以Web服務(wù)的方式發(fā)布Api接口,目前WebAPI是一種Restfule形式的Web服務(wù),相比WCF的復(fù)雜性,WebAPI的開發(fā)效率更高,而且在配置時(shí)也不需要客戶端和服務(wù)端的xml配置。
企業(yè)核心業(yè)務(wù)數(shù)據(jù)可以讓桌面、Web、平板、手機(jī)或物聯(lián)設(shè)備訪問,所以需要統(tǒng)一API接口,WebApi作為業(yè)務(wù)邏輯處理服務(wù)能夠滿足接口訪問和接口之間交互的需求。
2.基礎(chǔ)類庫模塊 2.1 數(shù)據(jù)訪問:Dapper-微型ORMapping框架 Dapper的優(yōu)勢: 1,Dapper是一個(gè)輕型的ORM類。代碼就一個(gè)SqlMapper.cs文件,編譯后就40K的一個(gè)很小的Dll. 2,Dapper很快。Dapper的速度接近與IDataReader,取列表的數(shù)據(jù)超過了DataTable。 3,Dapper支持Mysql,SqlLite,Mssql,Oracle等一系列的數(shù)據(jù)庫,當(dāng)然如果你知道原理也可以讓它支持Mongo db 4,Dapper的r支持多表并聯(lián)的對象。支持一對多 多對多的關(guān)系。并且沒侵入性,想用就用,不想用就不用。無XML無屬性。代碼以前怎么寫現(xiàn)在還怎么寫。 5,Dapper原理通過Emit反射IDataReader的序列隊(duì)列,來快速的得到和產(chǎn)生對象。性能實(shí)在高。 6,Dapper 是C#實(shí)現(xiàn),支持.net framework 各種版本;
7,Dapper語法十分簡單。并且無須遷就數(shù)據(jù)庫的設(shè)計(jì)。
?
?
國外大型網(wǎng)站采用的有: –StackOverflow,?StackExcahnge等。。。
?
讀取500條記錄,并做簡單對象的序列化操作時(shí)間對比如下圖:
?
2.2 DataRepository類
?
?實(shí)現(xiàn)數(shù)據(jù)實(shí)體操作封裝 -Insert—插入 -Update—更新 -Delete—?jiǎng)h除 -Select—選取 -Paged—分頁 2.3 ?ServiceBase類 ?實(shí)現(xiàn)業(yè)務(wù)實(shí)體對象的操作封裝 –Insert—插入 –Update—更新 –Delete—?jiǎng)h除 –Select—選取 –Paged—分頁 ?
2.4 服務(wù)實(shí)現(xiàn)類
?
-實(shí)現(xiàn)Iservice接口 -繼承ServiceBase基類 2.5 WebAPI服務(wù)發(fā)布 API Controller:
??? --[HttpGet]
??? --[HttpPost]
??? --[HttpPut]
??? --[HttpDelete]
2.6 動(dòng)態(tài)加載插件 -系統(tǒng)的擴(kuò)展性 -系統(tǒng)的變化性 -客戶二次開發(fā) -MEF –運(yùn)行時(shí)加載 2.7 AutoMapper—實(shí)體對象之間轉(zhuǎn)換 ?兩個(gè)實(shí)體類 –EPProduct –?數(shù)據(jù)實(shí)體 –Product–?業(yè)務(wù)實(shí)體 ?轉(zhuǎn)化示例代碼 –EPProduct?p =ProductRepository.Get(long.Parse(id)); –AutoMapper.Mapper.CreateMap<EPProduct, Product>(); –Productentity =AutoMapper.Mapper.Map<EPProduct, Product>(p) 2.8 面向接口編程--Ioc框架 ?SimpleInjector –靜態(tài)類型 –編譯階段 ?MEF –動(dòng)態(tài)類型 –運(yùn)行時(shí)階段 3.富客戶端開發(fā) 3.1 Asp.NETMVC?富客戶端開發(fā) ?Model –WebAPI (服務(wù)接口) ?Controller –路由 ?View –頁面 – ?富客戶端 –Ajax 局部刷新 – 鼠標(biāo)、鍵盤響應(yīng)事件等 –如Gmail郵箱等應(yīng)用示例 3.2?Jquery插件 ?Layout—Jquery Layout ?DataGrid – SlickGrid –性能非常高 ?Tree– Jstree/Ztree –評價(jià)都不錯(cuò) ?Tab– Jquery Tools ?Toolbar– Jquery Tools ?Dialog– Jquery Tools ?Form–Jquery Tools 3.3?前端頁面Ajax調(diào)用: GET/POST/PUT/DELETE [javascript]?view plaincopy
?????????$.doHttpClientGet?=?function(url,?fn)?{?????????$.getJSON(url,?fn);?????}??????????????????$.doHttpClientUpdate?=?function(url,?data,?fn)?{?????????$.ajax({?????????????url:?url,?????????????type:?'PUT',?????????????data:?data,?????????????dataType:?'json',?????????????contentType:?'application/json',?????????????success:?fn?????????});?????}??????????????????$.doHttpClientDelete?=?function(url,?data,?fn)?{?????????$.ajax({?????????????url:?url,?????????????type:?'DELETE',?????????????data:?data,?????????????dataType:?'json',?????????????contentType:?'application/json',?????????????success:?fn?????????});?????}??????????????????$.doHttpClientSave?=?function(url,?data,?fn)?{?????????$.ajax({?????????????url:?url,?????????????type:?'POST',?????????????data:?data,?????????????dataType:?'json',?????????????contentType:?'application/json',?????????????success:?fn?????????});?????}??????????????????$.doAjaxGet?=?function(url,?fn)?{??????????????????$.ajax({?????????????url:?url,?????????????type:?"GET",?????????????dataType:?'json',??????????????????????????contentType:?'application/json',?????????????success:?fn?????????});?????}?????????$.doAjaxPost?=?function(url,?data,?fn)?{?????????$.ajax({?????????????url:?url,?????????????type:?'POST',?????????????data:?data,?????????????dataType:?'json',?????????????contentType:?'application/json',?????????????success:?fn?????????});?????}??????????????$.buildHTML?=?function(tag,?html,?attrs)?{??????????????????if?(typeof?(html)?!=?'string')?{?????????????attrs?=?html;?????????????html?=?null;?????????}?????????var?h?=?'<'?+?tag;?????????for?(attr?in?attrs)?{?????????????if?(attrs[attr]?===?false)?continue;?????????????h?+=?'?'?+?attr?+?'="'?+?attrs[attr]?+?'"';?????????}?????????return?h?+=?html???">"?+?html?+?"</"?+?tag?+?">"?:?"/>";?????}??????????????$.fn.buildJsTree?=?function?(url,?fn)?{?????????var?object?=?require(['jstree'],?function(){?????????????$.jstree._themes?=?"/PlatJS/Scripts/jstree/themes/";?????????????var?myTree?=?$(this).jstree({?????????????????"json_data":?{?????????????????????"ajax":?{?????????????????????????"url":?url,?????????????????????????"type":?"GET",?????????????????????????"dataType":?"json",?????????????????????????"contentType":?"application/json?charset=utf-8",?????????????????????????"success":?fn?????????????????????}?????????????????},?????????????????"plugins":?["themes",?"json_data",?"ui"]?????????????});?????????})?????}?? 3.4?如何調(diào)試? ?Fiddler--
*****5star FireBug?for Firefox
?查看HTML,CSS,Javascript等 ?監(jiān)控下載圖片資源時(shí)間線 ?完善友好的調(diào)試 Firefox的RestClient插件—Rest Client測試插件
http://localhost:8081/ProductSys.WebAPI/api/order/insertwith?type="insertwith“
?[HttpPost]
public?HttpResponseMessageInsertWith(Order entity, string type)
http://localhost:8081/ProductSys.WebAPI/api/order/4
?[HttpDelete]
?public?HttpResponseMessage?Delete(string id)
?
?
3.5 Web異常錯(cuò)誤代碼
?
?100-199– Informational ?200-299– Client request successful ?300-399– Client request redirected, further action necessary ?400-499– Client request incomplete ?500-599– Server error 4.?Javascript 類語法 4.1 常見問題 ?Namespace(命名空間) –默認(rèn)為全局范圍,有潛在類型沖突隱患 ?SelfExecuting Fuction (自執(zhí)行匿名函數(shù)) ?Objectand Array (對象和數(shù)組初始化) –不要使用new?關(guān)鍵字 ?NullOr Empty (檢查NULL) ?
4.2 Javascript-自執(zhí)行匿名函數(shù) [javascript]?view plaincopy
??(function(?skillet,?$,?undefined?)?{????????????var?isHot?=?true;???????????????skillet.ingredient?=?"Bacon?Strips";???????????????????skillet.fry?=?function()?{??????????var?oliveOil;?????????????????????addItem(?"\t\n?Butter?\n\t"?);??????????addItem(?oliveOil?);??????????console.log(?"Frying?"?+?skillet.ingredient?);??????};???????????????????function?addItem(?item?)?{??????????if?(?item?!==?undefined?)?{??????????????console.log(?"Adding?"?+?$.trim(item)?);??????????}??????}??????}(?window.skillet?=?window.skillet?||?{},?jQuery?));???<pre?name="code"?class="javascript">??console.log(?skillet.ingredient?);????????skillet.fry();????????skillet.quantity?=?"12";??console.log(?skillet.quantity?);????????(function(?skillet,?$,?undefined?)?{????????????var?amountOfGrease?=?"1?Cup";???????????????????skillet.toString?=?function()?{??????????console.log(?skillet.quantity?+?"?"?+????????????????????????skillet.ingredient?+?"?&?"?+????????????????????????amountOfGrease?+?"?of?Grease"?);??????????console.log(?isHot???"Hot"?:?"Cold"?);??????};??????}(?window.skillet?=?window.skillet?||?{},?jQuery?));?????try?{????????????skillet.toString();???}?catch(?e?)?{??????console.log(?e.message?);???}</pre><pre?name="code"?class="javascript"></pre>?? 4.3 對象和數(shù)組初始化 [javascript]?view plaincopy
??var?person?=?{},???????keys?=?[];??????var?person?=?{??????????firstName:?"Elijah",??????????lastName:?"Manor",??????????sayFullName:?function()?{??????????????console.log(?this.firstName?+?"?"?+???????????????????this.lastName?);??????????}??????},???????keys?=?["123",?"676",?"242",?"4e3"];?? 4.4 判斷對象是否為NULL(c#) [csharp]?view plaincopy
??if?(?someString?!=?null?&&??????someString.length?>?0?)?{????????}??????if?(?!string.IsNullOrEmpty(someString)?)?{????????}?? 4.5 判斷對象是否為NULL(javascript) [javascript]?view plaincopy
Javascript中的正確寫法??????if?(?someString?)?{????????}?? 4.6 設(shè)置缺省值(c#) [csharp]?view plaincopy
<span?style="color:?rgb(255,?0,?0);">??if?(?someString?==?null?)?{?????someString?=?"default?Value";??}????someString?=?someString???someString?:?"default?value";?<pre?name="code"?class="javascript">請?jiān)贘avascript按如下格式寫????someString?=?someString?||?"default?value";??</pre><br>??<pre></pre>??<pre></pre>??<pre></pre>??<pre></pre>??<pre></pre>??<pre></pre>??<pre></pre>??<pre></pre>??<pre></pre>??<pre></pre>??<pre></pre>?? 4.7 不同類型的比較操作符(==, !=) ?// Unexpected Comparisons using the== Operator ?0?????????==? ''??????? //true ?0?????????==? '0'?????? //true ?false?????==? '0'?????? //true ?null??????==? undefined //true ?'\t\r\n ' ==? 0???????? //true 4.8?不同類型的比較操作符(===, !==) ?// Expected Comparisons using the ===Operator ?0?????????=== ''??????? //false ?0?????????=== '0'?????? //false ?false?????=== '0'?????? //false ?null??????=== undefined //false ?'\t\r\n ' === 0???????? //false 4.9?不可取的數(shù)組遍歷操作符for…in [csharp]?view plaincopy
var?myArray?=?[],?name;??myArray[5]?=?"test";??console.log(?myArray.length?);??????for?(?name?in?myArray?)?{??????console.log(?name,?myArray[name]?);??????????????}?? 4.10 正確的數(shù)組遍歷操作符for…;…;
[javascript]?view plaincopy
var?myArray?=?[],?name;??myArray[5]?=?"test";??console.log(?myArray.length?);??????for?(?var?i?=?0,?length?=?myArray.length;?i?<?length;?i++?)?{??????console.log(?i,?myArray[i]?);????????????????????????????????????????????}?? [javascript]?view plaincopy
for?(?var?name?in?object?)?{????????}????????for?(?var?name?in?object?)?{?????if?(?object.hasOwnProperty(name)?)?{?????????????}??}?? 5. RequireJS 模塊化腳本 RequireJS 是一個(gè)非常小巧的 JavaScript 模塊載入框架,是 AMD 規(guī)范最好的實(shí)現(xiàn)者之一。最新版本的 RequireJS 壓縮后只有 14K,堪稱非常輕量。它還同時(shí)可以和其他的框架協(xié)同工作,使用 RequireJS 必將使的前端代碼質(zhì)量得以提升。 RequireJS 作為 JavaScript 文件的加載器,還是可以完成異步非阻塞的文件加載。 [javascript]?view plaincopy
define(['Controllers/Main/ListView'],?function?(ListView)?{??????function?start()?{??????????var?users?=?JSON.parse(localStorage.users);??????????ListView.render({?users:?users?});??????}????????return?{??????????start:?start??????};??});?? 6. 網(wǎng)絡(luò)資源 6.1 NuGet—快捷獲取軟件包 ?充分利用開源軟件包,避免重復(fù)制造輪子; ?也可以自己發(fā)布軟件包,回饋社區(qū),先進(jìn)技術(shù)的積累可以節(jié)約項(xiàng)目成本。 6.2 技術(shù)資源 ?Asp.net MVC WebAPI –RestfulWeb Service的發(fā)展 ?Jquery –官網(wǎng) –插件開發(fā) ?RequrieJS –Javascript模塊化開發(fā)框架 ?Dapper –微型ORMapping 框架 ?EntityFramework –Microsoft實(shí)體框架 7. 總結(jié): 本文基于MVC WebAPI實(shí)現(xiàn)分布式三層架構(gòu),實(shí)現(xiàn)了通用數(shù)據(jù)訪問操作,業(yè)務(wù)實(shí)體和數(shù)據(jù)實(shí)體的交互操作,業(yè)務(wù)模塊之間的接口交互;實(shí)現(xiàn)了插件化的加載技術(shù)。此外限于篇幅,對于流程化的設(shè)計(jì),會(huì)在下文論述,主要會(huì)談及到工作流Api和WebApi的交互。 代碼示例說明:
提供的示例RequireMVC199中,可以看一下ProductSys.WebApi的服務(wù)層代碼,前端代碼看RequireMvc199的WebApplication項(xiàng)目即可。
?
完整示例,可以看一下ProductList頁面的代碼,這個(gè)示例是完整的,包括文件:
WebApplication 包括:
\Controllers
? ? --ProductController.cs
\ViewJS
? \Controllers
? ? \Product
? ? ? ?--product-list.js
? ? ? --product-detail.js
?
\Views
? \Product
? ? --productlist.cshtml
?
WebApi 包括:
ProductSys.WebApi
? \Controllers
? ? --ProductController.cs
?
ProductSys.ServiceImp
? \Service
? ? --ProductService.cs
解決方案下載地址: https://github.com/lgsky/Plat2012
轉(zhuǎn)載于:https://www.cnblogs.com/johnwood/p/3456029.html
總結(jié)
以上是生活随笔為你收集整理的转载CSDN (MVC WebAPI 三层分布式框架开发)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。