I was reading this article recently: http://enterprisecraftsmanship.com/2016/09/08/domain-services-vs-application-services/
The writer recommends a class like this:
public sealed class AtmService // Domain service
{
public void WithdrawMoney(Atm atm, decimal amount)
{
if (!atm.CanDispenseMoney(amount))
return;
decimal amountWithCommission = atm.CalculateAmountWithCommission(amount);
Result result = _paymentGateway.ChargePayment(amountWithCommission);
if (result.IsFailure)
return;
atm.DispenseMoney(amount);
}
}
Why doesn't the WithdrawMoney method go in the ATM class rather than the ATMService class? Surely this is almost as bad as putting all the domain logic in the application service?
Q1) Is it bad practice to use a domain object A (or list of domain object A's into domain object B when domain object A (or list of domain object A) is not an instance variable of domain object B? For example:
public class Customer
{
bool valuedCustomer=false;
public void IsValuedCustomer (List<Order> orders)
{
if (orders.Count > 100)
{
//Do some more logic here e.g. look how much they have spent etc
valuedCustomer = true;
}
}
}
Here the Orders List is passed to the Cutomer class so that it can be determined if they are a valued customer.
Q2) Is a domain service just used to figure out a value (could be object, list etc) that is not an instance variable of an entity/value?
Update
Following on from Ewans answer. Say I had a class like this:
public class Customer
{
List<Offer> offers = List<Offer>(); //this is the offers the customer is entitled to e.g. two offers out of ten.
public void AssignOffers (List<Offer> offers)
{
//All available offers (say 100) are passed to this method
//There is logic here to figure out what offers the person is entitled to (this could be a LINQ query). If the person is entitled to an offer then add it to the instance variable list
}
}