Giter Site home page Giter Site logo

amitpnk / onion-architecture-asp.net-core Goto Github PK

View Code? Open in Web Editor NEW
774.0 29.0 145.0 2.85 MB

WhiteApp API solution template which is built on Onion Architecture with all essential feature using .NET 5!

Home Page: https://marketplace.visualstudio.com/items?itemName=AmitNaik.OnionArchitecture

License: MIT License

C# 92.12% HTML 4.87% CSS 3.01%
entity-framework csharp c-sharp entity-framework-core asp-net-core aspnetcore onion-architecture onion repository repository-pattern

onion-architecture-asp.net-core's Introduction

Visual Studio Marketplace version Visual Studio Marketplace downloads Visual Studio Marketplace ratings Visual Studio Marketplace version


.NET Core Bugs Code Smells Duplicated Lines (%) Maintainability Rating Quality Gate Status Vulnerabilities Security Rating GitHub license

WhiteApp/QuickApp Onion architecture with ASP.NET Core


Logo

Onion Architecture

WhiteApp or QuickApp API solution template which is built on Onion Architecture with all essential feature using .NET Core!
Explore the docs »

Download from Marketplace · Report Bug · Request Feature

Give a Star! ⭐

If you like or are using this project to learn or start your solution, please give it a star. Thanks!

Support This Project

If you have found this project helpful, either as a library that you use or as a learning tool, please consider buying me a coffee:

Buy Me A Coffee

Table of Contents

Onion Architecture

Onion Architecture was introduced by Jeffrey Palermo to provide a better way to build applications in perspective of better testability, maintainability, and dependability on the infrastructures like databases and services

Onion, Clean or Hexagonal architecture: it's all the same. Which is built on Domain-Driven Desgin approach.

Domain in center and building layer top of it. You can call it as Domain-centric Architecture too.

Reference

About The Project

WhiteApp or QuickApp API solution template which is built on Onion Architecture with all essential feature using .NET Core.

image

Getting Started

Step 1: Download extension from project template

Download from Marketplace

image

Step 2: Create Project

Select project type as API, and select Onion Architecture

image

Step 3: Select Onion Architecture project template

Select project type as API, and select Onion Architecture

image

Step 4: Project is ready

image

Step 5: Configure connection string in appsettings.json

Make sure to connect proper database

  "ConnectionStrings": {
    "OnionArchConn": "Data Source=(local)\\sqlexpress01;Initial Catalog=OnionDb;Integrated Security=True",
    "IdentityConnection": "Data Source=(local)\\sqlexpress01;Initial Catalog=OnionDb;Integrated Security=True"
  }, 

and connect to logging in DB or proer path

  "Serilog": {
    "MinimumLevel": "Information",
    "WriteTo": [
      {
        "Name": "RollingFile",
        "Args": {
++          "pathFormat": "D:\\Logs\\log-{Date}.log",
          "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] {Message}{NewLine}{Exception}"
        }
      },
      {
        "Name": "MSSqlServer",
        "Args": {
++          "connectionString": "Data Source=(local)\\sqlexpress01;Initial Catalog=OnionDb3;Integrated Security=True",
          "sinkOptionsSection": {
            "tableName": "Logs",
            "schemaName": "EventLogging",
            "autoCreateSqlTable": true
          },
          "restrictedToMinimumLevel": "Warning"
        }
      }
    ],
    "Properties": {
      "Application": "Onion Architecture application"
    }
  },

Step 6: Create Database (Sample is for Microsoft SQL Server)

For Code First approach (To run this application, use Code First apporach)

  • For running migration:

    • Option 1: Using Package Manager Console:

      • Open Package Manager Console, select << ProjectName >>.Persistence as Default Project
      • Run these commands:
        PM> add-migration Initial-commit-Application -Context ApplicationDbContext -o Migrations/Application
        PM> add-migration Identity-commit -Context IdentityContext -o Migrations/Identity
        
        PM> update-database -Context ApplicationDbContext 
        PM> update-database -Context IdentityContext 

      Migration

  • Option 2: Using dotnet cli:
    • Install dotnet-ef cli:
      dotnet tool install --global dotnet-ef --version="3.1"
      
    • Navigate to OA and run these commands:
      $ dotnet ef migrations add Initial-commit-Application --context ApplicationDbContext -o Migrations/Application
      $ dotnet ef migrations add Identity-commit-Identity --context IdentityContext -o Migrations/Identity
      $ dotnet ef database update --context ApplicationDbContext 
      $ dotnet ef database update --context IdentityContext 
      

