From 39b28386ae6d22acb5137a768085ecbc901f6306 Mon Sep 17 00:00:00 2001 From: Huxley Deng Date: Mon, 7 Jul 2025 15:54:44 +0800 Subject: [PATCH] Implement endpoints with Services Dependency Injection. **Note: This implementation is not in minimalAPI way and not optimized, expected to be changed soon.** Add: `UserService` and its interface `IUserService`. Fix: `UserEndpoint` is now in instance class style with DI to work. Fix: change main program to work with above design. --- OptixServe.Api/Endpoints/UserEndpoint.cs | 31 +++++++++---- OptixServe.Api/Program.cs | 58 +++++++++++++++--------- OptixServe.Core/Services/UserService.cs | 25 ++++++++++ 3 files changed, 84 insertions(+), 30 deletions(-) create mode 100644 OptixServe.Core/Services/UserService.cs diff --git a/OptixServe.Api/Endpoints/UserEndpoint.cs b/OptixServe.Api/Endpoints/UserEndpoint.cs index add9286..e824663 100644 --- a/OptixServe.Api/Endpoints/UserEndpoint.cs +++ b/OptixServe.Api/Endpoints/UserEndpoint.cs @@ -1,4 +1,5 @@ using System.Text.Json.Serialization; +using OptixServe.Core.Services; using OptixServe.Api.Dtos; namespace OptixServe.Api.Endpoints; @@ -8,25 +9,37 @@ namespace OptixServe.Api.Endpoints; [JsonSerializable(typeof(IEnumerable))] public partial class UserJsonContext : JsonSerializerContext { } -public static class UserEndpoint +public class UserEndpoint { - public static IEnumerable GetUsers() + private readonly IUserService _userService; + + // 通过构造函数注入依赖 + public UserEndpoint(IUserService userService) { - return [ - new() {Id="1234", UserName = "xxx"}, - new() {Id="5678", UserName = "yyy"}, - ]; + _userService = userService; } public static void Register(WebApplication app) { var group = app.MapGroup("/users"); - group.MapGet("/", GetAllUsers); + group.MapGet("/", (UserEndpoint endpoint) => endpoint.GetAllUsers()); + group.MapGet("/{id}", (string id, UserEndpoint endpoint) => endpoint.GetUserById(id)); } - public static IResult GetAllUsers() + public IResult GetAllUsers() { - return Results.Ok(GetUsers()); + var users = _userService.GetUsers() + .Select(u => new UserDto { Id = u.Id, UserName = u.UserName }); + return Results.Ok(users); + } + + public IResult GetUserById(string id) + { + var user = _userService.GetUserById(id); + if (user == null) + return Results.NotFound(); + + return Results.Ok(new UserDto { Id = user.Id, UserName = user.UserName }); } } \ No newline at end of file diff --git a/OptixServe.Api/Program.cs b/OptixServe.Api/Program.cs index 5f29100..50db17d 100644 --- a/OptixServe.Api/Program.cs +++ b/OptixServe.Api/Program.cs @@ -1,5 +1,6 @@ using System.CommandLine; using OptixServe.Api.Endpoints; +using OptixServe.Core.Services; class Program { @@ -41,6 +42,7 @@ class Program var configFile = parseResult.GetValue(configOption); builder.AddConfigurationWithCommand(configFile); + builder.RegisterServices(); builder.RegiserJsonContext(); var app = builder.Build(); @@ -61,27 +63,6 @@ class Program /// static class ExtensionMethods { - /// - /// Registers all API endpoints - /// - /// WebApplication instance - public static void RegisterEndpoints(this WebApplication app) - { - UserEndpoint.Register(app); - } - - /// - /// Configures JSON serialization options with custom context - /// - /// WebApplicationBuilder instance - public static void RegiserJsonContext(this WebApplicationBuilder builder) - { - builder.Services.ConfigureHttpJsonOptions(options => - { - options.SerializerOptions.TypeInfoResolverChain.Add(UserJsonContext.Default); - }); - } - /// /// Adds configuration sources to the application builder /// @@ -105,4 +86,39 @@ static class ExtensionMethods builder.Configuration.AddConfiguration(configurationBuilder.Build()); } + + /// + /// Configures JSON serialization options with custom context + /// + /// WebApplicationBuilder instance + public static void RegiserJsonContext(this WebApplicationBuilder builder) + { + builder.Services.ConfigureHttpJsonOptions(options => + { + options.SerializerOptions.TypeInfoResolverChain.Add(UserJsonContext.Default); + }); + } + + /// + /// Configures services for DI + /// + /// + public static void RegisterServices(this WebApplicationBuilder builder) + { + // Application services + builder.Services.AddScoped(); + + // WebAPI Endpoint services + builder.Services.AddScoped(); + } + + /// + /// Registers all API endpoints + /// + /// WebApplication instance + public static void RegisterEndpoints(this WebApplication app) + { + UserEndpoint.Register(app); + } + } \ No newline at end of file diff --git a/OptixServe.Core/Services/UserService.cs b/OptixServe.Core/Services/UserService.cs new file mode 100644 index 0000000..8d9d292 --- /dev/null +++ b/OptixServe.Core/Services/UserService.cs @@ -0,0 +1,25 @@ +using OptixServe.Core.Models; + +namespace OptixServe.Core.Services; + +public interface IUserService +{ + IEnumerable GetUsers(); + User? GetUserById(string Id); +} + +public class UserService : IUserService +{ + public User? GetUserById(string Id) + { + throw new NotImplementedException(); + } + + public IEnumerable GetUsers() + { + return [ + new() { Id = "1234", UserName = "xxx", Password = "pass1" }, + new() { Id = "5678", UserName = "yyy", Password = "pass2" } + ]; + } +} \ No newline at end of file