Coder Social home page Coder Social logo

taorodrigueswork / rest-api Goto Github PK

View Code? Open in Web Editor NEW
6.0 2.0 2.0 254 KB

This is a template for building REST APIs using .NET 7 and Entity Framework Core 7. It is a monolith using multilayer architecture and uses some common libraries and configurations very used in the .Net community.

C# 100.00%
dot-net-7 dot-net-core dotnet csharp monolith-architecture multilayer-architecture api-versioning autofixture automapper global-error-handling nunit-tests sql-server sqlite-in-memory validation-filters azure-key-vault integration-test repository-pattern rest-api rest-api-template serilog

rest-api's Introduction

Run Unit Test and Get Code Coverage Quality Gate Status Coverage Bugs Code Smells Lines of Code Maintainability Rating Reliability Rating Security Rating Technical Debt Vulnerabilities

REST API TEMPLATE .NET CORE 7 / ENTITY FRAMEWORK CORE 7

This is a template for building REST APIs using .NET 7 and Entity Framework Core 7. It is a monolith using multilayer architecture and uses some common libraries and configurations very used in the .Net community.

To get started, you need to:

  • clone this repository
  • install SQL Server on your machine
  • replace ConnectionString to this configuration in your appsettings.json:
"ConnectionStrings": {
  "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=ApiTemplate;Trusted_Connection=True;"
}
  • run the application using Visual Studio 2022 or the .NET CLI

Technologies

This project uses the following technologies:

Project Design

This project has classes representing one-to-many and many-to-many relationships, based on the database diagram below. A schedule has a list of days (one-to-many), which also can have a list of person, and each person can belong to multiple days (many-to-many).

image

Project Structure

  • API: This folder contains the source code for the web API.
    • Controllers: This folder contains the controllers that handle incoming requests.
    • Program.c s: This file is responsible for configuring and running the web host. After .NET6 we don't have to use Startup.cs anymore. And the code in the Program file is more concise and simple. TODO: Code Coverage is not ignoring this file when running GitHub Actions using dot net cli and SonarCloud.
    • CustomMiddlewares: Implements the IMiddleware interface and is responsible for global error handling. It intercepts exceptions thrown by the application and throw the corret exception.
    • ValidationFilterAttribute.cs: Validates DTO required properties.
  • Business: This project uses a generic Interface in all classes, in order to make it simple to make dependency injection. We only need to register one time in the Program.cs file and it is going to inject all business classes into the system commented. All classes receive an AutoMapper and a Log via dependency injection.
    • IBusiness: This folder contains the interfaces for the business logic components.
  • Entities: This folder contains the entity models.
    • DTO: This folder contains the data transfer objects (DTOs) used for request and response payloads.
    • Entity: This folder contains the database entities. There is a many-to-many relationship example with an explicit class to handle it. There is also a BaseEntity class with a generic validate method for the entities.
    • MapperProfile: This file contains the AutoMapper mappings profile between DTOs and entity models.
  • Persistence: This folder contains the data access layer components, using Entity Framework Core ORM.
    • Context: It has an ApiContext that inherits from DbContext, which is a class provided by Entity Framework Core that represents a session with the database and allows you to query and save instances of your entity classes.
    • Migrations: Contain all migration files defining the changes to the model that should be applied to the database. NOTE: We are using SQL Server for production and Development and SQLite to run Integration Tests. In order to run the same migrations for different providers, we need to edit the migration and create a conditional to verify if the provider is SQL Server or SQLite. The difference between them is the AutoIncrement syntax for Primary Keys.
    • Repository: This folder contains the repository classes that handle database operations.
    • IRepository: This folder contains the interfaces for the repository classes.
  • Tests: This folder contains the business tests using NUnit, Mock and AutoFixture.

Components

Dockerfile, Azure Container Registry

In a project that utilizes .NET 7, there is no need to create a Dockerfile for containerization. Instead, developers can leverage the convenience of a NuGet package called Microsoft.NET.Build.Containers. To build and push the latest build to the staging environment using Azure Container Registry, all that is required is to execute the build-staging.yml file. (Azure Container Registry is a cloud-based container registry provided by Microsoft's Azure platform. It serves as a centralized repository for storing and managing Docker container images).

Logging

The logging component uses the built-in Serilog library. The logging level can be configured through the appsettings.json file. The logs are sent to Seq, but the configuration to log in an ElasticSearch sink is already in the Program.cs file.

Database Connectivity

The database connectivity is handled by Entity Framework Core. The database connection string can be configured through the appsettings.json file. All the migrations are going to run automatically when the application runs.

DTOs

DTOs are used to prevent exposing entity models directly to the API consumers. It allows for maintaining a clear separation between the application and the database. AutoMapper library is used to map data between DTOs and entity models.

Azure Key Vault

Azure Key Vault is a cloud service that can be used to store secrets and configuration data for .NET or ASP.NET Core applications. Azure Key Vault securely stores secret values and allows access to those values without any explicit code change to both applications hosted in Azure and users based on their Azure credentials.

rest-api's People

Contributors

taorodrigueswork avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  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.