For Database First approach

In Package Manager console in << ProjectName >>.Persistence, run below command

scaffold-dbcontext -provider Microsoft.EntityFrameworkCore.SqlServer -connection "Data Source=(local)\SQLexpress;Initial Catalog=OnionArchitectureDB;Integrated Security=True"

Step 7: Build and run application

Health check UI

Navigate to Health Checks UI https://localhost:44356/healthcheck-ui and make sure everything is green.

** Change port number according to your application

image

Swagger UI

Swagger UI https://localhost:44356/OpenAPI/index.html

** Change port number according to your application

image

Features available in this project

This is default white application for ASP.NET Core API development

This whiteapp contains following features, uncheck feature need to implement yet.

  • Application is implemented on Onion architecture
  • RESTful API
  • Entityframework Core
  • Expection handling
  • Automapper
  • Unit testing via NUnit
  • Integration testing via NUnit
  • Versioning
  • Swagger UI
  • CQRS Pattern

Below features will be implemented in infrastructure layer. You can plug and play based on your project.

  • Loggings - seriLog
  • Email
  • Health checks UI
  • JWT authentication with Microsoft Identity
  • Role based Authorization
  • Fluent validations
  • Database Seeding
  • Enable CORS origin
  • Enable feature flag (Make it true when you configure your email configuration)

Project description

we can see that all the Layers are dependent only on the Core Layers

Domain layer

Domain Layers (Core layer) is implemented in center and never depends on any other layer. Therefore, what we do is that we create interfaces to Persistence layer and these interfaces get implemented in the external layers. This is also known and DIP or Dependency Inversion Principle

Persistence layer

In Persistence layer where we implement reposistory design pattern. In our project, we have implement Entityframework which already implements a repository design pattern. DbContext will be UoW (Unit of Work) and each DbSet is the repository. This interacts with our database using dataproviders

Service layer

Service layer (or also called as Application layer) where we can implement business logic. For OLAP/OLTP process, we can implement CQRS design pattern. In our project, we have implemented CQRS design pattern on top of Mediator design pattern via MediatR libraries

In case you want to implement email feature logic, we define an IMailService in the Service Layer. Using DIP, it is easily possible to switch the implementations. This helps build scalable applications.

Infrastructure Layer

In this layer, we add our third party libraries like JWT Tokens Authentication or Serilog for logging, etc. so that all the third libraries will be in one place. In our project, we have implemented almost all important libraries, you can plug & play (add/remove) based on your project requirement in StartUp.cs file.

Presentation Layer

This can be WebApi or UI.

Licence Used

GitHub license

See the contents of the LICENSE file for details

Contact

Having any issues or troubles getting started? Drop a mail to [email protected] or Raise a Bug or Feature Request. Always happy to help.

onion-architecture-asp.net-core's People

Contributors

amitpnk 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  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

onion-architecture-asp.net-core's Issues

View Models are conflicting with project structure

Hi ,
Its a very good implementation and thanks ton for publishing this on git. I'm having issue with using view models. From GetAllCustomerQuery we should return the viewmodel or dtos but if i do so its conflicting with project architecture. From service i should not add reference to infrastructure right ? Could you please do this implementation.
Thank you in advance.

Thanks

[Need Explanation] Confusion on CustomExceptionMiddleware ?

Hello,
in your template you have defined CustomExceptionMiddleware class in both service & infrastructure layer, now i am unable to understand where should i put my other middlewares and why do we need it in both places? or is it infra calling service layer?

please help

Installing Template Not Work

I'm trying to install this template but it gives me an error.
And finally, I opened your project and installed it using the command line dotnet new install .

