Skip to content

Instantly share code, notes, and snippets.

View dcomartin's full-sized avatar

Derek Comartin dcomartin

View GitHub Profile
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);
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)
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)
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;
public class CustomerOrder : IOrder
{
private readonly CatalogContext _dbContext;
public CustomerOrder(CatalogContext dbContext)
{
_dbContext = dbContext;
}
public string GetShippingAddress(int orderId)
public interface IOrder
{
string GetShippingAddress(int orderId);
}
public interface Basket
{
int Additem(int basketId, int catalogItemId, decimal price, int quantity);
}
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();
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)
public class ClassA
{
private readonly ClassB _classB;
private readonly ClassC _classC;
public ClassA(ClassB classB, ClassC classC)
{
_classB = classB;
_classC = classC;
}