C# New Project

Everytime I setup a new project, besides consideration on if I need microservice architecture or a good old monolithic application there are always simliar steps needed to get the project in a maintainable state.

Docker Compose

Regardless of the architecture, anything new needs to run in a container. This is just defacto now days. I built a simple demo a few years back which is still relavant. Today I’d add that compose locally should bring up a containerised local environment for everything your application needs to run.

This should be run from one file like Run_Local.ps1, below is an example.

Example Run_Local.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<#
.SYNOPSIS
Run all the stubs locally

.DESCRIPTION
This script does the following:
1. Run Localstack (dynamodb)
#>

param (
[Parameter()]
[switch]
$RebuildStubs,

[Parameter()]
[switch]
$NoBff,

[Parameter()]
[switch]
$LocalStackOnly
)

Set-StrictMode -Version 2.0
$ErrorActionPreference = 'Stop'

Write-Output '****** Running stubs ******'
$host.ui.RawUI.WindowTitle = 'Porky Stubs'

if ($LocalStackOnly.isPresent) {
Write-Output '****** STARTING ONLY LOCALSTACK ******'
docker start pky_localstack
} elseif ($NoBff.isPresent) {
Write-Output '****** NO BFF ******'
docker-compose --project-name porky -f ./stubs/docker-compose-no-bff.yml up ($RebuildStubs ? "--build" : "") --remove-orphans
}
else {
Write-Output 'No params found! Try -LocalStackOnly or -NoBff and optionally -RebuildStubs'
}

Code Conventions

We want all members of the team rowing in the same direction. As there are many IDEs (Visual Studio, Ryder ect) the simplest way is to commit a .editorconfig file with the source control. Check out https://editorconfig.org/

Example .editorconfig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
root = true

[*.*]
end_of_line = crlf
indent_size = 4
indent_style = space
trim_trailing_whitespace = true

[*.{html,htm,xml,config,csproj,aspx,ascx,cshtml,resx,json,targets,props,yml,yaml}]
indent_size = 2

[*.cs]
accessor_owner_body = expression_body
braces_for_foreach = required
braces_for_ifelse = required
braces_for_for = required
csharp_new_line_before_open_brace = all
csharp_parentheses_redundancy_style = remove_if_not_clarifies_precedence
csharp_remove_blank_lines_near_braces_in_declarations = true
csharp_space_after_keywords_in_control_flow_statements = true
dotnet_sort_system_directives_first = true
extra_spaces = remove_all
insert_final_newline = false
method_or_operator_body = expression_body
namespace_body = file_scoped
object_creation_when_type_evident = target_typed
trailing_comma_in_multiline_lists = false


[*.docker-compose.yml]
quote_type = double

Static Code Analysis

You have more than one option!

SonarScanner (Remote Server)

Its important to understand that sonarscanner doesnt run tests, it reports on static code and can be provided test coverage reports.

Run your own containerised Sonarqube server locally.

SonarAnalyzer (Local Static Code Analysis)

Although many large IT companies have a sonar server which can be used a a quality gate in PRs and Master builds its possible to do these checks locally.

Unit Test code coverage

Logging

CI/CD pipeline

CI/CD pipeline for build & tests

References