Hi all,
consider the following simplified example:
public class Ticket
{
public int Id;
public TicketState State;
public Ticket()
{
// from where do I get the "New" state entity here? with its id and name
State = State.New;
}
public void Finished()
{
// from where do I get the "Finished" state entity here? with its id and name
State = State.Finished;
}
}
public class TicketState
{
public int Id;
public string Name;
}
The class state is used directly within the domain object ticket. Later in the ticket s lifecycle other states might be set.
The ticket is persisted into a Ticket table, as well as the TicketState. So within the DB the ticket will have a foreign key to the ticket state table.
When setting the appropiate state within my entity, how do I load the state instance from the DB? Do I have to inject a repository into the entity? Do I need to use a framework like castle for such case? Or are there better solutions, maybe passing the state from outside?
public class Ticket
{
//...
public ITicketStateRepository stateRep; //<-- inject
public Ticket()
{
State = stateRep.GetById(NEW_STATE_ID);
}
//...
}
Is there any best practice? So far I did not use any dependency injection framework or anything and kept any persistence things out of my domain..
Another approch:
public class Ticket
{
//...
public Ticket(NewTicketState newTicketState)
{
State = newTicketState;
}
public void Finished(FinishedTicketState finishedTicketState)
{
State = finishedTicketState;
}
//...
}