MIT License

技术解读

以下内容由 GitRead 中文社区 编辑团队基于项目 README 与官方资料整理, 结合中文开发者的常见使用场景补充技术选型建议。具体以项目官方文档为准。

深度剖析 eShopOnWeb 项目:一个 ASP.NET Core 8 参考应用

项目背景与痛点

eShopOnWeb 是一个由 .NET 社区支持的参考应用程序,使用 ASP.NET Core 8.0 构建。针对企业级应用中常见的痛点,如性能优化、微服务架构、安全性和可维护性等,该项目提供了一个完整的技术和业务框架。它不仅能帮助开发者快速构建现代的 Web 应用,还能够体现最新的 .NET 架构设计思路和技术实践。

核心技术揭秘

该项目引入了多项核心技术来优化架构,其中包括微服务架构、Clean Architecture(整洁架构)、Domain-Driven Design(领域驱动设计)等。下图展示了 eShopOnWeb 的关键技术栈及数据流向。

graph TD A[用户请求] --> B{"身份验证"} B -->|"通过"| C[前端控制器] B -->|"失败"| D[返回错误] C --> E[应用服务] E --> F{"处理业务逻辑"} F -->|"成功"| G[持久化数据] F -->|"失败"| H[返回错误] G --> I[发送通知]

应用服务层通过 Clean Architecture 将业务逻辑进行细粒度划分,并通过 Domain-Driven Design 进一步进行领域建模。而前端控制器则负责维护整个应用架构的清晰条理,保证各层间的解耦和可维护性。

功能亮点与差异

eShopOnWeb 项目的一大亮点在于其对微服务架构的完美实践。该架构通过分解大型应用为多个小型独立服务,提升系统可扩展性和可维护性。此外,它使用 ASP.NET Core 8.0 提供的最新功能,如内置的大幅性能优化,来确保应用的高效运行。

相对其他同类竞品,eShopOnWeb 较为突出的特性包括:

  1. 完善的认证与授权体系:通过与 OpenID Connect 和 OAuth 2.0 的集成,提供了强大而灵活的用户认证解决方案。
  2. 现代化的前端开发:该项目引入了最新前端工具和库,提供了丰富的用户交互体验,同时也便于开发者进行高效编码。
  3. 业务逻辑与基础设施的解耦:通过采用 Clean Architecture 和领域驱动设计,让系统的业务逻辑与实现细节分离,提升代码的可读性和可维护性。

应用场景与落地建议

在生产环境下使用 eShopOnWeb 项目时,需要考虑的关键点包括系统容量规划、性能优化、安全性和运维流程的标准化等。对于业务场景来说,此项目非常适合搭建中大型电商系统、企业门户系统等。

  1. 容量规划:根据实际业务需求进行系统容量的预估,确保能够支持高峰期的并发请求。
  2. 性能优化:注重数据库索引优化、缓存机制的引入以及响应式编程等技术来提升应用性能。
  3. 安全性:需要遵守最新的安全标准和最佳实践,以防止常见的安全威胁,如SQL注入、XSS攻击等。
  4. 运维流程标准化:建立规范的版本控制、CI/CD 管道和自动化测试流程,以确保应用的稳定可靠。

综合评价

总体而言,eShopOnWeb 提供了一个兼顾现代架构理念与企业级应用需求的参考框架,是开发者学习和实践 ASP.NET Core 最佳实践与架构设计的一个极佳范例。然而,尽管该项目的优秀设计和灵活性带来了诸多优势,也需要注意其复杂性和对开发者技能的要求较高的门槛。针对不同的应用场景,开发者可能需要投入更多精力进行调整和优化。

关于

GitHub 原始描述(英文)

Sample ASP.NET Core 8.0 reference application, now community supported: https://github.com/NimblePros/eShopOnWeb

描述取自 GitHub 官方 API,保持仓库作者书写的原文。

语言占比

Bicep
C#
CSS
Dockerfile
HTML
JavaScript
SCSS

README

Microsoft eShopOnWeb ASP.NET Core Reference Application

eShop sample applications have been updated and moved to https://github.com/dotnet/eShop. Active development will continue there. We also recommend the Reliable Web App patterns guidance for building web apps with enterprise app patterns.

A new community supported version of eShopOnWeb can be found at https://github.com/NimblePros/eShopOnWeb

Sample ASP.NET Core reference application, powered by Microsoft, demonstrating a single-process (monolithic) application architecture and deployment model. If you’re new to .NET development, read the Getting Started for Beginners guide.

A list of Frequently Asked Questions about this repository can be found here.

Overview Video

Steve “ardalis” Smith recorded a live stream providing an overview of the eShopOnWeb reference app in October 2020.

eBook

This reference application is meant to support the free .PDF download ebook: Architecting Modern Web Applications with ASP.NET Core and Azure, updated to ASP.NET Core 8.0. Also available in ePub/mobi formats.

You can also read the book in online pages at the .NET docs here: https://docs.microsoft.com/dotnet/architecture/modern-web-apps-azure/

The eShopOnWeb sample is related to the eShopOnContainers sample application which, in that case, focuses on a microservices/containers-based application architecture. However, eShopOnWeb is much simpler in regards to its current functionality and focuses on traditional Web Application Development with a single deployment.

The goal for this sample is to demonstrate some of the principles and patterns described in the eBook. It is not meant to be an eCommerce reference application, and as such it does not implement many features that would be obvious and/or essential to a real eCommerce application.