26 / 11 / 2022 11:33:14 - Microsoft VSIX Installer
26/11/2022 11:33:14 - ------------------------------------------
26 / 11 / 2022 11:33:14 - vsixinstaller.exe version:
26 / 11 / 2022 11:33:14 - 17.4.2118 - 17 - 4P4Inse - ge4c88902
26 / 11 / 2022 11:33:14 - -------------------------------------------
26 / 11 / 2022 11:33:14 - Command line parameters:
26 / 11 / 2022 11:33:14 - C:\Program Files(x86)\Microsoft Visual Studio\Installer\resources\app\ServiceHub\Services\Microsoft.VisualStudio.Setup.Service\VSIXInstaller.exe, C:\Users\METRO\Downloads\OATemplate.vsix
26 / 11 / 2022 11:33:14 - -------------------------------------------
26 / 11 / 2022 11:33:14 - Microsoft VSIX Installer
26/11/2022 11:33:14 - ------------------------------------------
26 / 11 / 2022 11:33:14 - Initializing Install...
26 / 11 / 2022 11:33:14 - Extension Details...
26 / 11 / 2022 11:33:14 - Identifier : 8BA5060C - 7058 - 4A68 - AD4C - 3FE2429AACBA
26 / 11 / 2022 11:33:14 - Name : Onion Architecture
26/11/2022 11:33:14 - Author : Amit Naik
26/11/2022 11:33:14 - Version : 2.1
26 / 11 / 2022 11:33:14 - Description : WhiteApp or QuickApp API solution template which is built on Onion Architecture with all essential feature using .NET Core
26 / 11 / 2022 11:33:14 - Locale : en - US
26 / 11 / 2022 11:33:14 - MoreInfoURL :
26 / 11 / 2022 11:33:14 - InstalledByMSI : False
26 / 11 / 2022 11:33:14 - SupportedFrameworkVersionRange : [4.5,)
26/11/2022 11:33:14 -
26/11/2022 11:33:14 - SignatureState : Unsigned
26/11/2022 11:33:14 - Supported Products :
26/11/2022 11:33:14 - Microsoft.VisualStudio.Community
26/11/2022 11:33:14 - Version : [15.0,17.0)
26/11/2022 11:33:14 - ProductArchitecture : x86
26/11/2022 11:33:14 -
26/11/2022 11:33:14 - References :
26/11/2022 11:33:14 - Prerequisites :
26/11/2022 11:33:14 - -------------------------------------------------------
26/11/2022 11:33:14 - Identifier : Microsoft.VisualStudio.Component.CoreEditor
26/11/2022 11:33:14 - Name : Visual Studio core editor
26/11/2022 11:33:14 - Version : [15.0,)
26/11/2022 11:33:14 -
26/11/2022 11:33:14 - Signature Details...
26/11/2022 11:33:14 - Extension is not signed.
26/11/2022 11:33:14 -
26/11/2022 11:33:14 - Searching for applicable products...
26/11/2022 11:33:14 - Found installed product - Global Location
26/11/2022 11:33:14 - Found installed product - Visual Studio Community 2022
26/11/2022 11:33:14 - VSIXInstaller.NoApplicableSKUsException: This extension is not installable on any currently installed products.
at VSIXInstaller.ExtensionService.GetInstallableDataImpl(IInstallableExtension extension, String extensionPackParentName, Boolean isRepairSupported, IStateData stateData, IEnumerable1 & skuData) at VSIXInstaller.ExtensionService.GetInstallableData(String vsixPath, String extensionPackParentName, Boolean isRepairSupported, IStateData stateData, IEnumerable1 & skuData)
at VSIXInstaller.ExtensionPackService.IsExtensionPack(IStateData stateData, Boolean isRepairSupported)
at VSIXInstaller.ExtensionPackService.ExpandExtensionPackToInstall(IStateData stateData, Boolean isRepairSupported)
at VSIXInstaller.App.Initialize(Boolean isRepairSupported)
at VSIXInstaller.App.Initialize()
at System.Threading.Tasks.Task`1.InnerInvoke()
at System.Threading.Tasks.Task.Execute()

IdentityContext

Unable to create an object of type 'IdentityContext'. For the different patterns supported at design time.

Net Core version 5 Template?

Hello,

can you release a net core 5 version of the project or guide us how to do the upgrades ourselves. i tried using updated assemblies and framework target but it broke the project for me.

thanks

Getting error 500

Hi,
When I try to connect to my db using the get call in onion architecture api template, am getting below error.

Couldn't find the root cause. Help would be appreciated.

{
"StatusCode": 500,
"ErrorMessage": "Cannot open database "dbname" requested by the login. The login failed.\r\nLogin failed for user 'env name'."
}

Approach for multi HttpPatch Api for One Entity

Hi
I Have 3 patch API With 3 different Dto For modified One Entity :

which approach is better

1- Create 3 services For Modified with 3 different DTOs (All Those Methods are the same)

2- Create 1 Service For Modified With One Dto Called "EditDto" And Map Those 3 DTOs To EditDto

3- Create 1 Service For Modified With Entity As param And Map Those 3 DTOs To Entity

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.