前言:SOA(面向服務的架構)是目前企業應用開發過程中普遍採用的技術,基於MVC WebAPI三層分佈式框架開發,以此適用於企業信息系統的業務處理,是本文論述的重點。此外,插件技術的應用,富客戶端JQuery實現技術,本文也對其具體實現做以說明。相關示例解決方案可以參考GitHub資源,在文章結尾給出。
1. 系統分層體系架構設計
分佈式三層系統簡單分爲數據訪問層,業務邏輯層和前端展現層。分層架構設計是構建大型分佈式系統的必要手段,因爲可以使得系統健壯,可擴展。
SOA即面向服務的架構,可以幫助企業構建靈活,擴展性強的複雜業務系統,按照服務的理念進行功能交付,調用方也不用去知道實現一方的具體細節;雙方是按照統一消息格式,接口方式進行交互的。
SOA的實現是基於以Web服務的方式發佈Api接口,目前WebAPI是一種Restfule形式的Web服務,相比WCF的複雜性,WebAPI的開發效率更高,而且在配置時也不需要客戶端和服務端的xml配置。
企業核心業務數據可以讓桌面、Web、平板、手機或物聯設備訪問,所以需要統一API接口,WebApi作爲業務邏輯處理服務能夠滿足接口訪問和接口之間交互的需求。
1,Dapper是一個輕型的ORM類。代碼就一個SqlMapper.cs文件,編譯後就40K的一個很小的Dll.
7,Dapper語法十分簡單。並且無須遷就數據庫的設計。
讀取500條記錄,並做簡單對象的序列化操作時間對比如下圖:
2.2 DataRepository類
API Controller:
--[HttpGet]
--[HttpPost]
--[HttpPut]
--[HttpDelete]
GET/POST/PUT/DELETE
/***
* HttpGet獲取服務端數據
* @url 業務數據
* @data
*/
$.doHttpClientGet = function(url, fn) {
$.getJSON(url, fn);
}
/***
* HttpPut更新數據到服務端
* @url 業務數據
* @data
*/
$.doHttpClientUpdate = function(url, data, fn) {
$.ajax({
url: url,
type: 'PUT',
data: data,
dataType: 'json',
contentType: 'application/json',
success: fn
});
}
/***
* HttpDelete刪除數據
* @url 業務數據
* @data
*/
$.doHttpClientDelete = function(url, data, fn) {
$.ajax({
url: url,
type: 'DELETE',
data: data,
dataType: 'json',
contentType: 'application/json',
success: fn
});
}
/***
* HttpPost保存數據
* @url 業務數據
* @data
*/
$.doHttpClientSave = function(url, data, fn) {
$.ajax({
url: url,
type: 'POST',
data: data,
dataType: 'json',
contentType: 'application/json',
success: fn
});
}
/***
* ajax獲取服務端數據
* @url 業務數據
* @data
*/
$.doAjaxGet = function(url, fn) {
//$.getJSON(url, fn);
$.ajax({
url: url,
type: "GET",
dataType: 'json',
//data: data,
contentType: 'application/json',
success: fn
});
}
$.doAjaxPost = function(url, data, fn) {
$.ajax({
url: url,
type: 'POST',
data: data,
dataType: 'json',
contentType: 'application/json',
success: fn
});
}
//構造html的通用方法
$.buildHTML = function(tag, html, attrs) {
// you can skip html param
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 + ">" : "/>";
}
//構造JsTree的通用方法
$.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 如何調試?
FireBug for Firefox
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異常錯誤代碼
4.2 Javascript-自執行匿名函數
//Self-Executing Anonymous Func: Part 2 (Public & Private)
(function( skillet, $, undefined ) {
//Private Property
var isHot = true;
//Public Property
skillet.ingredient = "Bacon Strips";
//Public Method
skillet.fry = function() {
var oliveOil;
addItem( "\t\n Butter \n\t" );
addItem( oliveOil );
console.log( "Frying " + skillet.ingredient );
};
//Private Method
function addItem( item ) {
if ( item !== undefined ) {
console.log( "Adding " + $.trim(item) );
}
}
}( window.skillet = window.skillet || {}, jQuery ));
<pre name="code" class="javascript">//Public Properties
console.log( skillet.ingredient ); //Bacon Strips
//Public Methods
skillet.fry(); //Adding Butter & Fraying Bacon Strips
//Adding a Public Property
skillet.quantity = "12";
console.log( skillet.quantity ); //12
//Adding New Functionality to the Skillet
(function( skillet, $, undefined ) {
//Private Property
var amountOfGrease = "1 Cup";
//Public Method
skillet.toString = function() {
console.log( skillet.quantity + " " +
skillet.ingredient + " & " +
amountOfGrease + " of Grease" );
console.log( isHot ? "Hot" : "Cold" );
};
}( window.skillet = window.skillet || {}, jQuery ));
try {
//12 Bacon Strips & 1 Cup of Grease
skillet.toString(); //Throws Exception
} catch( e ) {
console.log( e.message ); //isHot is not defined
}
</pre>
//建議申明對象或數組的寫法
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#)
// <span style="color:#ff0000;">C# 例子. 不要在Javascript中這樣寫</span>
if ( someString != null &&
someString.length > 0 ) {
//Do something here...
}
// C# 例子 檢查字符串是否爲空
if ( !string.IsNullOrEmpty(someString) ) {
//Do something here...
}
4.5 判斷對象是否爲NULL(javascript)
Javascript中的正確寫法
// Simplified JavaScript syntax to check for
// undefined, null, & empty string values
if ( someString ) {
//Do something here...
}
4.6 設置缺省值(c#)
<span style="color: rgb(255, 0, 0);">// C# 例子,不要在Javascript這樣寫</span>
if ( someString == null ) {
someString = "default Value";
}
// Slightly better, but don't do this either
someString = someString ? someString : "default value"; <pre name="code" class="javascript">請在Javascript按如下格式寫
// JavaScript syntax to set a default value
someString = someString || "default value";
var myArray = [], name;
myArray[5] = "test";
console.log( myArray.length ); //6
for ( name in myArray ) {
console.log( name, myArray[name] );
//Outputs...
// 5, test
}
4.10 正確的數組遍歷操作符for…;…;
var myArray = [], name;
myArray[5] = "test";
console.log( myArray.length ); //6
for ( var i = 0, length = myArray.length; i < length; i++ ) {
console.log( i, myArray[i] );
//Outputs...
// 0, undefined
// 1, undefined
// 2, undefined
// 3, undefined
// 4, undefined
// 5, test
}
for ( var name in object ) {
//Your code here
}
/* Check if object has property before
iterating, because functions inherited
from prototype are also included */
for ( var name in object ) {
if ( object.hasOwnProperty(name) ) {
//Your code here
}
}
RequireJS 是一個非常小巧的 JavaScript 模塊載入框架,是 AMD 規範最好的實現者之一。最新版本的 RequireJS 壓縮後只有 14K,堪稱非常輕量。它還同時可以和其他的框架協同工作,使用 RequireJS 必將使的前端代碼質量得以提升。
define(['Controllers/Main/ListView'], function (ListView) {
function start() {
var users = JSON.parse(localStorage.users);
ListView.render({ users: users });
}
return {
start: start
};
});
提供的示例RequireMVC199中,可以看一下ProductSys.WebApi的服務層代碼,前端代碼看RequireMvc199的WebApplication項目即可。
完整示例,可以看一下ProductList頁面的代碼,這個示例是完整的,包括文件:
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