2016年6月12日 星期日

Why IHttpActionResult

Action Results in Web API 2 The IHttpActionResult interface was introducted in Web API 2. Essentially, it defines an HttpResponseMessage factory. Here are some advantages of using the IHttpActionResult interface:
  • Simplifies unit testing your controllers.
  • Moves common logic for creating HTTP responses into separate classes.
  • Makes the intent of the controller action clearer, by hiding the low-level details of constructing the response.
other advantages:

  • Respecting to single responsibility principle: cause action methods have the responsibility of serving the HTTP requests and should not involve in creating the HTTP response messages.
  • Useful implementations already defined in the System.Web.Http.Results namely: 
    • Ok
    • NotFound
    • Exception
    • Unauthorized
    • BadRequest
    • Conflict
    • Redirect
    • InvalidModelState
    • 完整列表
  • Uses Async and Await by default.
  • Easy to create own ActionResult just by implementing ExecuteAsync method.
  • you can use ResponseMessageResult ResponseMessage(HttpResponseMessage response) to convert HttpResponseMessage to IHttpActionResult.

2016年3月21日 星期一

Entity Framework使用資料庫View並設定關聯

Entity Model除可用資料表外也可加入資料庫檢視(View),但需先正確設定View的Entity Key,如此關聯建立時才知如何對應兩邊的資料:


建立關聯時在關聯線上點兩下滑鼠,還需指定那一邊是主要(Principal)、那邊是相依的(Dependent):

2016年3月7日 星期一

Kendo UI Grid 匯出 PDF 中文變方塊解法

  • 第一階段:先讓Kendo Grid可以匯出PDF
1.在Grid上顯示匯出PDF按鈕
$("#grid").kendoGrid({
    //...
    toolbar: ["pdf"],
    pdf: {
        allPages: true
    }
}); 
2.載入 pako_deflate.min.js 啟用壓縮 PDF 功能
<script src="Kendo/js/pako_deflate.min.js"></script>
  • 第二階段:自訂中文字型
1.定義支援Unicode的TrueType中文字型,以下以DFKai-SB為例:
@font-face {
    font-family: "DFKai-SB";
    src: url("Kendo/styles/fonts/KaiU/kaiu.ttf") format("truetype");
}
2.透過CSS讓Kendo Grid(.k-grid)使用自訂的DFKai-SB字型:
.k-grid {
    font-family: "DFKai-SB";
}

2015年9月11日 星期五

架設Windows內部網路共用的Git伺服器(透過Visual Studio存取)

對於一個軟體公司來說,「資料備份分享」十分基本也十分重要。Git是目前最好的一個解決方案:免費,存取快速。

目的:
  1. 透過Visual Studio內建的「Team Explorer」-「Clone」複製Git伺服器上的專案
  2. 可離線編輯,「Commit」更新後再「Sync」同步至Git伺服器
做法分兩個部分:Git伺服器、開發人員電腦。

Git伺服器

  1. 準備一台Windows伺服器(如:server1),並建立一個目錄(如:d:\git),分享出來(如://server1/git)。
  2. 安裝Git for Windows。
  3. 在Git伺服器上為每個專案建立對應的Git儲存庫
    每個專案應對應到各自的儲存庫,不該多個專案放在一個儲存庫。
    這裡的專案,不是Visuals Studio中的專案(Project),比較像Visual Studio中的解決方案(Solution)。
    以下以ERP專案為例。
  4. 開啟「Git Bash」,依序執行以下指令為專案(如:ERP)建立Git儲存庫(ERP.git,凡透過bare初始化的儲存庫名稱慣例以.git結尾):
    cd /d/git
    git init --bare ERP.git

開發人員電腦

  1. 安裝Git for Windows。
  2. 開啟Visual Studio,「View」--「Team Explorer」視窗。
  3. 設定個人資訊,做為上傳Git儲存庫時的署名。

  4. 點選「Clone」,輸入Git伺服器分享出來的儲存庫位置,如下:
  5. 按下「Clone」後,即可自Git儲存庫複製檔案至指定的本機目錄(如:d:\playground\ERP)。
  6. 點選下方的EFP即可開啟此工作目錄。
  7. 再點選「New」,即可在此工作目錄下新增Visual Studio專案...
  8. 例如:ConsoleApp。
  9. 建置專案後,儲存此次異動:新增ConsoleApp專案。
  10. 同步至Git儲存庫。


其它開發人員,依相同方式存取。

參考:Bare Repositories

2015年3月18日 星期三

2015年1月7日 星期三

JavaScript 的 Model-View 框架:Knockout、Backbone、Angular 比較

這三個JavaScript Library都可幫助我們在瀏覽器中呈現(Template)/繫結資料(Data Binding),也就是所謂的MVW Framework(Model-View-Whatever)。以難易度來講,Knockout和Angular都很容易上手,尤其是Angular提供強大及神奇的資料繫結功能,但若考慮擴充及自訂性,則是有點學習難度Backbone取勝。
(參考資源:http://codeutopia.net/blog/2013/03/16/knockout-vs-backbone-vs-angular/)

2015年1月5日 星期一

HTML5 CSS3 學習資源


由淺入深的學習路徑
  1. HTML5 & CSS3 Fundamentals: Development for Absolute Beginners
    由Microsoft Virtual Academy 微軟虚擬學院提供教學影片
  2. The Modern Web Platform Jump Start
    由Microsoft Virtual Academy 微軟虚擬學院提供教學影片、投影片、測驗及sample code
  3. Web Services 開發實戰: 使用 ASP.NET Web API 2
    HTML5及CSS3純粹是Browser端的技術,Web Server端以.NET來說可用Web API來提供資料。