What is the root of the population in this case?

I have these entities

  • User
  • Task - each task must be assigned to a user
  • Project - a task can be assigned to a project, but this is not necessary, the user must be assigned as the creator of the project
  • TaskStatus - each task must have an assigned status

and I created cumulative roots as follows

Cumulative roots

  • User with objects: Task, Task Status
  • Project

Am I going in the right direction?

+3
source share
1 answer

Yes, that sounds perfectly correct. This is my improvisation in C #:

//aggregate root
public class User{/*first name, last name, contacts, etc.*/}

//aggregate root
public class Project{
  public IList Tasks{get; private set;}
  public User CreatedBy{get; private set;}
  public Project(User createdBy){
    CreatedBy=createdBy;
  }
  public RegisterTask(string taskDescription, User assignedTo){
    Tasks.Add(new Task(taskDescription, assignedTo));
  }
  public StartWorkOn(Task task){
    if(!Tasks.Contains(task))
      throw new Exception("Task is not registered");
    task.StartWork();
  }
}

//entity
public class Task{
  public User AssignedTo{get;private set;}
  public string Description{get; private set;}
  public TaskStatus Status{get; private set;}
  internal Task(string description, User assignedTo){
    AssignedTo=assignedTo;
    Description=description;
    Status=TaskStatus.Pending;
  }
  internal void StartWork(){
    if(Status!=TaskStatus.Pending)
      throw new Exception("Can't start what ongoing");
    Status=TaskStatus.InProgress;
  }
  internal void Finish(){
    if(Status!=TaskStatus.InProgress)
      throw new Exception("Can't finish what has not started");
    Status=TaskStatus.Finished;
  }      
}

//value object
public enum TaskStatus{ Pending, InProgress, Finished }

var me=new User();
_users.Save(me);

var you=new User();
_users.Save(you);

var project=new Project(me);
project.RegisterTask("Have a nice day!", you);
project.StartWorkOn(project.Tasks.First());
_projects.Save(project);

Keep in mind - this is just a sketch. Do not take this too seriously.

+1
source

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


All Articles