Skip to content
/ dotnet-starter Public template

My template project for new C# / dotnet / .NET projects

License

Notifications You must be signed in to change notification settings

wonderbird/dotnet-starter

Repository files navigation

Starter Template for new .NET Projects

Gitpod ready-to-code Build Status Badge

This repository provides a starter template for new C# projects.

Thanks

Many thanks to JetBrains who provide an Open Source License for this project ❤️.

Development

What is inside?

Attention:

The contained .editorconfig brings formatting configuration and static code analysis rules. Violating these rules will fail your build. You can delete or adapt this file to get more flexible rules.

Additional folders, files and configuration:

  • DotnetStarter.Logic - is a DLL project for business logic
  • DotnetStarter.Logic.Tests - is the corresponding xUnit test project, configured with static code rules allowing underscores in test names (GlobalSuppressions.cs)
  • .github\dependabot.yml - configuration for the GitHub Dependabot.
  • .github\workflows\dotnet.yml - automatic builds using GitHub actions.

Creating a New Project From this Template

After having forked this starter project, you'll need to adapt the project names inside this solution, fix the links in the badges section above and some documentation:

  1. Change the README.md to your needs

  2. Change the LICENSE to your needs

  3. Rename DotnetStarter to match your new project

    • rename the .sln, the contained projects and the root namespaces to match your new project
    • adapt the test entry in the .github/workflow/dotnet.yml file
    • adapt the test directory in the .gitpod.yml file

Quick-Start

Click the Gitpod ready-to-code badge (also above) to launch a web IDE.

If that does not work for you or if you'd like to have the project on your local machine, then continue reading.

Prerequisites

To compile, test and run this project the .NET 7.0.x SDK is required on your machine. For calculating code metrics I recommend metrix++. This requires python.

If you are interested in test coverage, then you'll need the following tools installed:

dotnet tool install --global coverlet.console --configfile NuGet-OfficialOnly.config
dotnet tool install --global dotnet-reportgenerator-globaltool --configfile NuGet-OfficialOnly.config

Build, Test, Run

Run the following commands from the folder containing the .sln file in order to build and test.

Build the Solution and Run the Tests

dotnet build
dotnet test
# If you like continuous testing then use the dotnet file watcher to trigger your tests
dotnet watch -p ./DotnetStarter.Logic.Tests test
# As an alternative, run the tests with coverage and produce a coverage report
rm -r DotnetStarter.Logic.Tests/TestResults && \
  dotnet test --no-restore --verbosity normal /p:CollectCoverage=true /p:CoverletOutputFormat=cobertura /p:CoverletOutput='./TestResults/coverage.cobertura.xml' && \
  reportgenerator "-reports:DotnetStarter.Logic.Tests/TestResults/*.xml" "-targetdir:report" "-reporttypes:Html;lcov" "-title:DotnetStarter"
open report/index.html

Before Creating a Pull Request ...

Apply code formatting rules

# Install https://csharpier.io globally, once
dotnet tool install -g csharpier

# Format code
dotnet csharpier .

Check Code Metrics

... check code metrics using metrix++

  • Configure the location of the cloned metrix++ scripts

    export METRIXPP=/path/to/metrixplusplus
  • Collect metrics

    python "$METRIXPP/metrix++.py" collect --std.code.complexity.cyclomatic --std.code.lines.code --std.code.todo.comments --std.code.maintindex.simple -- .
  • Get an overview

    python "$METRIXPP/metrix++.py" view --db-file=./metrixpp.db
  • Apply thresholds

    python "$METRIXPP/metrix++.py" limit --db-file=./metrixpp.db --max-limit=std.code.complexity:cyclomatic:5 --max-limit=std.code.lines:code:25:function --max-limit=std.code.todo:comments:0 --max-limit=std.code.mi:simple:1

At the time of writing, I want to stay below the following thresholds:

--max-limit=std.code.complexity:cyclomatic:5
--max-limit=std.code.lines:code:25:function
--max-limit=std.code.todo:comments:0
--max-limit=std.code.mi:simple:1

Finally, remove all code duplication. The next section describes how to detect code duplication.

Remove Code Duplication Where Appropriate

To detect duplicates I use the CPD Copy Paste Detector tool from the PMD Source Code Analyzer Project.

If you have installed PMD by download & unzip, replace pmd by ./run.sh. The homebrew pmd formula makes the pmd command globally available.

pmd cpd --minimum-tokens 50 --language cs --files .

References

.NET Core

Code Quality

About

My template project for new C# / dotnet / .NET projects

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages