原先的模式
GamesEndpoints.cs
| public static class GamesEndpoints |
| { |
| public static RouteGroupBuilder MapGamesEndpoints(this IEndpointRouteBuilder endpoints) |
| { |
| |
| InMemGamesRepository repository = new(); |
| var group = endpoints.MapGroup("/games").WithParameterValidation(); |
| group.MapGet("/", () => repository.GetAll()); |
| return group; |
| } |
| } |
InMemGamesRepository.cs
| using GameStore.Api.Entities; |
| namespace GameStore.Api.Repositoriesa; |
| |
| public class InMemGamesRepository |
| { |
| private readonly List<Game> games = new() |
| { |
| new Game(){ |
| Id = 1, |
| Name="Street Fighter II", |
| Genre ="Fighting", |
| Price = 19.99M, |
| ReleaseDate = new DateTime(1991,2,1), |
| ImgUri = "https://palcehold.co/100" |
| }, |
| new Game(){ |
| Id = 2, |
| Name="FIFA 23", |
| Genre ="Sports", |
| Price = 29.99M, |
| ReleaseDate = new DateTime(2022,2,1), |
| ImgUri = "https://palcehold.co/100" |
| }, |
| |
| }; |
| |
| public IEnumerable<Game> GetAll() |
| { |
| return games; |
| } |
| public Game? Get(int id) |
| { |
| return games.Find(game => game.Id == id); |
| } |
| public void Create(Game game) |
| { |
| game.Id = games.Max(game => game.Id) + 1; |
| games.Add(game); |
| } |
| public void Update(Game updatedGame) |
| { |
| var index = games.FindIndex(game => game.Id == updatedGame.Id); |
| games[index] = updatedGame; |
| } |
| public void Delete(int id) |
| { |
| var index = games.FindIndex(game => game.Id == id); |
| games.RemoveAt(index); |
| } |
| |
| } |
现在的模式
GamesEndpoints.cs
| |
| using System.Diagnostics.CodeAnalysis; |
| using System.Reflection.Metadata; |
| using GameStore.Api.Entities; |
| using GameStore.Api.Repositoriesa; |
| |
| namespace GameStore.Api.Endpoints; |
| |
| public static class GamesEndpoints |
| { |
| |
| const string GetGameEndpointName = "GetGame"; |
| public static RouteGroupBuilder MapGamesEndpoints(this IEndpointRouteBuilder endpoints) |
| { |
| |
| var group = endpoints.MapGroup("/games").WithParameterValidation(); |
| |
| |
| group.MapGet("/", (IGamesRepository repository) => repository.GetAll()); |
| |
| group.MapGet("/{id}", (IGamesRepository repository,int id) => |
| { |
| Game? game = repository.Get(id); |
| if (game is null) |
| { |
| return Results.NotFound(); |
| } |
| return Results.Ok(game); |
| }).WithName(GetGameEndpointName); |
| |
| group.MapPut("/{id}", (IGamesRepository repository,int id, Game updatedGame) => |
| { |
| |
| Game? existingGame = repository.Get(id); |
| if (existingGame is null) |
| { |
| return Results.NotFound(); |
| } |
| existingGame.Name = updatedGame.Name; |
| existingGame.Genre = updatedGame.Genre; |
| existingGame.Price = updatedGame.Price; |
| existingGame.ReleaseDate = updatedGame.ReleaseDate; |
| existingGame.ImgUri = updatedGame.ImgUri; |
| |
| repository.Update(existingGame); |
| |
| return Results.NoContent(); |
| }); |
| |
| group.MapDelete("/{id}", (IGamesRepository repository,int id) => |
| { |
| Game? game = repository.Get (id); |
| if (game is not null) |
| { |
| repository.Delete(id); |
| } |
| return Results.NoContent(); |
| }); |
| |
| |
| return group; |
| |
| } |
| |
| } |
对比
手动实例化
| InMemGamesRepository repository = new(); |
| group.MapGet("/", () => repository.GetAll()); |
依赖注入
依赖注入:
| group.MapGet("/", (IGamesRepository repository) => repository.GetAll()); |
注册服务:
只有在builder中注册了服务,上面的接口在访问的时候才会自动帮你实例化.
| var builder = WebApplication.CreateBuilder(args); |
| builder.Services.AddSingleton<IGamesRepository, InMemGamesRepository>(); |
| |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
2022-01-07 U盘被切割成两个硬盘,如何恢复成一个?