There's a task connected with changing state of an entity. And I'm quite confused thinking whether it should be an event-based approach which involves something like CQRS
or I can use a State pattern
and leave all logic inside the entities.
I have found an article which shows a domain model (or a piece of it) that uses a State pattern: http://www.prowareness.com/blog/?p=1448
Ordering system is quite close to my domain model. So the example is great. But still I'm wondering if it is a good practice considering MVC pattern and if it is possible to implement with RavenDB/NHibernate
?
EDIT: question rethought
Let's follow the example:
First, here's a domain entity called Idea
:
[Serializable]
public class Idea : AbstractEntity<Guid> {
private static IStateFactory stateFactory;
private AbstractState state = new InitiatedState();
[Required, StringLength(150)]
public String Title { get; set; }
[Required]
public String ProblemContext { get; set; }
public DateTime CreatedOn { get; set; }
public Guid InitiatorId { get; set; }
[Required]
public Decimal InvestmentAmount { get; set; }
public Boolean IsInitiated {
get { return this.state.IsInitiated; }
}
public Boolean IsRejected {
get { return this.state.IsRejected; }
}
public Boolean IsUnderInitialAssessment {
get { return this.state.IsUnderInitialAssessment; }
}
public Boolean IsConfirmedForImplementation {
get { return this.state.IsConfirmedForImplementation; }
}
}
Whereas AbstractState
is:
public abstract class AbstractState {
public virtual Boolean IsInitiated {
get { return true; }
}
public virtual Boolean IsRejected {
get { return false; }
}
public virtual Boolean IsUnderInitialAssessment {
get { return false; }
}
public virtual Boolean IsConfirmedForImplementation {
get { return false; }
}
}
and state factory interface is defined like this:
public interface IStateFactory {
AbstractState GetState(String state);
}
the final thought is to put the method:
public void AlterState(String stateString) {
this.state = stateFactory.GetState(stateString);
}
- Is the design ok? what are the cons and pros?
- What about extensibility? From my perspective one can extend/implement its own state factory. But if there's a change in
AbstractState
itself everything changes accordingly.
Thanks!