VERSIONS

The main branch is currently running ASP.NET Core 8.0.
Older versions are tagged.

Topics (eBook TOC)

  • Introduction
  • Characteristics of Modern Web Applications
  • Choosing Between Traditional Web Apps and SPAs
  • Architectural Principles
  • Common Web Application Architectures
  • Common Client Side Technologies
  • Developing ASP.NET Core MVC Apps
  • Working with Data in ASP.NET Core Apps
  • Testing ASP.NET Core MVC Apps
  • Development Process for Azure-Hosted ASP.NET Core Apps
  • Azure Hosting Recommendations for ASP.NET Core Web Apps

Running the sample using Azd template

The store’s home page should look like this:

eShopOnWeb home page screenshot

The Azure Developer CLI (azd) is a developer-centric command-line interface (CLI) tool for creating Azure applications.

You need to install it before running and deploying with Azure Developer CLI.

Windows

powershell -ex AllSigned -c "Invoke-RestMethod 'https://aka.ms/install-azd.ps1' | Invoke-Expression"

Linux/MacOS

curl -fsSL https://aka.ms/install-azd.sh | bash

And you can also install with package managers, like winget, choco, and brew. For more details, you can follow the documentation: https://aka.ms/azure-dev/install.

After logging in with the following command, you will be able to use the azd cli to quickly provision and deploy the application.

azd auth login

Then, execute the azd init command to initialize the environment.

azd init -t dotnet-architecture/eShopOnWeb 

Run azd up to provision all the resources to Azure and deploy the code to those resources.

azd up 

According to the prompt, enter an env name, and select subscription and location, these are the necessary parameters when you create resources. Wait a moment for the resource deployment to complete, click the web endpoint and you will see the home page.

Notes:

  1. Considering security, we store its related data (id, password) in the Azure Key Vault when we create the database, and obtain it from the Key Vault when we use it. This is different from directly deploying applications locally.
  2. The resource group name created in azure portal will be rg-{env name}.

You can also run the sample directly locally (See below).

Running the sample locally

Most of the site’s functionality works with just the web application running. However, the site’s Admin page relies on Blazor WebAssembly running in the browser, and it must communicate with the server using the site’s PublicApi web application. You’ll need to also run this project. You can configure Visual Studio to start multiple projects, or just go to the PublicApi folder in a terminal window and run dotnet run from there. After that from the Web folder you should run dotnet run --launch-profile Web. Now you should be able to browse to https://localhost:5001/. The admin part in Blazor is accessible to https://localhost:5001/admin

Note that if you use this approach, you’ll need to stop the application manually in order to build the solution (otherwise you’ll get file locking errors).

After cloning or downloading the sample you must setup your database. To use the sample with a persistent database, you will need to run its Entity Framework Core migrations before you will be able to run the app.

You can also run the samples in Docker (see below).

Configuring the sample to use SQL Server

  1. By default, the project uses a real database. If you want an in memory database, you can add in the appsettings.json file in the Web folder

    {
       "UseOnlyInMemoryDatabase": true
    }
    
  2. Ensure your connection strings in appsettings.json point to a local SQL Server instance.

  3. Ensure the tool EF was already installed. You can find some help here

    dotnet tool update --global dotnet-ef
    
  4. Open a command prompt in the Web folder and execute the following commands:

    dotnet restore
    dotnet tool restore
    dotnet ef database update -c catalogcontext -p ../Infrastructure/Infrastructure.csproj -s Web.csproj
    dotnet ef database update -c appidentitydbcontext -p ../Infrastructure/Infrastructure.csproj -s Web.csproj
    

    These commands will create two separate databases, one for the store’s catalog data and shopping cart information, and one for the app’s user credentials and identity data.

  5. Run the application.

    The first time you run the application, it will seed both databases with data such that you should see products in the store, and you should be able to log in using the [email protected] account.

    Note: If you need to create migrations, you can use these commands:

    -- create migration (from Web folder CLI)
    dotnet ef migrations add InitialModel --context catalogcontext -p ../Infrastructure/Infrastructure.csproj -s Web.csproj -o Data/Migrations
    
    dotnet ef migrations add InitialIdentityModel --context appidentitydbcontext -p ../Infrastructure/Infrastructure.csproj -s Web.csproj -o Identity/Migrations
    

Running the sample in the dev container

This project includes a .devcontainer folder with a dev container configuration, which lets you use a container as a full-featured dev environment.

You can use the dev container to build and run the app without needing to install any of its tools locally! You can work in GitHub Codespaces or the VS Code Dev Containers extension.

Learn more about using the dev container in its readme.

Running the sample using Docker

You can run the Web sample by running these commands from the root folder (where the .sln file is located):

docker-compose build
docker-compose up

You should be able to make requests to localhost:5106 for the Web project, and localhost:5200 for the Public API project once these commands complete. If you have any problems, especially with login, try from a new guest or incognito browser instance.

You can also run the applications by using the instructions located in their Dockerfile file in the root of each project. Again, run these commands from the root of the solution (where the .sln file is located).

Community Extensions

We have some great contributions from the community, and while these aren’t maintained by Microsoft we still want to highlight them.

eShopOnWeb VB.NET by Mohammad Hamdy Ghanem

FShopOnWeb An F# take on eShopOnWeb by Sean G. Wright and Kyle McMaster

评论

暂无评论