Giter Club home page Giter Club logo

cleanarch's Introduction

.NET6.0 - Clean Architecture using Repository Pattern and Dapper with Logging and Unit Testing

Introduction

This article covers creation of sample CRUD API in .NET 6.0 using clean architecture. Read the full documentation here

We will use following tools, technologies, and framework in this sample:

  • Visual Studio 2022 and .NET 6.0
  • C#
  • MS SQL DB
  • Clean Architecture
    • The Clean Architecture is the system architecture guideline proposed by Robert C. Martin also known as Uncle Bob. It derived from many architectural guidelines such as Hexagonal Architecture, Onion Architecture, etc.
  • Dapper (mini ORM)
    • Dapper is a simple Object Mapper or a Micro-ORM and is responsible for mapping between database and programming language.
  • Repository Pattern
  • Unit of Work
  • Swagger UI
  • API Authentication (Key Based)
  • Logging (using log4net)
  • Unit Testing (MSTest Project)

DB setup:

  • Create a new table that will be used to perform the CRUD operation. You can use the scripts shared under the CleanArch.Sql/Scripts folder of the code sample.

Solution and Project setup:

  • Once back end is ready, Open Visual Studio 2022 and create a blank solution project, and name it to CleanArch.

  • Set Up Core Layer: Under the solution, create a new Class Library project and name it CleanArch.Core.

  • One thing to note down here is, The Core layer should not depend on any other Project or Layer. This is very important while working with Clean Architecture.

  • Set Up Application Layer: Add another Class Library Project and name it CleanArch.Application.

    • Add a reference to the Core project, The Application project always depends only on the Core Project.
  • Set Up Logging: Add a new Class Library Project (CleanArch.Logging)

    • Install the log4net package from the Nuget Package Manager and add a reference to the Application project
  • Set Up SQL Project: Add a new Class Library Project (CleanArch.Sql). We’ll be using this project to manage the Dapper Queries.

  • Set Up Infrastructure Layer: Add a new Class Library Project and name it CleanArch.Infrastructure.

    • Add the reference to projects (Application, Core, and Sql), and also add a new folder Repository.

    • Add the required packages to be used in this project.

    Install-Package Dapper
    Install-Package Microsoft.Extensions.Configuration
    Install-Package Microsoft.Extensions.DependencyInjection.Abstractions
    Install-Package System.Data.SqlClient
    
  • Set up API Project: Add a new .NET 6.0 Web API project and name it CleanArch.Api.

    • Add the reference to projects (Application, Infrastructure, and Logging), and also add the Swashbuckle.AspNetCore package.
    Install-Package Dapper
    Install-Package Microsoft.Extensions.Configuration
    Install-Package Microsoft.Extensions.DependencyInjection.Abstractions
    Install-Package System.Data.SqlClient
    
    • Set up the appsettings.json and log4net.config (for logging).
    • Configure Startup settings, such as RegisterServices (defined under CleanArch.Infrastructure project), configure log4net and add the Swagger UI (with authentication scheme).
      • In .NET 6.0 all these settings will be done under Program.cs file and there is no need to add a separate Startup class.
    • Set up AuthorizationFilter and controllers.
  • Set up a Test Project: Add a new MSTest Test project and name it CleanArch.Test and add the below packages.

    Install-Package Microsoft.Extensions.Configuration
    Install-Package MSTest.TestFramework
    Install-Package MSTest.TestAdapter
    Install-Package Moq
    
  • Review the project structure in the solution explorer.

Build and Run Test Cases:

  • Build the solution and run the code coverage, this will run all the test cases and show you the test code coverage.

Build and Run Test Cases:

Run the project and test all the CRUD API methods. (Make sure CleanArch.Api is set as a startup project)

  • Swagger UI

  • Running API without authentication throws error.

  • Add API Authorization.

  • POST - Add new record.

  • GET - Get All records.

  • PUT - Update the existing record.

  • GET - Get single record.

  • DELETE - Delete the existing record.

cleanarch's People

Contributors

sandeepkumar17 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.