Classified Ad entity

As we have understood from our EventStorming model, we most probably need an entity to represent a classified ad. It seems to be one of the central concepts of our system. We spent a great deal of time discussing our model with domain experts and this term kept coming up in the conversation. It is a perfect indicator that we have identified some vital domain concept also because we keep getting repeated pattern when one command results in one event, and we keep having "classified ad" as an object.

Entities are represented in code as objects, and therefore we need a class so we can create instances of such class. Here is our first attempt to create a class to represent a classified ad:

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

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

You might feel puzzled about this class, and you'd be right to question this kind of entity implementation. It looks like a property bag, and the only difference with a DTO (data transfer object) here is that this class only has one property and all other details are represented by private fields. This class will compile, but it is practically unusable because even the single public property can be only set from inside the class, but we have not exposed any way of doing this.

However, despite being useless, this implementation demonstrates two essential principles that we need to keep in mind while going further. First, all entities need to have an id, and it must be accessible from outside the entity. Second, since we are using an object-oriented language, we shall try to encapsulate as much as we can and keep our internals safe and preferably invisible to the outside world.

To be able to instantiate this class correctly, let's create a constructor that will at least allow us to set the entity id.

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;
}

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

Several things added here:

  • Since we only set the Id property value in the constructor, we can make it a read-only property.
  • When creating an instance of ClassifiedAd, we must supply an id, because there is no parameterless constructor.
  • The supplied id must be valid. Otherwise, the constructor will throw an argument exception.

Right here, we enforced the rule that our entity can only be created giving a valid set of arguments (currently only one), and any created entity of the given type will be by definition legitimate. You might be concerned that a classified ad without some human-readable attributes like title and price is in fact not correct, but this concern is not technical. The business might decide that this is indeed a valid entity.

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

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