Adding behavior

The next thing for us to do is to figure out what we can tell our entity to do. Remember, we need to design (and implement) behavior-first. The only reason for us to add those private fields to the entity was actually to support the behavior. As we discussed before, each action that is performed in the system, amends the system state. And those private fields represent just that - state. But again, encapsulation being enforced, we shall not allow manipulating the entity state by changing property values from outside the entity, this will lead us to the dusty land of CRUD. Let's see how we can breathe life into the entity.

namespace Marketplace.Domain
{
public class ClassifiedAd
{
public Guid Id { get; }

public ClassifiedAd(Guid id)
{
if (id == default)
throw new ArgumentException(
"Identity must be specified", nameof(id));

Id = id;
}

public void SetTitle(string title) => _title = title;

public void UpdateText(string text) => _text = text;

public void UpdatePrice(decimal price) => _price = price;

private Guid _ownerId;
private string _title;
private string _text;
private decimal _price;
}
}

We have added three straightforward methods, and you might feel a bit disappointed because these are property setters (not even glorified). But what we do here is expressing the idea of using Ubiquitous Language in code and transfer words from sticky notes (commands in this case) to methods.

Of course, this is just a start. In the next session, we will look deeper into our entity implementation and find out how methods that express behavior can become more useful. 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.45.5