# HG changeset patch # User jorge.rowies # Date 1307289983 10800 # Node ID 23325dddddcc6070dfdd1697d6cc63643d3e16a1 # Parent 92c5a12015f394faa8923c9cb2e23432a5b06d07 Persistencia del estado de los eventos diff -r 92c5a12015f3 -r 23325dddddcc Agendas/trunk/src/Agendas.Domain/Evento.cs --- a/Agendas/trunk/src/Agendas.Domain/Evento.cs Sun Jun 05 11:57:34 2011 -0300 +++ b/Agendas/trunk/src/Agendas.Domain/Evento.cs Sun Jun 05 13:06:23 2011 -0300 @@ -8,7 +8,6 @@ protected Evento() { _tracks = new List(); - _estado = new EventoNullState(this); } #region Propiedades del Evento @@ -54,17 +53,71 @@ get { return _tracks; } } - private EventoState _estado; + /// + /// Estado del evento en formato string (para persistencia a DB) + /// + public virtual string Estado + { + get + { + if (_eventoState != null) + return _eventoState.GetDescripcion(); + + return string.Empty; + } + set + { + if (value != Estado) + { + switch (value) + { + case EventoPropuestoState.Descripcion: + { + _eventoState = new EventoPropuestoState(); + break; + } + case EventoAgendadoState.Descripcion: + { + _eventoState = new EventoAgendadoState(); + break; + } + case EventoConfirmadoState.Descripcion: + { + _eventoState = new EventoConfirmadoState(); + break; + } + case EventoPublicadoState.Descripcion: + { + _eventoState = new EventoPublicadoState(); + break; + } + } + } + } + } + + private EventoState _eventoState; /// - /// Estado del evento + /// Obtiene una instancia de la clase que representa el estado del evento /// - public virtual EventoState Estado + public virtual EventoState GetEstado() { - get { return _estado; } - protected internal set { _estado = value; } + if (_eventoState == null) + SetEstado(new EventoNullState()); + + return _eventoState; } + /// + /// Asigna la instancia de la clase que representa el estado del evento + /// + /// Instancia que representa el estado + public virtual void SetEstado(EventoState eventoState) + { + _eventoState = eventoState; + } + #endregion #region Acciones sobre el evento @@ -79,7 +132,7 @@ public static Evento Proponer(string titulo, Persona persona, string urlInvitacion) { var evento = new Evento {Titulo = titulo, Ponente = persona, UrlInvitacion = urlInvitacion}; - evento.Estado.Promover(Accion.Proponer); + evento.GetEstado().Promover(evento, Accion.Proponer); return evento; } @@ -111,7 +164,7 @@ Ponente = persona; Fecha = fecha; UrlInvitacion = urlInvitacion; - this.Estado.Promover(Accion.Agendar); + this.GetEstado().Promover(this, Accion.Agendar); } public virtual void Actualizar(Persona persona, DateTime? fecha, string urlInvitacion) @@ -131,12 +184,12 @@ public virtual void Confirmar() { - this.Estado.Promover(Accion.Confirmar); + this.GetEstado().Promover(this, Accion.Confirmar); } public virtual void Publicar() { - this.Estado.Promover(Accion.Publicar); + this.GetEstado().Promover(this, Accion.Publicar); } #endregion diff -r 92c5a12015f3 -r 23325dddddcc Agendas/trunk/src/Agendas.Domain/EventoAgendadoState.cs --- a/Agendas/trunk/src/Agendas.Domain/EventoAgendadoState.cs Sun Jun 05 11:57:34 2011 -0300 +++ b/Agendas/trunk/src/Agendas.Domain/EventoAgendadoState.cs Sun Jun 05 13:06:23 2011 -0300 @@ -4,24 +4,24 @@ { public class EventoAgendadoState : EventoState { - private readonly Evento _evento; + public const string Descripcion = "Agendado"; - public EventoAgendadoState(Evento evento) - { - _evento = evento; - _evento.AddTrack(new Track(_evento, Accion.Agendar)); - } - - public override void Promover(Accion accion) + public override void Promover(Evento evento, Accion accion) { switch (accion) { case Accion.Confirmar: - _evento.Estado = new EventoConfirmadoState(_evento); + evento.SetEstado(new EventoConfirmadoState()); + evento.AddTrack(new Track(evento, Accion.Confirmar)); break; default: throw new AccionNoSoportadaException(this.GetType(), accion); } } + + public override string GetDescripcion() + { + return Descripcion; + } } } \ No newline at end of file diff -r 92c5a12015f3 -r 23325dddddcc Agendas/trunk/src/Agendas.Domain/EventoConfirmadoState.cs --- a/Agendas/trunk/src/Agendas.Domain/EventoConfirmadoState.cs Sun Jun 05 11:57:34 2011 -0300 +++ b/Agendas/trunk/src/Agendas.Domain/EventoConfirmadoState.cs Sun Jun 05 13:06:23 2011 -0300 @@ -4,24 +4,24 @@ { public class EventoConfirmadoState : EventoState { - private readonly Evento _evento; + public const string Descripcion = "Confirmado"; - public EventoConfirmadoState(Evento evento) - { - _evento = evento; - _evento.AddTrack(new Track(_evento, Accion.Confirmar)); - } - - public override void Promover(Accion accion) + public override void Promover(Evento evento, Accion accion) { switch (accion) { case Accion.Publicar: - _evento.Estado = new EventoPublicadoState(_evento); + evento.SetEstado(new EventoPublicadoState()); + evento.AddTrack(new Track(evento, Accion.Publicar)); break; default: throw new AccionNoSoportadaException(this.GetType(), accion); } } + + public override string GetDescripcion() + { + return Descripcion; + } } } \ No newline at end of file diff -r 92c5a12015f3 -r 23325dddddcc Agendas/trunk/src/Agendas.Domain/EventoNullState.cs --- a/Agendas/trunk/src/Agendas.Domain/EventoNullState.cs Sun Jun 05 11:57:34 2011 -0300 +++ b/Agendas/trunk/src/Agendas.Domain/EventoNullState.cs Sun Jun 05 13:06:23 2011 -0300 @@ -4,26 +4,26 @@ { public class EventoNullState : EventoState { - private readonly Evento _evento; - - public EventoNullState(Evento evento) - { - _evento = evento; - } - - public override void Promover(Accion accion) + public override void Promover(Evento evento, Accion accion) { switch (accion) { case Accion.Proponer: - _evento.Estado = new EventoPropuestoState(_evento); + evento.SetEstado(new EventoPropuestoState()); + evento.AddTrack(new Track(evento, Accion.Proponer)); break; case Accion.Agendar: - _evento.Estado = new EventoAgendadoState(_evento); + evento.SetEstado(new EventoAgendadoState()); + evento.AddTrack(new Track(evento, Accion.Agendar)); break; default: throw new AccionNoSoportadaException(this.GetType(), accion); } } + + public override string GetDescripcion() + { + return "NullState"; + } } } \ No newline at end of file diff -r 92c5a12015f3 -r 23325dddddcc Agendas/trunk/src/Agendas.Domain/EventoPropuestoState.cs --- a/Agendas/trunk/src/Agendas.Domain/EventoPropuestoState.cs Sun Jun 05 11:57:34 2011 -0300 +++ b/Agendas/trunk/src/Agendas.Domain/EventoPropuestoState.cs Sun Jun 05 13:06:23 2011 -0300 @@ -4,24 +4,24 @@ { public class EventoPropuestoState : EventoState { - private readonly Evento _evento; + public const string Descripcion = "Propuesto"; - public EventoPropuestoState(Evento evento) - { - _evento = evento; - _evento.AddTrack(new Track(_evento, Accion.Proponer)); - } - - public override void Promover(Accion accion) + public override void Promover(Evento evento, Accion accion) { switch (accion) { case Accion.Agendar: - _evento.Estado = new EventoAgendadoState(_evento); + evento.SetEstado(new EventoAgendadoState()); + evento.AddTrack(new Track(evento, Accion.Agendar)); break; default: throw new AccionNoSoportadaException(this.GetType(), accion); } } + + public override string GetDescripcion() + { + return Descripcion; + } } } \ No newline at end of file diff -r 92c5a12015f3 -r 23325dddddcc Agendas/trunk/src/Agendas.Domain/EventoPublicadoState.cs --- a/Agendas/trunk/src/Agendas.Domain/EventoPublicadoState.cs Sun Jun 05 11:57:34 2011 -0300 +++ b/Agendas/trunk/src/Agendas.Domain/EventoPublicadoState.cs Sun Jun 05 13:06:23 2011 -0300 @@ -4,17 +4,16 @@ { public class EventoPublicadoState : EventoState { - private readonly Evento _evento; + public const string Descripcion = "Publicado"; - public EventoPublicadoState(Evento evento) - { - _evento = evento; - _evento.AddTrack(new Track(_evento, Accion.Publicar)); - } - - public override void Promover(Accion accion) + public override void Promover(Evento evento, Accion accion) { throw new AccionNoSoportadaException(this.GetType(), accion); } + + public override string GetDescripcion() + { + return Descripcion; + } } } \ No newline at end of file diff -r 92c5a12015f3 -r 23325dddddcc Agendas/trunk/src/Agendas.Domain/EventoState.cs --- a/Agendas/trunk/src/Agendas.Domain/EventoState.cs Sun Jun 05 11:57:34 2011 -0300 +++ b/Agendas/trunk/src/Agendas.Domain/EventoState.cs Sun Jun 05 13:06:23 2011 -0300 @@ -7,6 +7,8 @@ { public abstract class EventoState { - public abstract void Promover(Accion accion); + public abstract void Promover(Evento evento, Accion accion); + + public abstract string GetDescripcion(); } } diff -r 92c5a12015f3 -r 23325dddddcc Agendas/trunk/src/Agendas.Tests/AgendarTests.cs --- a/Agendas/trunk/src/Agendas.Tests/AgendarTests.cs Sun Jun 05 11:57:34 2011 -0300 +++ b/Agendas/trunk/src/Agendas.Tests/AgendarTests.cs Sun Jun 05 13:06:23 2011 -0300 @@ -213,7 +213,7 @@ var evento = DefaultEventoRepository.GetEventosSinFecha().Single(e => e.Titulo == "Html 5"); - Assert.IsInstanceOf(typeof(EventoPropuestoState), evento.Estado); + Assert.IsInstanceOf(typeof(EventoPropuestoState), evento.GetEstado()); Assert.That(evento.GetTrackNews().Count(t => t.Accion == Accion.Proponer) == 1); } @@ -227,7 +227,7 @@ var evento = DefaultEventoRepository.GetEventosConFecha().Single(e => e.Titulo == "Html 5"); - Assert.IsInstanceOf(typeof(EventoAgendadoState), evento.Estado); + Assert.IsInstanceOf(typeof(EventoAgendadoState), evento.GetEstado()); Assert.That(evento.GetTrackNews().Count(t => t.Accion == Accion.Agendar) == 1); } @@ -243,7 +243,7 @@ agenda.Confirmar(evento.Id); - Assert.IsInstanceOf(typeof(EventoConfirmadoState), evento.Estado); + Assert.IsInstanceOf(typeof(EventoConfirmadoState), evento.GetEstado()); Assert.That(evento.GetTrackNews().Count(t => t.Accion == Accion.Confirmar) == 1); } @@ -260,7 +260,7 @@ agenda.Confirmar(evento.Id); agenda.Publicar(evento.Id); - Assert.IsInstanceOf(typeof(EventoPublicadoState), evento.Estado); + Assert.IsInstanceOf(typeof(EventoPublicadoState), evento.GetEstado()); Assert.That(evento.GetTrackNews().Count(t => t.Accion == Accion.Publicar) == 1); }