Skip to content

Update to ServiceComposer 4.2.0 and use contractless composition handlers #1284

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

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
Draft
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 @@ -6,7 +6,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="ServiceComposer.AspNetCore" Version="4.1.3" />
<PackageReference Include="ServiceComposer.AspNetCore" Version="4.2.0-beta.3" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ public async Task Get_composed_product_should_return_expected_values()
HttpClient ClientProvider(string name) =>
name switch
{
var val when val == typeof(Shipping.ViewModelComposition.ProductDetailsGetHandler).FullName => _shippingApiClient,
var val when val == typeof(Warehouse.ViewModelComposition.ProductDetailsGetHandler).FullName => _warehouseApiClient,
var val when val == typeof(Shipping.ViewModelComposition.CompositionHandlers.ProductDetailsCompositionHandler).FullName => _shippingApiClient,
var val when val == typeof(Warehouse.ViewModelComposition.CompositionHandlers.ProductDetailsCompositionHandler).FullName => _warehouseApiClient,
var val when val == typeof(Sales.ViewModelComposition.ProductDetailsGetHandler).FullName => _salesApiClient,
var val when val == typeof(Catalog.ViewModelComposition.ProductDetailsGetHandler).FullName => _catalogApiClient,
_ => throw new NotSupportedException($"Missing HTTP client for {name}")
Expand All @@ -72,6 +72,7 @@ HttpClient ClientProvider(string name) =>

// Act
var composedResponse = await compositionClient.GetAsync("/products/details/1");
var stringContent = await composedResponse.Content.ReadAsStringAsync();
dynamic composedViewModel = await composedResponse.Content.AsExpando();

// Assert
Expand All @@ -80,9 +81,10 @@ HttpClient ClientProvider(string name) =>
Assert.Equal("Banana Holder", composedViewModel.ProductName);
Assert.Equal("Outdoor travel cute banana protector storage box", composedViewModel.ProductDescription);
Assert.Equal(10, composedViewModel.ProductPrice);
Assert.Equal(4, composedViewModel.ProductInventory);
Assert.Equal(false, composedViewModel.ProductOutOfStock);

Assert.Equal("Express Delivery, Regular mail", composedViewModel.ProductShippingOptions);
Assert.Equal(false, composedViewModel.ProductOutOfStock);
Assert.Equal(4, composedViewModel.ProductInventory);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="ServiceComposer.AspNetCore" Version="4.1.3" />
<PackageReference Include="ServiceComposer.AspNetCore" Version="4.2.0-beta.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Catalog.ViewModelComposition\Catalog.ViewModelComposition.csproj" />
Expand Down
2 changes: 2 additions & 0 deletions ASP.Net Core API Gateway - 01/CompositionGateway/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddHttpContextAccessor();
services.AddHttpClient();
services.AddRouting();
services.AddViewModelComposition();
services.AddControllers();
}

public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="ServiceComposer.AspNetCore" Version="4.1.3" />
<PackageReference Include="ServiceComposer.AspNetCore" Version="4.2.0-beta.3" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="ServiceComposer.AspNetCore" Version="4.1.3" />
<PackageReference Include="ServiceComposer.AspNetCore" Version="4.2.0-beta.3" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using JsonUtils;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using ServiceComposer.AspNetCore;

namespace Shipping.ViewModelComposition.CompositionHandlers;

public class ProductDetailsCompositionHandler(HttpClient client, IHttpContextAccessor httpContextAccessor)
{
[HttpGet("/products/details/{id}")]
public async Task Get(string id)
{
var url = $"/api/shipping-options/product/{id}";
var response = await client.GetAsync(url);

dynamic productShippingOptions = await response.Content.AsExpando();

var options = ((IEnumerable<dynamic>)productShippingOptions.Options)
.Select(o => o.Option)
.ToArray();

var vm = httpContextAccessor.HttpContext.Request.GetComposedResponseModel();
vm.ProductShippingOptions = string.Join(", ", options);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="ServiceComposer.AspNetCore" Version="4.1.3" />
<PackageReference Include="ServiceComposer.AspNetCore" Version="4.2.0-beta.3" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using ServiceComposer.AspNetCore;
using Shipping.ViewModelComposition.CompositionHandlers;

namespace Shipping.ViewModelComposition
{
public class ViewModelCompositionOptionsCustomization : IViewModelCompositionOptionsCustomization
{
public void Customize(ViewModelCompositionOptions options)
{
options.RegisterHttpClient<ProductDetailsGetHandler>((serviceProvider, httpClient) =>
options.RegisterHttpClient<ProductDetailsCompositionHandler>((serviceProvider, httpClient) =>
{
var configuration = serviceProvider.GetService<IConfiguration>();
var baseAddress = configuration?.GetSection("Shipping:BaseAddress").Value ?? "http://localhost:5004";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System.Net.Http;
using System.Threading.Tasks;
using JsonUtils;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using ServiceComposer.AspNetCore;

namespace Warehouse.ViewModelComposition.CompositionHandlers;

public class ProductDetailsCompositionHandler(HttpClient client, IHttpContextAccessor httpContextAccessor)
{
[HttpGet("/products/details/{id}")]
public async Task Get(string id)
{
var url = $"/api/inventory/product/{id}";
var response = await client.GetAsync(url);

dynamic stockItem = await response.Content.AsExpando();

dynamic vm = httpContextAccessor.HttpContext.Request.GetComposedResponseModel();
vm.ProductInventory = stockItem.Inventory;
vm.ProductOutOfStock = stockItem.Inventory == 0;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
using System;
using ConfigurationUtils;
using Microsoft.Extensions.DependencyInjection;
using ConfigurationUtils;
using ServiceComposer.AspNetCore;
using Warehouse.ViewModelComposition.CompositionHandlers;

namespace Warehouse.ViewModelComposition
{
public class ViewModelCompositionOptionsCustomization : IViewModelCompositionOptionsCustomization
{
public void Customize(ViewModelCompositionOptions options)
{
options.RegisterHttpClient<ProductDetailsGetHandler>("http://localhost:5003");
options.RegisterHttpClient<ProductDetailsCompositionHandler>("http://localhost:5003");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="ServiceComposer.AspNetCore" Version="4.1.3" />
<PackageReference Include="ServiceComposer.AspNetCore" Version="4.2.0-beta.3" />
</ItemGroup>

<ItemGroup>
Expand Down
3 changes: 3 additions & 0 deletions ASP.Net Core API Gateway - 01/nuget.config
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,8 @@
<packageSource key="ServiceComposer packages">
<package pattern="*" />
</packageSource>
<packageSource key="local packages">
<package pattern="*" />
</packageSource>
</packageSourceMapping>
</configuration>