I have created a small ERP application and the architecture I chose included the Repository pattern and the anemic Domain model and Business logic layer. I was reading Microsoft's guide about microservices here:
and I saw some examples like this
and others on various sites. My understanding is that anemic model and BusinessLayer in large systems is an antipattern and logic should reside in the Domain model. Though the model should not (and cannot) query the Database but rather all needed information should be provider as parameters and queried at the CommandHandler (if I m not mistaken). But in some cases the data I need to fetch from the database is needed conditionally. Take this example for instance
public void CalculateDiscount(CommercialDocumentEntry enrty,CommercialDocumentEntryLine line)
{
var customer = RepositoryProvider.CustomerRepositrory.FindbyId(enrty.BillingTraderId);
if (customer.ZoneId.HasValue)
{
var zone = RepositoryProvider.ZoneRepositrory.FindbyId(customer.ZoneId.Value);
line.TDiscountItemPercent = zone.Discount;
}
else if (line.Item.PriceListId.HasValue)
{
var priceList = RepositoryProvider.PriceListRepositror.FindbyId(PriceListId.Value);
line.TDiscountItemPercent = priceList.Discount;
}
else
{
var finData= RepositoryProvider.TraderFinancialDataRepository.FindbyId(enrty.BillingTraderId);
if (finData.TurnOver > 1000)
{
var globalDicount = RepositoryProvider.GlobalDiscoutntsRepository.GetGlobalDiscountbyTurnOver(finData.TurnOver);
line.TDiscountItemPercent = globalDicount.Value;
}
else if (enrty.BTotalValue>1000)
{
var valueDicount = RepositoryProvider.ValueDiscoutntsRepository.GetValueDiscountbyTotal(enrty.BTotalValue);
line.TDiscountItemPercent= customer.IsLocal? valueDicount.LocalDiscount:valueDicount.ForeignDiscount;
}
}
}
If I decide to refactor my code and
- Place CalculateDiscount on the CommercialDocumentEntry aggregate
- Discard CommercialDocumentEntryBusinessLogic and call CalculateDiscount method from a DiacountCommandHandler
- Make all the calls to the database before calling the CalculateDiscount method from the DiacountCommandHandler and pass the results as parameters the CalculateDiscount method
Then I will have made 5 calls to the database in stead of 2. This will eventually deteriorate the performance of my application.
How do I tackle this problem with DDD and CQRS?
Thank you very much for your answers.