Skip to content

Commit fce1286

Browse files
Update to ServiceComposer and use contractless composition handlers
1 parent 16b8a31 commit fce1286

File tree

11 files changed

+36
-46
lines changed

11 files changed

+36
-46
lines changed

ASP.Net Core API Gateway - 01/Catalog.ViewModelComposition/Catalog.ViewModelComposition.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<ItemGroup>
88
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
9-
<PackageReference Include="ServiceComposer.AspNetCore" Version="4.1.2" />
9+
<PackageReference Include="ServiceComposer.AspNetCore" Version="4.2.0-beta.2" />
1010
</ItemGroup>
1111

1212
<ItemGroup>

ASP.Net Core API Gateway - 01/Composition.Tests/When_calling_composition_gateway.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ HttpClient ClientProvider(string name) =>
5555
name switch
5656
{
5757
var val when val == typeof(Shipping.ViewModelComposition.ProductDetailsGetHandler).FullName => _shippingApiClient,
58-
var val when val == typeof(Warehouse.ViewModelComposition.ProductDetailsGetHandler).FullName => _warehouseApiClient,
58+
var val when val == typeof(Warehouse.ViewModelComposition.CompositionHandlers.ProductDetailsCompositionHandler).FullName => _warehouseApiClient,
5959
var val when val == typeof(Sales.ViewModelComposition.ProductDetailsGetHandler).FullName => _salesApiClient,
6060
var val when val == typeof(Catalog.ViewModelComposition.ProductDetailsGetHandler).FullName => _catalogApiClient,
6161
_ => throw new NotSupportedException($"Missing HTTP client for {name}")

ASP.Net Core API Gateway - 01/CompositionGateway/CompositionGateway.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<TargetFramework>net8.0</TargetFramework>
55
</PropertyGroup>
66
<ItemGroup>
7-
<PackageReference Include="ServiceComposer.AspNetCore" Version="4.1.2" />
7+
<PackageReference Include="ServiceComposer.AspNetCore" Version="4.2.0-beta.2" />
88
</ItemGroup>
99
<ItemGroup>
1010
<ProjectReference Include="..\Catalog.ViewModelComposition\Catalog.ViewModelComposition.csproj" />

ASP.Net Core API Gateway - 01/CompositionGateway/Startup.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ public class Startup
99
{
1010
public void ConfigureServices(IServiceCollection services)
1111
{
12+
services.AddHttpContextAccessor();
1213
services.AddHttpClient();
1314
services.AddRouting();
1415
services.AddViewModelComposition();
16+
services.AddControllers();
1517
}
1618

1719
public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)

ASP.Net Core API Gateway - 01/ConfigurationUtils/ConfigurationUtils.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
</PropertyGroup>
66

77
<ItemGroup>
8-
<PackageReference Include="ServiceComposer.AspNetCore" Version="4.1.2" />
8+
<PackageReference Include="ServiceComposer.AspNetCore" Version="4.2.0-beta.2" />
99
</ItemGroup>
1010

1111
</Project>

ASP.Net Core API Gateway - 01/Sales.ViewModelComposition/Sales.ViewModelComposition.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<ItemGroup>
88
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
9-
<PackageReference Include="ServiceComposer.AspNetCore" Version="4.1.2" />
9+
<PackageReference Include="ServiceComposer.AspNetCore" Version="4.2.0-beta.2" />
1010
</ItemGroup>
1111

1212
<ItemGroup>

ASP.Net Core API Gateway - 01/Shipping.ViewModelComposition/Shipping.ViewModelComposition.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<ItemGroup>
88
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
9-
<PackageReference Include="ServiceComposer.AspNetCore" Version="4.1.2" />
9+
<PackageReference Include="ServiceComposer.AspNetCore" Version="4.2.0-beta.2" />
1010
</ItemGroup>
1111

1212
<ItemGroup>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System.Net.Http;
2+
using System.Threading.Tasks;
3+
using JsonUtils;
4+
using Microsoft.AspNetCore.Http;
5+
using Microsoft.AspNetCore.Mvc;
6+
using ServiceComposer.AspNetCore;
7+
8+
namespace Warehouse.ViewModelComposition.CompositionHandlers;
9+
10+
public class ProductDetailsCompositionHandler(HttpClient client, IHttpContextAccessor httpContextAccessor)
11+
{
12+
[HttpGet("/products/details/{id}")]
13+
public async Task Get(string id)
14+
{
15+
var url = $"/api/inventory/product/{id}";
16+
var response = await client.GetAsync(url);
17+
18+
dynamic stockItem = await response.Content.AsExpando();
19+
20+
dynamic vm = httpContextAccessor.HttpContext.Request.GetComposedResponseModel();
21+
vm.ProductInventory = stockItem.Inventory;
22+
vm.ProductOutOfStock = stockItem.Inventory == 0;
23+
}
24+
}

ASP.Net Core API Gateway - 01/Warehouse.ViewModelComposition/ProductDetailsGetHandler.cs

Lines changed: 0 additions & 35 deletions
This file was deleted.
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
using System;
2-
using ConfigurationUtils;
3-
using Microsoft.Extensions.DependencyInjection;
1+
using ConfigurationUtils;
42
using ServiceComposer.AspNetCore;
3+
using Warehouse.ViewModelComposition.CompositionHandlers;
54

65
namespace Warehouse.ViewModelComposition
76
{
87
public class ViewModelCompositionOptionsCustomization : IViewModelCompositionOptionsCustomization
98
{
109
public void Customize(ViewModelCompositionOptions options)
1110
{
12-
options.RegisterHttpClient<ProductDetailsGetHandler>("http://localhost:5003");
11+
options.RegisterHttpClient<ProductDetailsCompositionHandler>("http://localhost:5003");
1312
}
1413
}
1514
}

0 commit comments

Comments
 (0)