RESTier - .NET RESTful API Framework

個 title 全英文,有啲唔慣,不過冇法,都唔知可以點加中文?最多係將 Framework 改做框架,框咩架吖,奇奇怪怪!

一年多前,我打算用 DreamFactory 嚟搞 REST API,當時用緊 v1.9.4,用得好開心,不過 DreamFactory 發展得好快,轉下眼就出 version 2.2,仲改埋個 license,收起啲 database connectors,以後要俾錢先可以 connect database。唔可以 connect database 即係冇用。

講到 RESTful 標準,.NET 有個 OData 仲有 Web API 可以搞 RESTful API,祇不過 Microsoft 嘅慣例就係會搞到啲嘢複複雜雜,你用佢哋嘅標準就得,你如果想用佢嘅 tools 嚟搞非 Microsoft 嘅嘢,你就死得,一定有手尾!

已經係 2016 年,Microsoft 衰咗好多年,佢話要參與 Open Source 亦都搞咗好幾年,今年特別積極,影響我嘅有:

  • TFS Online 免費 account
  • Visual Studio Code,當然仲有 Visual Studio 2015 Express
  • 買咗 Xamarin 免費任用
  • 搞咗 .NET Core 1.0 & ASP.NET Core 1.0,仲 Open Source 埋
  • MS SQL 2016 for Linux

於是,我又「王者回歸」,升級我啲舊 projects 之餘,亦增加 mobile 嘅功能,講到 mobile 少不免要搞 REST API,原本打算用 Python 搞,不過又想試下用哂 .NET 搞,咁就 … Google 下。

RESTier

is a RESTful API development framework for building standardized, OData V4 based RESTful services on .NET platform

Open Source,好似係 Microsoft 搞嘅 OData 旁支(NuGet 話嘅),放喺 GitHub 任用,班 contributors 好多中文名,咁就一定要撐下!

開波:

