There is a task associated with changing the state of an object . And I'm pretty confused thinking if this should be an event-based approach that includes something like CQRS , or can I use the State pattern and leave all the logic inside the entities.
I found an article that shows a domain model (or part of it) that uses a state template: http://www.prowareness.com/blog/?p=1448
The ordering system is pretty close to my domain model. So the example is wonderful. But still, I wonder if this is a good practice considering the MVC pattern, and if it can be implemented using RavenDB/NHibernate ?
EDITOR: rethought question
Let's look at an example:
First, here is a domain object 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; } } }
While AbstractState :
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 the state of the factory is defined as follows:
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 good? what are the pros and cons?
- How about extensibility? From my point of view, you can expand / implement your own factory state. But if there is a change in
AbstractState , everything changes accordingly.
Thanks!
lexeme
source share