Skip to content

Commit

Permalink
feat: add database option
Browse files Browse the repository at this point in the history
  • Loading branch information
EnisMulic committed Nov 4, 2023
1 parent 2340821 commit c4a12ed
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 14 deletions.
4 changes: 4 additions & 0 deletions .template.config/dotnetcli.host.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
"GitHost": {
"longName": "git-host",
"shortName": "gh"
},
"Database": {
"longName": "database",
"shortName": "db"
}
}
}
10 changes: 10 additions & 0 deletions .template.config/ide.host.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@
"text": "Select the git respository host"
},
"isVisible": true
},
{
"id": "Database",
"name": {
"text": "Database"
},
"description": {
"text": "Select the database"
},
"isVisible": true
}
]
}
38 changes: 38 additions & 0 deletions .template.config/template.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,38 @@
"UseNoGitHost": {
"type": "computed",
"value": "(GitHost == \"None\")"
},
"Database": {
"type": "parameter",
"datatype": "choice",
"choices": [
{
"choice": "MSSql",
"description": "Use Microsoft SQL or Azure SQL"
},
{
"choice": "PostgreSql",
"description": "Use PostgreSql"
},
{
"choice": "None",
"description": ""
}
],
"defaultValue": "None",
"description": "The type of database to use"
},
"UseMSSql": {
"type": "computed",
"value": "(Database == \"MSSql\")"
},
"UsePostgreSql": {
"type": "computed",
"value": "(Database == \"PostgreSql\")"
},
"UseDatabase": {
"type": "computed",
"value": "(Database != \"None\")"
}
},
"sources": [
Expand All @@ -60,6 +92,12 @@
{
"condition": "(UseNoGitHost)",
"exclude": [".azuredevops/**", ".github/**"]
},
{
"condition": "(!UseDatabase)",
"exclude": [
"src/Application/Infrastructure/Persistance/**"
]
}
]
}
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ dotnet new vsma --name ProjectName
```
dotnet new vsma --name [ProjectName]
[-gh|--git-host [Github|AzureDevOps|None]]
[-db|--database [MsSql|PostgreSql|None]]
```

### Options
Expand All @@ -35,6 +36,8 @@ dotnet new vsma --name [ProjectName]

* `-gh|--git-host [Github|AzureDevOps|None]`
Choose the platform you will host your projects git repository, this will give you a base CI workflow, pull request template, and anything specific to the platform that might be of use. The default value is `None`.
* `-db|--database [MsSql|PostgreSql|None]`
Choose what database to use for your project. The default is `None`

## How to Run

Expand Down
5 changes: 4 additions & 1 deletion src/Api/Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,19 @@

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.1" />
<PackageReference Include="Microsoft.Extensions.Logging.ApplicationInsights" Version="2.21.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
<PackageReference Include="Carter" Version="7.0.0" />
<PackageReference Include="FluentValidation.AspNetCore" Version="11.2.2" />
<PackageReference Include="Hellang.Middleware.ProblemDetails" Version="6.5.1" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="7.0.2" />

<!-- #if (UseDatabase) -->
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<!-- #endif -->

</ItemGroup>
<ItemGroup>
<Folder Include="Services\" />
Expand Down
28 changes: 19 additions & 9 deletions src/Application/Application.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
Expand All @@ -10,10 +10,18 @@
</PropertyGroup>

<ItemGroup>

<PackageReference Include="MediatR" Version="11.1.0" />
<PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="11.0.0" />
<PackageReference Include="FluentValidation" Version="11.4.0" />
<PackageReference Include="FluentValidation.DependencyInjectionExtensions" Version="11.4.0" />

<PackageReference Include="AutoMapper" Version="12.0.0" />
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="12.0.0" />
<PackageReference Include="Carter" Version="7.0.0" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.1" />

<!-- #if (UseDatabase) -->
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.1">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand All @@ -24,15 +32,17 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="AutoMapper" Version="12.0.0" />
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="12.0.0" />
<PackageReference Include="Carter" Version="7.0.0" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.1" />
<PackageReference Include="Dapper" Version="2.0.123" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks" Version="7.0.12" />

<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks.EntityFrameworkCore" Version="7.0.0" />

<!-- #if (UseMsSql) -->
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks" Version="7.0.12" />
<PackageReference Include="AspNetCore.HealthChecks.SqlServer" Version="7.0.0" />
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="7.0.0" />

<!-- #elif (UsePostgreSql) -->
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="7.0.1" />
<PackageReference Include="AspNetCore.HealthChecks.NpgSql" Version="7.1.0" />
<!-- #endif -->
<!-- #endif -->
</ItemGroup>
</Project>
18 changes: 14 additions & 4 deletions src/Application/Infrastructure/ConfigureServices.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
using Application.Common.Interfaces;
#if UseDatabase
using Application.Infrastructure.Persistance;
using Application.Infrastructure.Persistance.Interceptors;
using Application.Infrastructure.Services;

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;

#endif
using Application.Infrastructure.Services;

using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

Expand All @@ -14,6 +18,9 @@ public static class ConfigureInfrastructureServices
{
public static IServiceCollection AddInfrastructureServices(this IServiceCollection services, IConfiguration configuration)
{
services.AddTransient<IDateTime, DateTimeService>();

#if UseDatabase
services.AddScoped<ISaveChangesInterceptor, AuditableEntityInterceptor>();
services.AddScoped<ISaveChangesInterceptor, DispatchDomainEventsInterceptor>();
services.AddScoped<ISaveChangesInterceptor, SoftDeleteInterceptor>();
Expand All @@ -22,15 +29,18 @@ public static IServiceCollection AddInfrastructureServices(this IServiceCollecti
{
options.AddInterceptors(sp.GetServices<ISaveChangesInterceptor>());
#if UseMSSql
options.UseSqlServer(configuration.GetConnectionString("Default"),
builder => builder.MigrationsAssembly(typeof(ApplicationDbContext).Assembly.FullName));
#elif UsePostgreSql
options.UseNpgsql(configuration.GetConnectionString("Default"),
builder => builder.MigrationsAssembly(typeof(ApplicationDbContext).Assembly.FullName));
#endif
});

services.AddTransient<IDateTime, DateTimeService>();

services.AddHealthChecks()
.AddDbContextCheck<ApplicationDbContext>("Database");

#endif

return services;
}
Expand Down

0 comments on commit c4a12ed

Please sign in to comment.