This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public async Task<IActionResult> OnPost(CatalogItemViewModel productDetails) | |
{ | |
if (productDetails?.Id == null) | |
{ | |
return RedirectToPage("/Index"); | |
} | |
await SetBasketModelAsync(); | |
var result = await _basketService.AddItemToBasket(BasketModel.Id, productDetails.Id, productDetails.Price, productDetails.Quantity); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IBasketService | |
{ | |
Task<Either<int, QuantityValidationError>> AddItemToBasket(int basketId, int catalogItemId, decimal price, int quantity = 1); | |
} | |
public class QuantityValidationError | |
{ | |
public string Message { get; set; } | |
public QuantityValidationError(string message) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IBasketService | |
{ | |
Task<int> AddItemToBasket(int basketId, int catalogItemId, decimal price, int quantity = 1) | |
} | |
public class BasketService : IBasketService | |
{ | |
private readonly IAsyncRepository<Basket> _basketRepository; | |
public BasketService(IAsyncRepository<Basket> basketRepository) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Either<TLeft, TRight> | |
{ | |
private readonly TLeft _left; | |
private readonly TRight _right; | |
private readonly bool _isLeft; | |
public Either(TLeft left) | |
{ | |
_left = left; | |
_isLeft = true; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CustomerOrder : IOrder | |
{ | |
private readonly CatalogContext _dbContext; | |
public CustomerOrder(CatalogContext dbContext) | |
{ | |
_dbContext = dbContext; | |
} | |
public string GetShippingAddress(int orderId) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IOrder | |
{ | |
string GetShippingAddress(int orderId); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface Basket | |
{ | |
int Additem(int basketId, int catalogItemId, decimal price, int quantity); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ScopedLifetimeTest | |
{ | |
[Fact] | |
public async Task ShouldShitTheBed() | |
{ | |
var serviceCollection = new ServiceCollection(); | |
serviceCollection.AddScoped<ClassA>(); | |
serviceCollection.AddScoped<ClassB>(); | |
serviceCollection.AddScoped<ClassC>(); | |
var provider = serviceCollection.BuildServiceProvider(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ClassC | |
{ | |
private string? _state; | |
public async Task<int> NotThreadSafe(string state) | |
{ | |
_state = state; | |
// Delay this Task so we can end up having ClassB change the state before we return the length | |
if (state.Length > 0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ClassA | |
{ | |
private readonly ClassB _classB; | |
private readonly ClassC _classC; | |
public ClassA(ClassB classB, ClassC classC) | |
{ | |
_classB = classB; | |
_classC = classC; | |
} |