Screen Shot 2016-07-11 at 5.59.19 PM Screen Shot 2016-07-11 at 8.56.42 PM Screen Shot 2016-07-11 at 8.57.34 PM
我用 VS2015,開一個新嘅 Blank Solution,就叫 WebApiRestier。 加一個新嘅 project,選用 C# > Web > ASP.NET Web Application,叫做 NorthWindApi 選 ASP.NET 4.5.2 Empty template,Web API,No Authentication。
Screen Shot 2016-07-11 at 8.58.10 PM Screen Shot 2016-07-11 at 8.59.04 PM Screen Shot 2016-07-11 at 8.59.31 PM
Progress bar,VS2015 話你知佢做緊嘢,請等等。 搞完,Solution Explorer 會有嘢睇。 Right click 個 project,再選 Manage NuGet Packages。
Screen Shot 2016-07-11 at 9.00.44 PM Screen Shot 2016-07-11 at 9.01.51 PM Screen Shot 2016-07-11 at 9.02.33 PM
出現 NuGet Package Manager,選 Browse,check “include prerelease”,輸入 Restier。 Highlight Microsoft.Restier,目前係 v0.5.0-beta,click Install。 VS2015 做嘢。
Screen Shot 2016-07-11 at 9.02.52 PM Screen Shot 2016-07-11 at 9.03.42 PM Screen Shot 2016-07-11 at 9.04.03 PM
VS2015 話,根據你部機嘅情況,我要下載以上嘅軟件,click OK。 你有權唔 Accept,唔答應就咪用,唯有 click Accept。 你答應咗,VS2015 就做嘢,正式下載有關嘅軟件。
Screen Shot 2016-07-11 at 9.06.16 PM Screen Shot 2016-07-11 at 9.07.28 PM Screen Shot 2016-07-11 at 9.08.56 PM
安裝完成。 Right click folder Models,選 Add>New Item。 選 C#>Data>ADO.NET Entity Data Model,俾個名:NorthWindEdm,click Add。
Screen Shot 2016-07-11 at 9.09.57 PM Screen Shot 2016-07-11 at 9.10.27 PM Screen Shot 2016-07-11 at 9.10.47 PM
VS2015 會進一步問你想用咩 model,選 Code First from database,click Next。 要駁 SQL Server 攞 database 資料,click New Connection。 我係用 MS SQL,如果你唔係就要自己諗掂佢。😀
Screen Shot 2016-07-11 at 9.11.47 PM Screen Shot 2016-07-11 at 9.12.29 PM Screen Shot 2016-07-11 at 9.13.07 PM
今次我係用 NorthWind 嚟做示範。 我揀 Yes, include the sensitive data… 即係將個 password 擺喺個 Web.Config 之內。 選好哂之後隻 Next button 先可以 click 得,玩嘢,無聊!
Screen Shot 2016-07-11 at 9.13.43 PM Screen Shot 2016-07-11 at 9.14.24 PM Screen Shot 2016-07-11 at 9.15.42 PM
立即顯示 NorthWind 嘅 Tables 同埋 Views。 我選哂啲 Tables 同埋唔 check Pluralize…,我唔鍾意分單數雙數,廢事日後用起上嚟打錯字,打少個 s 就完全唔同哂,都幾麻煩㗎。 click 完 Finish 等一陣,VS2015 就會出現哩個畫面。
Screen Shot 2016-07-11 at 9.16.05 PM Screen Shot 2016-07-11 at 9.17.17 PM Screen Shot 2016-07-12 at 10.21.44 AM
Solution Explorer 喺 Models 之下多咗好多個 classes,對應番 NorthWind 嘅 Tables。 差不多用得,改埋 App_Start > WebApiConfig.cs 就 OK。 我保留原本嘅 codes,增加 Restier 要嘅 Routing,因為遲啲我要加啲 Custom Routings 到時原本嘅 Routing codes 就有用。
Screen Shot 2016-07-12 at 10.23.07 AM Screen Shot 2016-07-12 at 10.24.13 AM Screen Shot 2016-07-12 at 10.24.44 AM
VS2015 Debug > Run,個 Browser 出現 error,咁係啱嘅,我個 Routing 寫明要用 root path =api/NorthWind。 我用 Chrom 嘅 Extension “Secure Shell” 嚟試,未加 root path 會出 error。 加咗 root path,綠色,200 OK,仲有料到添,一切正常。
Screen Shot 2016-07-12 at 10.26.05 AM Screen Shot 2016-07-12 at 10.26.50 AM Screen Shot 2016-07-12 at 10.30.54 AM
Collapse 嚟睇,全部 Models 之內嘅 classes 都喺度。 淨係睇 Customers 數下總共有幾多 Customer。
Screen Shot 2016-07-12 at 10.37.57 AM Screen Shot 2016-07-12 at 10.40.17 AM
唔睇成隻 record,淨係睇 Customer ID 同埋 Company Name。 哩個勁,淨係要 ANTON 嘅 record!

NorthWind RESTful API 大功告成!

送上 WebApiConfig 嘅 codes:


using NorthWindApi.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using System.Web.OData.Extensions;

namespace NorthWindApi
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services

            // Web API routes
            config.MapHttpAttributeRoutes();

            config.EnableUnqualifiedNameCall(true);
            RegisterNorthWind(config, GlobalConfiguration.DefaultServer);

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        }
        public static void RegisterNorthWind(HttpConfiguration config, HttpServer server)
        {
            config.MapRestierRoute<EntityFrameworkApi<NorthWindEdm>>(
                "NorthWind",
                "api/NorthWind",
                new RestierBatchHandler(server));
        }
    }
}

 

Chrome Extension REST Client plugin 我用哩隻:DHC REST Client

NorthWind database 我用哩個 Sql Script:Northwind Community Edition

 

預告一下,下次講 Custom Routing,即係除咗 CRUD 功能,我要做一啲自選功能。