View on GitHub

TFG-DDD-Theoretical

Theoretical introductory information research about DDD, hex. architecture and CQRS

layout: page title: โ€œentitiesโ€ permalink: /entities

Entities ๐Ÿ‘ป

layer : domain ๐Ÿ”ด

The most common example used to explain a DDD entity is person. A person is well differenced to another beacuse of a unique Id number. We have seen examples like this hundred of times.

In terms of code ,an entity no more than a classic class with its unique Id, its own methods and attributes, but remember that these attributes are value objects


@Entity(name="incidence") //JAVAX PERSISTANCE ANNOTATION
@org.axonframework.modelling.command.AggregateRoot //AXON FRAMEWORK ANNOTATION
public class Incidence extends AggregateRoot{
	
	
	@EmbeddedId //VALUE OBJECT
	@AttributeOverride(name="value", column=@Column(name="incidence_id"))
	@TargetAggregateIdentifier
	IncidenceId id;
	
	@Embedded //VALUE OBJECT
	@AttributeOverride(name="value", column=@Column(name="creator"))
	WorkerId creator;
	
	
	@Transient
	ArrayList <IncidenceAssignation> asignees;
		
	@Embedded //VALUE OBJECT
	IncidenceCreationTimeStamp createdOn;
	
	@Embedded //VALUE OBJECT
	IncidenceShortDescription description;
	
	@Embedded //VALUE OBJECT
	IncidenceStatus status;
	

	private Incidence() {}
	
  //CONSTRUCTOR
	private Incidence(
			IncidenceId id, 
			WorkerId creator, 
			IncidenceCreationTimeStamp createdOn,
			IncidenceShortDescription description) {
		this.id          = id;
		this.creator     = creator;
		this.createdOn   = createdOn;
		this.description = description;
		this.asignees    = new ArrayList<IncidenceAssignation>();
		this.status      = new IncidenceStatus("OPEN");
	}

โ— One important I thing learnt during this research was the law of Demeter ๐Ÿ‡ฌ๐Ÿ‡ท ๐Ÿ‘ which helps us to loose coupling among classes.

An example :

		public void changeStatus(String proposed) {
				this.status = new IncidenceStatus(proposed);
		}

If we had to change the status of an Incidence we should do something like โŽincidence1232343.status = new IncidenceStatus(proposed); โŽ but we donโ€™t want that any class know about the inner classes of Incidence.class, so we just use the changeStatus() method and everything else is transparent. This is also a good software developing practice itself and of course it will appear more in this app.


index