In DDD, why do they often use methods instead of properties?

In DDD examples, I often see the use of methods in which I would probably use a property. Why is this?

For example (from Strengthening Your Domain: Aggregate Construction )

public class Order { public bool IsLocal() { return Customer.Province == BillingProvince; } } 
+4
source share
2 answers

One argument for choosing methods instead of properties is when there is some code that does something. If it just returns some internal field value, use the property. If he has any logic inside or uses some method of using calculation. This makes it clearer for the client of the code that something happens when you call this method.

I think I read in the CLR via CSharp that Microsoft regrets having the DateTime.Now property instead. It returns a new value every time you call it. This should be a method not a property.

+8
source

There is no reason to replace getters with methods specific to DDD. General recommendations apply here (this is done when performing heavy calculations or changing a state).

Setters are another case. Some people even consider them code smells. You should get suspicion every time you see a setter. In the ideal case, the state of an object changes only in methods whose names match the verbs in the domain.

+2
source

Source: https://habr.com/ru/post/1389896/


All Articles