"symbols": { //数据源类型"DbType": { "type": "parameter", //它是参数 "datatype": "choice", //选项类型 "choices": [ //选项取值 { "choice": "MsSQL", "description": "MS SQL Server" }, { "choice": "MySQL", "description": "MySQL" }, { "choice": "PgSQL", "description": "PostgreSQL" }, { "choice": "SQLite", "description": "SQLite" }, { "choice": "None", "description": "None" } ], "defaultValue": "None", "description": "The type of SQL to use" }, "MsSQL": { "type": "computed", "value": "(DbType == \"MsSQL\")" }, "MySQL": { "type": "computed", "value": "(DbType == \"MySQL\")" }, "PgSQL": { "type": "computed", "value": "(DbType == \"PgSQL\")" }, "SQLite": { "type": "computed", "value": "(DbType == \"SQLite\")" }, "None": { "type": "computed", "value": "(DbType == \"None\")" } }
b) 修改项目文件:根据条件选择对应程序集
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>netcoreapp3.1</TargetFramework> </PropertyGroup> <ItemGroup Condition="'$(None)'!='True'"> <PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.8" /> </ItemGroup> <ItemGroup Condition="'$(SQLite)' == 'True'"> <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="3.1.8" /> </ItemGroup> <ItemGroup Condition="'$(MsSQL)' == 'True'"> <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.8" /> </ItemGroup> <ItemGroup Condition="'$(PgSQL)' == 'True'"> <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="3.1.4" /> </ItemGroup> </Project>
c) 使用数据库处,同步处理
#if (!None) //审计日志存储 services.AddDbContext<AuditLogDBContent>(options => { string conn = Configuration.GetConnectionString("LogDB"); #if Sqlite options.UseSqlite(conn, options => { options.MigrationsAssembly("AuditLogDemo"); }); #endif #if PgSQL options.UseNpgsql(conn); #endif #if MsSQL options.UseSqlServer(conn); #endif }); #endif
d) 更新模板后,查看已生成了对应参数
e) 创建项目查看生效情况:
//不使用数据库 dotnet new apiTmp -n AA -D None //使用PG数据库 dotnet new apiTmp -n BB -D PgSQL
可以看到项目引用和程序代码都根据传入参数生成了。
3、项目模板打包发布好东西当然不能只给自己使用,而应该怎么将生成的模板给大家使用呢?——Nuget 包发布
a)在模板根目录创建一个nuspec文件:apiTmp.nuspec
<?xml version="1.0" encoding="utf-8"?> <package xmlns="http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd"> <metadata> <id>apiTmp</id> <version>1.0.0</version> <description> WebApi Template </description> <authors>cwshegn</authors> <packageTypes> <packageType /> </packageTypes> </metadata> </package>
b)使用Nuget Pack 命令打包
nuget pack apiTmp.nuspec -OutputDirectory .
c) 发布到nuget服务,给小伙伴们下载使用
总结:通过dotnet cli 创建的模板项目,能更方便的根据项目需求生成对应的项目结构,提升开发效率。
可以动手把积累的好项目打包成一个模板项目给小伙伴们使用
参考:
https://devblogs.microsoft.com/dotnet/how-to-create-your-own-templates-for-dotnet-new/
https://github.com/dotnet/dotnet-template-samples
https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-new
https://docs.microsoft.com/en-us/nuget/install-nuget-client-tools
到此这篇关于.NET Core自定义项目模板的文章就介绍到这了,更多相关.NET Core自定义项目模板内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!