Product existence check / Asynchronous domain #31
-
Hi Kamil, very great example and reading by the way! How can you make sure that the products you are adding to an order ( Where do you think this check would take place? In my opinion it is a domain concern. Adding an invalid item to the invoice would break a domain rule. In this case, an But that leads me to another question. public async Task<OrderId> PlaceOrderAsync(
List<OrderProductData> orderProductsData,
List<ProductPriceData> allProductPrices,
string currency,
List<ConversionRate> conversionRates,
IProductExistenceChecker productExistenceChecker)
{
await CheckRuleAsync(new ProductsExistRule(orderProductsData, productExistenceChecker));
CheckRule(new CustomerCannotOrderMoreThan2OrdersOnTheSameDayRule(_orders));
CheckRule(new OrderMustHaveAtLeastOneProductRule(orderProductsData));
var order = Order.CreateNew(orderProductsData, allProductPrices, currency, conversionRates);
this._orders.Add(order);
this.AddDomainEvent(new OrderPlacedEvent(order.Id, this.Id, order.GetValue()));
return order.Id;
} Or should caching be the way to go and stay synchronous? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
From my experience, the best place to check if something exists is an Application Layer (Application Service / Command Handler). If a product does not exist it means that the command is invalid so the operation should be aborted immediately (fail-fast principle). In Application Service you can use async/await so it will be more efficient too. However, this is the case when the boundary between the application layer and domain layer is blurry. So you can put this logic in Domain but in my opinion, it is not a good idea and I would stick to the Application Layer. I try to avoid I/O operation in Domain Layer (even hidden behind the interface) and make Domain pure (but this is only a general rule) |
Beta Was this translation helpful? Give feedback.
Hi @chrishanzlik
From my experience, the best place to check if something exists is an Application Layer (Application Service / Command Handler). If a product does not exist it means that the command is invalid so the operation should be aborted immediately (fail-fast principle). In Application Service you can use async/await so it will be more efficient too.
However, this is the case when the boundary between the application layer and domain layer is blurry. So you can put this logic in Domain but in my opinion, it is not a good idea and I would stick to the Application Layer. I try to avoid I/O operation in Domain Layer (even hidden behind the interface) and make Domain pure (but this i…