Code Monkey home page Code Monkey logo

cleanminimalapi's Introduction

Clean Minimal API

GitHub Workflow Status Codecov Nuget GitHub Sponsors Buy Me A Coffee

This is a template API using a streamlined version of Clean Architecture alongside .NET's Minimal APIs.

Prerequisites

This solution in built on the .NET 8 SDK, you need to install that before it will work for you. If you want to build the Dockerfile you will need to install Docker as well.

Installation

This is a template and you can install it using the dotnet new cli. To install the lastest version of the template run the following command.

dotnet new install CleanMinimalApi.Template

To create a new solution using this template run the following command

dotnet new cleanminimalapi --name {YOUR_SOLUTION_NAMESPACE} --au "{YOU_AUTHORS_NAME}"

Docker

There's a dockerfile included in the build folder and serves the purpose of restoring, building, testing, publishing and then creating a runtime image of the API. Works on my machine.....you can add a version prefix and suffix to version the service in the assembly. The Dockerfile does have stages so you can just run the tests or publish the solution depending on your needs. Review the build-pipeline.yml in the github folder for more detailed usage.

docker build . -t cleanminimalapi:latest --build-arg VERSION_PREFIX {VERSION_NUMBER} -- build-arg VERSION_SUFFIX {PRERELEASE_NAME}

The Github Action does publish an image of this API and you check it out for yourself by runnning this command in docker.

docker pull stphnwlsh/cleanminimalapi

Architecture

This solution is loosely based on Clean Architecture patterns, it's by no means perfect. I prefer to call it "Lean Mean Clean Architecture". Inspiration has been taken from Jason Taylor's Clean Architecture Template, but I have made some structural decisions to take some things further and scaled back others.

There's a little CQRS type stuff going on here but it's more in style than real separated functions for reading and writing as under the covers they are the same data source.

Breaking the Clean Architecture pattern is the fact that the Infrastructure project is referenced by the Presentation project. This is for Dependency Injection purposes, so to protect this a little further, all classes in the Infrastructure project are internal. This stops them being accidentally used in the Presentation project.

Project Structure

It's streamlined into 3 functional projects. All serve their own purpose and segregate aspects of the application to allow easier replacement and updating.

  1. Presentation - Setting up the interactions between the Application layer and the consumer. In the project that's via a Minimal API but it could be many other things. The Minimal API uses endpoints to funnel the actions to the layer that owns the domain.
  2. Application - This project owns the domain and business logic. There's validation of the Commands and Queries and handling of domain entities in their own separated structures. Each domain type has it's own interface to a datasource downstream, this project doesn't care what fulfills this contract, as long as someone does.
  3. Infrastructure - Here's where the database comes into play. Infra owns the data objects and works with the repository interfaces to fetch, create, update and remove object from the source. There's some entity mapping here to allow specific models with attributes to remain in this layer and not bleed through to the Application layer.

Features

There are plenty of handy implementations of features throughout this solution, in no particular order here are some that might interest you.

Resources

This sample would not have been possible without gaining inspiration from the following resources. If you are on your own learning adventure please read the following blogs and documentation.

Connect and Support

If you like this, or want to checkout my other work, please connect with me on LinkedIn, and/or follow me on Medium or GitHub.

If you want to see more updates or more projects then please support me at GitHub Sponsors or Buy Me A Coffee

Please Sponsor Me

cleanminimalapi's People

Contributors

gerrit-amagno avatar github-actions[bot] avatar michael-wolfenden avatar stphnwlsh avatar yusufpapurcu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

cleanminimalapi's Issues

Consider removing the 'AddEntityFramework*' call

Describe the bug
There is a warning related to using AddEntityFrameworkInMemoryDatabase and AddDbContext<MovieReviewsDbContext> at the same time.

Since you're using AddDbContext, this method already adds the necessary EF Core services to the DI container. You might not need to call AddEntityFrameworkInMemoryDatabase explicitly inside AddInfrastructure() extension method in the Infrastructure Layer.

This produces a Warning with the below log message:

[Warning] [Microsoft.EntityFrameworkCore.Infrastructure] 'AddEntityFramework*' was called on the service provider, but 'UseInternalServiceProvider' wasn't called in the DbContext options configuration. Consider removing the 'AddEntityFramework*' call, as in most cases it's not needed and may cause conflicts with other products and services registered in the same service provider.

To Reproduce
Steps to reproduce the behavior:

  1. Run the Application.
  2. Check Console Logs.

Cheers.

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.