時間:2023-06-06 15:51:01 | 來源:網(wǎng)站運(yùn)營
時間:2023-06-06 15:51:01 來源:網(wǎng)站運(yùn)營
推薦:打造自己的.NET Core項(xiàng)目模板:本文已收錄于:.NET Core開發(fā)精選文章目錄,持續(xù)更新,歡迎投稿!template.json
!.template.config
,同時在這個文件夾下面創(chuàng)建template.json。{ "author": "Catcher Wong", //必須 "classifications": [ "Web/WebAPI" ], //必須,這個對應(yīng)模板的Tags "name": "TplDemo", //必須,這個對應(yīng)模板的Templates "identity": "TplDemoTemplate", //可選,模板的唯一名稱 "shortName": "tpl", //必須,這個對應(yīng)模板的Short Name "tags": { "language": "C#" , "type":"project" }, "sourceName": "TplDemo", // 可選,要替換的名字 "preferNameDirectory": true // 可選,添加目錄 }
在這里,有幾個比較重要的東西,一個是shortName,一個是sourceName。-h
就絕對不寫 ``--help`template.json
之后,還需要安裝一下這個模板到我們的cli
中。dotnet new -i
進(jìn)行模板的安裝。dotnet new -i ./content/TplDemo
這里要注意的是,與.template.config
文件夾同級的目錄,都會被打包進(jìn)模板中。 在執(zhí)行安裝命令之后,就可以看到我們的模板已經(jīng)安裝好了。dotnet new tpl -h
因?yàn)槲覀兡壳斑€沒有設(shè)置參數(shù),所以這里顯示的是還沒有參數(shù)。RequestLogMiddleware
,就用它來做例子。template.json
中添加過濾EnableRequestLog
的symbol
。同時指定源文件{ "author": "Catcher Wong", //others... "symbols":{ //是否啟用RequestLog這個Middleware "EnableRequestLog": { "type": "parameter", //它是參數(shù) "dataType":"bool", //bool類型的參數(shù) "defaultValue": "false" //默認(rèn)是不啟用 } }, "sources": [ { "modifiers": [ { "condition": "(!EnableRequestLog)", //條件,由EnableRequestLog參數(shù)決定 "exclude": [ //排除下面的文件 "src/TplDemo/Middlewares/RequestLogMiddleware.cs", "src/TplDemo/Middlewares/RequestLogServiceCollectionExtensions.cs" ] } ] } ] }
第二步,在模板的代碼中做一下處理Startup.cs
,因?yàn)镸iddleware就是在這里啟用的。 using System; //other using... using TplDemo.Core;#if (EnableRequestLog) using TplDemo.Middlewares;#endif /// <summary> /// /// </summary> public class Startup { public void Configure(IApplicationBuilder app, IHostingEnvironment env) { //other code....#if (EnableRequestLog) //request Log app.UseRequestLog();#endif app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); }); } }
這樣的話,只要EnableRequestLog
是true,那么就可以包含這兩段代碼了。dotnet new tpl -n NoLog
這個命令等價于dotnet new tpl -n WithLog -E false
下面是建好之后的目錄結(jié)構(gòu)和Startup.csdotnet new tpl -n WithLog -E true
可以看到,效果已經(jīng)出來了。template.json
,加入下面的內(nèi)容{ "author": "Catcher Wong", //others "symbols":{ "sqlType": { "type": "parameter", "datatype": "choice", "choices": [ { "choice": "MsSQL", "description": "MS SQL Server" }, { "choice": "MySQL", "description": "MySQL" }, { "choice": "PgSQL", "description": "PostgreSQL" }, { "choice": "SQLite", "description": "SQLite" } ], "defaultValue": "MsSQL", "description": "The type of SQL to use" }, "MsSQL": { "type": "computed", "value": "(sqlType == /"MsSQL/")" }, "MySQL": { "type": "computed", "value": "(sqlType == /"MySQL/")" }, "PgSQL": { "type": "computed", "value": "(sqlType == /"PgSQL/")" }, "SQLite": { "type": "computed", "value": "(sqlType == /"SQLite/")" } }}
看了上面的JSON內(nèi)容之后,相信大家也知道個所以然了。有一個名為sqlType的參數(shù),它有幾中數(shù)據(jù)庫選擇,默認(rèn)是MsSQL。<ItemGroup Condition="'$(MySQL)' == 'True' "> <PackageReference Include="MySqlConnector" Version="0.47.1" /></ItemGroup><ItemGroup Condition="'$(PgSQL)' == 'True' "> <PackageReference Include="Npgsql" Version="4.0.3" /></ItemGroup><ItemGroup Condition="'$(SQLite)' == 'True' "> <PackageReference Include="Microsoft.Data.Sqlite" Version="2.1.0" /></ItemGroup>
同樣的,代碼也要做相應(yīng)的處理#if (MsSQL) using System.Data.SqlClient;#elif (MySQL) using MySql.Data.MySqlClient;#elif (PgSQL) using Npgsql;#else using Microsoft.Data.Sqlite;#endif protected DbConnection GetDbConnection() {#if (MsSQL) return new SqlConnection(_connStr);#elif (MySQL) return new MySqlConnection(_connStr);#elif (PgSQL) return new NpgsqlConnection(_connStr);#else return new SqliteConnection(_connStr);#endif }
修改好之后,同樣要去重新安裝這個模板,安裝好之后,就可以看到sqlType這個參數(shù)了。dotnet new tpl -n MsSQLTest -s MsSQL dotnet new tpl -n PgSQLTest -s PgSQL
然后打開對應(yīng)的csproj關(guān)鍵詞:項(xiàng)目,模板,打造,推薦
客戶&案例
營銷資訊
關(guān)于我們
微信公眾號
版權(quán)所有? 億企邦 1997-2025 保留一切法律許可權(quán)利。