I am developing software where each action in my entities need create tasks that will be execute in my infrastructure. When I create a task it is only records in the database. Afterwards, a windows service will execute it.
Actually, I use a Service and my Entities are Anemic, then when I change the state of my entity I also create my tasks to infrastructure.
public void CreateMachine(string name){
var machine = new Machine(){
Name = name,
Status = Status.Releasing;
};
var tasks = new List<Task>(){
new Task("CreateMachine", machine.Name), //infrastructure task
new Task("ChangeMachineSize", machine.Name, machine.Foo), //infrastructure task
new Task("ActivateMachine", machine.Bar), //infrastructure task
new Task("SetMachineEntityStatusToReleased", machine.Name) //business task (go to my database)
_db.Task.Add(tasks);
_db.Msachine.Update(machine);
_db.SaveChanges();
}
}
But I want use rich domain, because my services are getting too complex.
Public Class Machine{
public Machine(string name)
{
Name = name;
Status = Status.Releasing;
//Can I Create Tasks for create machine here?
}
public void Stop(){
Status = Status.Releasing
//Can I create Tasks for stop machine here?
}
[Required]
[StringLength(50)]
public string Name { get; set; }
public Status Status { get; protected set; }
}
Is there a design pattern or best practice for this design? I am violating the Single Responsibility Principle if I add the task creation inside my entity?