Skip to content

Commit

Permalink
Fix/issue 826 support cross thread minimal api (dotnet-architecture#827)
Browse files Browse the repository at this point in the history
* remove repository global variable to suuport muliple thread (call by handle method)

* add parallel call tests

Co-authored-by: cedri <cedri@BAS>
  • Loading branch information
michelcedric and cedri authored Dec 20, 2022
1 parent a72dd77 commit 707f869
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 54 deletions.
10 changes: 4 additions & 6 deletions src/PublicApi/CatalogBrandEndpoints/CatalogBrandListEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ namespace Microsoft.eShopWeb.PublicApi.CatalogBrandEndpoints;
/// <summary>
/// List Catalog Brands
/// </summary>
public class CatalogBrandListEndpoint : IEndpoint<IResult>
public class CatalogBrandListEndpoint : IEndpoint<IResult, IRepository<CatalogBrand>>
{
private IRepository<CatalogBrand> _catalogBrandRepository;
private readonly IMapper _mapper;

public CatalogBrandListEndpoint(IMapper mapper)
Expand All @@ -28,18 +27,17 @@ public void AddRoute(IEndpointRouteBuilder app)
app.MapGet("api/catalog-brands",
async (IRepository<CatalogBrand> catalogBrandRepository) =>
{
_catalogBrandRepository = catalogBrandRepository;
return await HandleAsync();
return await HandleAsync(catalogBrandRepository);
})
.Produces<ListCatalogBrandsResponse>()
.WithTags("CatalogBrandEndpoints");
}

public async Task<IResult> HandleAsync()
public async Task<IResult> HandleAsync(IRepository<CatalogBrand> catalogBrandRepository)
{
var response = new ListCatalogBrandsResponse();

var items = await _catalogBrandRepository.ListAsync();
var items = await catalogBrandRepository.ListAsync();

response.CatalogBrands.AddRange(items.Select(_mapper.Map<CatalogBrandDto>));

Expand Down
10 changes: 4 additions & 6 deletions src/PublicApi/CatalogItemEndpoints/CatalogItemGetByIdEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ namespace Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints;
/// <summary>
/// Get a Catalog Item by Id
/// </summary>
public class CatalogItemGetByIdEndpoint : IEndpoint<IResult, GetByIdCatalogItemRequest>
public class CatalogItemGetByIdEndpoint : IEndpoint<IResult, GetByIdCatalogItemRequest, IRepository<CatalogItem>>
{
private IRepository<CatalogItem> _itemRepository;
private readonly IUriComposer _uriComposer;

public CatalogItemGetByIdEndpoint(IUriComposer uriComposer)
Expand All @@ -26,18 +25,17 @@ public void AddRoute(IEndpointRouteBuilder app)
app.MapGet("api/catalog-items/{catalogItemId}",
async (int catalogItemId, IRepository<CatalogItem> itemRepository) =>
{
_itemRepository = itemRepository;
return await HandleAsync(new GetByIdCatalogItemRequest(catalogItemId));
return await HandleAsync(new GetByIdCatalogItemRequest(catalogItemId), itemRepository);
})
.Produces<GetByIdCatalogItemResponse>()
.WithTags("CatalogItemEndpoints");
}

public async Task<IResult> HandleAsync(GetByIdCatalogItemRequest request)
public async Task<IResult> HandleAsync(GetByIdCatalogItemRequest request, IRepository<CatalogItem> itemRepository)
{
var response = new GetByIdCatalogItemResponse(request.CorrelationId());

var item = await _itemRepository.GetByIdAsync(request.CatalogItemId);
var item = await itemRepository.GetByIdAsync(request.CatalogItemId);
if (item is null)
return Results.NotFound();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ namespace Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints;
/// <summary>
/// List Catalog Items (paged)
/// </summary>
public class CatalogItemListPagedEndpoint : IEndpoint<IResult, ListPagedCatalogItemRequest>
public class CatalogItemListPagedEndpoint : IEndpoint<IResult, ListPagedCatalogItemRequest, IRepository<CatalogItem>>
{
private IRepository<CatalogItem> _itemRepository;
private readonly IUriComposer _uriComposer;
private readonly IMapper _mapper;

Expand All @@ -32,27 +31,27 @@ public void AddRoute(IEndpointRouteBuilder app)
app.MapGet("api/catalog-items",
async (int? pageSize, int? pageIndex, int? catalogBrandId, int? catalogTypeId, IRepository<CatalogItem> itemRepository) =>
{
_itemRepository = itemRepository;
return await HandleAsync(new ListPagedCatalogItemRequest(pageSize, pageIndex, catalogBrandId, catalogTypeId));
})
return await HandleAsync(new ListPagedCatalogItemRequest(pageSize, pageIndex, catalogBrandId, catalogTypeId), itemRepository);
})
.Produces<ListPagedCatalogItemResponse>()
.WithTags("CatalogItemEndpoints");
}

public async Task<IResult> HandleAsync(ListPagedCatalogItemRequest request)
public async Task<IResult> HandleAsync(ListPagedCatalogItemRequest request, IRepository<CatalogItem> itemRepository)
{
await Task.Delay(1000);
var response = new ListPagedCatalogItemResponse(request.CorrelationId());

var filterSpec = new CatalogFilterSpecification(request.CatalogBrandId, request.CatalogTypeId);
int totalItems = await _itemRepository.CountAsync(filterSpec);
int totalItems = await itemRepository.CountAsync(filterSpec);

var pagedSpec = new CatalogFilterPaginatedSpecification(
skip: request.PageIndex.Value * request.PageSize.Value,
take: request.PageSize.Value,
brandId: request.CatalogBrandId,
typeId: request.CatalogTypeId);

var items = await _itemRepository.ListAsync(pagedSpec);
var items = await itemRepository.ListAsync(pagedSpec);

response.CatalogItems.AddRange(items.Select(_mapper.Map<CatalogItemDto>));
foreach (CatalogItemDto item in response.CatalogItems)
Expand Down
16 changes: 7 additions & 9 deletions src/PublicApi/CatalogItemEndpoints/CreateCatalogItemEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ namespace Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints;
/// <summary>
/// Creates a new Catalog Item
/// </summary>
public class CreateCatalogItemEndpoint : IEndpoint<IResult, CreateCatalogItemRequest>
public class CreateCatalogItemEndpoint : IEndpoint<IResult, CreateCatalogItemRequest, IRepository<CatalogItem>>
{
private IRepository<CatalogItem> _itemRepository;
private readonly IUriComposer _uriComposer;

public CreateCatalogItemEndpoint(IUriComposer uriComposer)
Expand All @@ -31,26 +30,25 @@ public void AddRoute(IEndpointRouteBuilder app)
[Authorize(Roles = BlazorShared.Authorization.Constants.Roles.ADMINISTRATORS, AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)] async
(CreateCatalogItemRequest request, IRepository<CatalogItem> itemRepository) =>
{
_itemRepository = itemRepository;
return await HandleAsync(request);
return await HandleAsync(request, itemRepository);
})
.Produces<CreateCatalogItemResponse>()
.WithTags("CatalogItemEndpoints");
}

public async Task<IResult> HandleAsync(CreateCatalogItemRequest request)
public async Task<IResult> HandleAsync(CreateCatalogItemRequest request, IRepository<CatalogItem> itemRepository)
{
var response = new CreateCatalogItemResponse(request.CorrelationId());

var catalogItemNameSpecification = new CatalogItemNameSpecification(request.Name);
var existingCataloogItem = await _itemRepository.CountAsync(catalogItemNameSpecification);
var existingCataloogItem = await itemRepository.CountAsync(catalogItemNameSpecification);
if (existingCataloogItem > 0)
{
throw new DuplicateException($"A catalogItem with name {request.Name} already exists");
}

var newItem = new CatalogItem(request.CatalogTypeId, request.CatalogBrandId, request.Description, request.Name, request.Price, request.PictureUri);
newItem = await _itemRepository.AddAsync(newItem);
newItem = await itemRepository.AddAsync(newItem);

if (newItem.Id != 0)
{
Expand All @@ -59,7 +57,7 @@ public async Task<IResult> HandleAsync(CreateCatalogItemRequest request)
// In production, we recommend uploading to a blob storage and deliver the image via CDN after a verification process.

newItem.UpdatePictureUri("eCatalog-item-default.png");
await _itemRepository.UpdateAsync(newItem);
await itemRepository.UpdateAsync(newItem);
}

var dto = new CatalogItemDto
Expand All @@ -73,6 +71,6 @@ public async Task<IResult> HandleAsync(CreateCatalogItemRequest request)
Price = newItem.Price
};
response.CatalogItem = dto;
return Results.Created($"api/catalog-items/{dto.Id}", response);
return Results.Created($"api/catalog-items/{dto.Id}", response);
}
}
13 changes: 5 additions & 8 deletions src/PublicApi/CatalogItemEndpoints/DeleteCatalogItemEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,29 @@ namespace Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints;
/// <summary>
/// Deletes a Catalog Item
/// </summary>
public class DeleteCatalogItemEndpoint : IEndpoint<IResult, DeleteCatalogItemRequest>
public class DeleteCatalogItemEndpoint : IEndpoint<IResult, DeleteCatalogItemRequest, IRepository<CatalogItem>>
{
private IRepository<CatalogItem> _itemRepository;

public void AddRoute(IEndpointRouteBuilder app)
{
app.MapDelete("api/catalog-items/{catalogItemId}",
[Authorize(Roles = BlazorShared.Authorization.Constants.Roles.ADMINISTRATORS, AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)] async
(int catalogItemId, IRepository<CatalogItem> itemRepository) =>
{
_itemRepository = itemRepository;
return await HandleAsync(new DeleteCatalogItemRequest(catalogItemId));
return await HandleAsync(new DeleteCatalogItemRequest(catalogItemId), itemRepository);
})
.Produces<DeleteCatalogItemResponse>()
.WithTags("CatalogItemEndpoints");
}

public async Task<IResult> HandleAsync(DeleteCatalogItemRequest request)
public async Task<IResult> HandleAsync(DeleteCatalogItemRequest request, IRepository<CatalogItem> itemRepository)
{
var response = new DeleteCatalogItemResponse(request.CorrelationId());

var itemToDelete = await _itemRepository.GetByIdAsync(request.CatalogItemId);
var itemToDelete = await itemRepository.GetByIdAsync(request.CatalogItemId);
if (itemToDelete is null)
return Results.NotFound();

await _itemRepository.DeleteAsync(itemToDelete);
await itemRepository.DeleteAsync(itemToDelete);

return Results.Ok(response);
}
Expand Down
16 changes: 7 additions & 9 deletions src/PublicApi/CatalogItemEndpoints/UpdateCatalogItemEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ namespace Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints;
/// <summary>
/// Updates a Catalog Item
/// </summary>
public class UpdateCatalogItemEndpoint : IEndpoint<IResult, UpdateCatalogItemRequest>
{
private IRepository<CatalogItem> _itemRepository;
public class UpdateCatalogItemEndpoint : IEndpoint<IResult, UpdateCatalogItemRequest, IRepository<CatalogItem>>
{
private readonly IUriComposer _uriComposer;

public UpdateCatalogItemEndpoint(IUriComposer uriComposer)
Expand All @@ -29,25 +28,24 @@ public void AddRoute(IEndpointRouteBuilder app)
[Authorize(Roles = BlazorShared.Authorization.Constants.Roles.ADMINISTRATORS, AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)] async
(UpdateCatalogItemRequest request, IRepository<CatalogItem> itemRepository) =>
{
_itemRepository = itemRepository;
return await HandleAsync(request);
return await HandleAsync(request, itemRepository);
})
.Produces<UpdateCatalogItemResponse>()
.WithTags("CatalogItemEndpoints");
}

public async Task<IResult> HandleAsync(UpdateCatalogItemRequest request)
public async Task<IResult> HandleAsync(UpdateCatalogItemRequest request, IRepository<CatalogItem> itemRepository)
{
var response = new UpdateCatalogItemResponse(request.CorrelationId());

var existingItem = await _itemRepository.GetByIdAsync(request.Id);
var existingItem = await itemRepository.GetByIdAsync(request.Id);

CatalogItem.CatalogItemDetails details = new(request.Name, request.Description, request.Price);
existingItem.UpdateDetails(details);
existingItem.UpdateBrand(request.CatalogBrandId);
existingItem.UpdateType(request.CatalogTypeId);

await _itemRepository.UpdateAsync(existingItem);
await itemRepository.UpdateAsync(existingItem);

var dto = new CatalogItemDto
{
Expand Down
14 changes: 6 additions & 8 deletions src/PublicApi/CatalogTypeEndpoints/CatalogTypeListEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,31 @@ namespace Microsoft.eShopWeb.PublicApi.CatalogTypeEndpoints;
/// <summary>
/// List Catalog Types
/// </summary>
public class CatalogTypeListEndpoint : IEndpoint<IResult>
public class CatalogTypeListEndpoint : IEndpoint<IResult, IRepository<CatalogType>>
{
private IRepository<CatalogType> _catalogTypeRepository;
private readonly IMapper _mapper;

public CatalogTypeListEndpoint(IMapper mapper)
{
{
_mapper = mapper;
}

public void AddRoute(IEndpointRouteBuilder app)
{
app.MapGet("api/catalog-types",
app.MapGet("api/catalog-types",
async (IRepository<CatalogType> catalogTypeRepository) =>
{
_catalogTypeRepository = catalogTypeRepository;
return await HandleAsync();
return await HandleAsync(catalogTypeRepository);
})
.Produces<ListCatalogTypesResponse>()
.WithTags("CatalogTypeEndpoints");
}

public async Task<IResult> HandleAsync()
public async Task<IResult> HandleAsync(IRepository<CatalogType> catalogTypeRepository)
{
var response = new ListCatalogTypesResponse();

var items = await _catalogTypeRepository.ListAsync();
var items = await catalogTypeRepository.ListAsync();

response.CatalogTypes.AddRange(items.Select(_mapper.Map<CatalogTypeDto>));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
using Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints;
using Microsoft.eShopWeb.Web.ViewModels;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net;
using System.Threading.Tasks;

namespace PublicApiIntegrationTests.CatalogItemEndpoints
Expand Down Expand Up @@ -45,5 +48,26 @@ public async Task ReturnsCorrectCatalogItemsGivenPageIndex1()

Assert.AreEqual(totalExpected, model2.CatalogItems.Count());
}

[DataTestMethod]
[DataRow("catalog-items")]
[DataRow("catalog-brands")]
[DataRow("catalog-types")]
[DataRow("catalog-items/1")]
public async Task SuccessFullMutipleParallelCall(string endpointName)
{
var client = ProgramTest.NewClient;
var tasks = new List<Task<HttpResponseMessage>>();

for (int i = 0; i < 100; i++)
{
var task = client.GetAsync($"/api/{endpointName}");
tasks.Add(task);
}
await Task.WhenAll(tasks.ToList());
var totalKO = tasks.Count(t => t.Result.StatusCode != HttpStatusCode.OK);

Assert.AreEqual(0, totalKO);
}
}
}

0 comments on commit 707f869

Please sign in to comment.