MyWebAPITemplate
1.0.0
這是我自以為是的,不斷地正在進行的,開頭模板,用於構建REST API。
如果您只想開始編碼,想要一個已經經過深思熟慮的項目結構,這是非常簡單且快速學習的,並且可以在較小和較大的項目上工作,則此模板適合您。
app -> App and related files
├─> src -> App source
│ ├─> Web -> Entry point for API - runnable project
│ │ ├─> Controllers -> API endpoints
│ │ ├─> Extensions -> Extension methods
│ │ ├─> Interfaces -> Web project's interfaces
│ │ ├─> Mappers -> Model mappers
│ │ ├─> Middlewares -> Middlewares (e.g. global error handling)
│ │ |─> Models -> Models in and out of API
│ │ ├─> Settings -> Options pattern for App (e.g. connection strings)
│ │ ├─> Validators -> Model validation rules
│ │ ├── Program.cs -> START
│ │ └── Startup.cs -> START
│ ├─> Core -> Holds business logic
│ │ ├─> Dtos -> Data-transfer-object (used for business logic)
│ │ ├─> Entities -> Entity Framework entities for database
│ │ ├─> Interfaces -> Core project's interfaces
│ │ ├─> Mappers -> Dto and Entity mappers
│ │ ├─> Services -> Business logic services
│ └─> Infrastructure -> Data access and external services
│ ├─> Database -> Database access
│ │ ├─> Configurations -> Entity Framework table configurations
│ │ ├─> Migrations -> Entity Framework autogenerated migrations
│ │ ├─> Repositories -> Repository pattern
│ │ ├── AppDbContext.cs -> Base context
│ │ ├── AppSeed.cs -> Database seeding
│ │ └── EfRepository.cs -> Base repository
│ └─> ExternalServices -> Services that access external services (e.g. ext. email service)
└─> tests -> App tests
├── FunctionalTests -> Endpoint tests etc.
│ ├─> Tests -> All tests
│ └─> Utils -> Utility functions (e.g. test database setup)
├── IntegrationTests -> Database tests etc.
│ ├─> Tests -> All tests
│ ├─> Utils -> Utilities functions (e.g. test database setup)
├── UnitTests -> Application logic tests etc.
│ ├─> Tests -> All tests
│ ├─> Utils -> Utility functions
└── Shared -> Model builders and other shared functions for tests
dotnet tool install --global dotnet-ef
dotnet ef database update -c ApplicationDbContext -p . /src/Infrastructure/Infrastructure.csproj -s . /src/Web/Web.csprojMyWebAPITemplate.sln打開F5或Debug/Start Debugging dotnet run -p . /src/Web/當您需要創建新的遷移(更改數據庫架構時)時,請運行此命令以告訴實體框架以處理數據庫所需的架構更改。
dotnet ef migrations add NewMigrationNameHere --context ApplicationDbContext -p . /src/Infrastructure/Infrastructure.csproj -s . /src/Web/Web.csproj -o Database /Migrations 該項目始於我處理以單一整體方式構建API時需要完成的一切信息流的方式。有很多事情要記住,還有很多選擇。因此,我為自己製作了這個模板,以存儲我發現實用的所有實踐,我希望這個模板也可以幫助別人學習一兩個東西。
在此模板中,我以實用格式收集了對API構建的知識。根據最佳實踐,受歡迎程度和我自己的偏好,做出了建築偏好,圖書館選擇和其他選擇。該模板的目的是提供一個很好的基本示例,介紹如何使用可擴展,易於使用的ASP.NET構建REST API,並包含必要的配置和其他好處。該模板的靈感來自Microsoft Eshoponweb參考應用程序和許多其他模板。
該項目已獲得MIT許可 - 因此,無論如何都可以隨意使用它。歡迎建議和幫助。 ?