Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(MinimalAPIs): Support AddMasaMinimalAPIs #432

Merged
merged 1 commit into from
Feb 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,41 @@ public static WebApplication AddServices(
return app;
}

public static IServiceCollection AddMasaMinimalAPIs(
this IServiceCollection services,
Action<ServiceGlobalRouteOptions>? action = null)
{
if (services.All(service => service.ImplementationType != typeof(MinimalApisMarkerService)))
{
services.AddSingleton<MinimalApisMarkerService>();

services.AddHttpContextAccessor();
if (action == null)
{
services.Configure<ServiceGlobalRouteOptions>(_ =>
{

});
}
else
{
services.Configure(action);
}

MasaApp.TrySetServiceCollection(services);
var serviceProvider = services.BuildServiceProvider();
var serviceMapOptions = serviceProvider.GetRequiredService<IOptions<ServiceGlobalRouteOptions>>().Value;
var serviceTypes = TypeHelper.GetServiceTypes<ServiceBase>(serviceMapOptions.Assemblies.ToArray());
foreach (var serviceType in serviceTypes)
{
GlobalMinimalApiOptions.AddService(serviceType);
services.AddSingleton(serviceType);
}
}

return services;
}

private sealed class MinimalApisMarkerService
{

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

// ReSharper disable once CheckNamespace

namespace Microsoft.AspNetCore.Builder;

public static class WebApplicationExtensions
{
public static void MapMasaMinimalAPIs(this WebApplication webApplication)
{
MasaApp.Build(webApplication.Services);
GlobalMinimalApiOptions.WebApplication = webApplication;

var serviceMapOptions = webApplication.Services.GetRequiredService<IOptions<ServiceGlobalRouteOptions>>().Value;
foreach (var serviceType in GlobalMinimalApiOptions.ServiceTypes)
{
var serviceInstance = (ServiceBase)webApplication.Services.GetRequiredService(serviceType);
if (serviceInstance.RouteOptions.DisableAutoMapRoute ?? serviceMapOptions.DisableAutoMapRoute ?? false)
return;

serviceInstance.AutoMapRoute(serviceMapOptions, serviceMapOptions.Pluralization);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

// ReSharper disable once CheckNamespace

namespace Masa.Contrib.Service.MinimalAPIs;

[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
internal static class GlobalMinimalApiOptions
{
#pragma warning disable S2223
public static WebApplication? WebApplication;
#pragma warning restore S2223
public static List<Type> ServiceTypes { get; private set; } = new();

public static void AddService(Type serviceType)
{
if (ServiceTypes.Contains(serviceType))
return;

ServiceTypes.Add(serviceType);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

// ReSharper disable once CheckNamespace

namespace Masa.Contrib.Service.MinimalAPIs;

[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
internal static class TypeHelper
{
public static IEnumerable<Type> GetServiceTypes<TService>(params Assembly[] assemblies)
where TService : class
=> from type in assemblies.SelectMany(assembly => assembly.GetTypes())
where !type.IsAbstract && BaseOf<TService>(type)
select type;

private static bool BaseOf<T>(Type type)
{
if (type.BaseType == typeof(T)) return true;

return type.BaseType != null && BaseOf<T>(type.BaseType);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

// ReSharper disable once CheckNamespace

namespace Microsoft.AspNetCore.Builder;

public abstract class ServiceBase : IService
{
public WebApplication App => MasaApp.GetRequiredService<WebApplication>();
private WebApplication? _webApplication;

public WebApplication App => _webApplication ??= GlobalMinimalApiOptions.WebApplication ?? MasaApp.GetRequiredService<WebApplication>();

public string BaseUri { get; init; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ public void TestAddMultiServices()
public void AddService()
{
var app = _builder.AddServices();
Assert.IsTrue(_builder.Services.Any(service => service.ServiceType == typeof(CustomService) && service.Lifetime == ServiceLifetime.Scoped));
Assert.IsTrue(_builder.Services.Any(service
=> service.ServiceType == typeof(CustomService) && service.Lifetime == ServiceLifetime.Scoped));

var servicePrvider = _builder.Services.BuildServiceProvider();
var customService = servicePrvider.GetService<CustomService>();
Expand All @@ -46,4 +47,21 @@ public void AddService()

Assert.IsTrue(newCustomService.GetTest2() == 1);
}

[TestMethod]
public void TestMapMasaMinimalAPIs()
{
_builder.Services.AddMasaMinimalAPIs();
Assert.IsTrue(_builder.Services.Any<CatalogService>());
Assert.IsTrue(_builder.Services.Any<CustomService>());
Assert.IsTrue(_builder.Services.Any<GoodsService>());
Assert.IsTrue(_builder.Services.Any<UserService>());

var app = _builder.Build();

app.MapMasaMinimalAPIs();

var service = (ServiceBase)app.Services.GetRequiredService(typeof(CatalogService));
Assert.AreEqual(app, service.App);
}
}