Create a dotnet core API boilerplate template with most popular open source libraries

Precise Agency
2 min readAug 10, 2020

--

Every time I create a new project, I have to go through exactly same steps to setup some open source libraries that I always use for any projects.

Here is my list of most popular github c# libraries for a web API app.

  1. Autofac
  2. Swagger
  3. Serilog
  4. AutoMapper
  5. Newtonsoft.Json
  6. nunit & NUnit3TestAdapter
  7. AutoFixture (& AutoFixture.AutoMoq for integration with Moq)
  8. Moq
  9. FluentAssertions

I mean there is nothing difficult to setup all these libraries, it is just time consuming and repetitive job that has to be done every single time.

So here I am trying to create a small template project that is pre-configured with all these libraries above, so that speed up your new project setup-process.

Create the template project

I had this template project in a folder structures like this,

|-- Api
|-- *.cs (project files)
|-- <MyProject>.csproj
|-- Api.Tests
|-- *.cs (project files)
|-- <MyProject>.Tests.csproj
|-- <MyProject>.sln

This is the link to the published nuget package,

You can install this template via nuget using dotnet new -i,

dotnet new --install Superwalnut.NetCoreApiTemplate

The template will be installed as core-api-autofac-swagger-serilog.

Then you can create your new project using this template,

dotnet new core-api-autofac-swagger-serilog -n MyFirstApi

What is included in your new project

Autofac

Using ContainerBuilder() that you can populate IServiceCollection and register your autofac modules in Startup.cs. Taking ApiModule from the template as an example, you can also create modules for your Service Layer, Repository Layer, etc.

It has an ApiModule that registers Serilog & Automapper.

Swagger

Pre-configured swagger endpoint and You can access via https://localhost:5001/swagger

Serilog

Pre-configured serilog with console sinks in the appsettings.json,

configure it in the program.cs

register it in the ApiModule,

Then you can inject into the any class’s constructor and use it,

AutoMapper

Created a default AutoMapper profile with a class Foo/FooDto,

That is registered in the ApiModule with profiles,

Now you can inject the IMapper to any class’s constructor and start mapping things,

NUnit, AutoFixture & Moq

Created a TestBase.cs for global setup of Fixture,

And create mocks and setup mock methods,

FluentAssertions

Use the fluent style to assert your tests,

Finally,

Congratulations on setup your .net core app with this template.

If you find my article is helpful and saving you some time, please click Applaud to support me.

Here is the link to GitHub to grab the complete source code for the app.

--

--

Precise Agency

I am exploring web dev trends in areas like .net, serverless, react, angular, etc. & using medium as an amazing platform to improve my skills and share my code