Separated Presentation // Observer Pattern // Events and Delegates
Separated Presentation
Ao aplicar este padrão, a apresentação e a lógica de negócio são mantidas em camadas separadas, divididas por dois módulos lógicos com responsabilidades distintas e bem definidas.Observer Pattern
As alterações são despoletadas por meio de eventos e tratadas pela lógica de negócio conseguindo-se baixo acoplamento.Exemplo
Exemplo básico deste conceito aplicado à gestão de estado em memória.Lógica de Negócio
IStateValue.cspublic interface IStateValue
{
object Value { get; set; }
string KeyName { get; set; }
void SetDefault();
object this[string KeyName] { get; set; }
}
CachingEventArgs.cs
public class CachingEventArgs : EventArgs
{
public IStateValue ApplicationStateBase { get; set; }
}
public delegate void InitializeStorageContextEventHandler(object sender, IStateValue stateValue);
ICachingEvent.cs
public interface ICachingEvent
{
void OnInitializeStorageContext();
event InitializeStorageContextEventHandler InitializeStorageContext;
}
Controller.cs
public class Controller
{
private IStateValue _stateValue;
public void InitializeStorage(IStateValue stateValue)
{
_stateValue = stateValue;
CheckValue();
}
public void CheckValue()
{
var result = _stateValue?.Value;
if (result == null)
{
_stateValue.SetDefault();
}
}
}
Logic.cs
public class Logic
{
readonly Controller _controller;
ICachingEvent _caching;
public Logic(ICachingEvent caching)
{
_controller = new Controller();
_caching = caching;
_caching.InitializeStorageContext += InitializeStorage;
}
private void InitializeStorage(object sender, IStateValue stateValue)
{
_controller.InitializeStorage(stateValue);
}
}
Apresentação
Camada desenvolvida em ASP.NET Web Forms.StateValue.cs
public class StateValue<T> : IStateValue where T : class
{
private string _keyName;
private HttpSessionState _sessionState;
private T _value;
#region IStateValue Members
public StateValue() { StateStorage = HttpContext.Current.Session; }
public StateValue(string keyName) : this()
{
Add(keyName);
}
public HttpSessionState StateStorage
{
get { return _sessionState; }
set { _sessionState = value; }
}
#endregion
#region IStateValue Implementation
public object this[string KeyName]
{
get { return _sessionState[KeyName]; }
set { _sessionState[KeyName] = (T) value; }
}
public object Value
{
get
{
if (StateStorage == null || StateStorage[_keyName] == null)
{
return _value;
}
return StateStorage[_keyName];
}
set
{
_value = (T) value;
if (StateStorage != null)
{
StateStorage[_keyName] = (T) value;
}
}
}
public string KeyName
{
get { return _keyName; }
set { _keyName = value; }
}
public void SetDefault()
{
_value = default(T);
}
#endregion
public void Add(string keyName)
{
Add(keyName, default(T));
}
public void Add(string keyName, T value)
{
if (StateStorage[keyName] == null)
{
_sessionState.Add(keyName, value);
_value = value;
}
_keyName = keyName;
}
public void ClearAll()
{
_sessionState.Clear();
}
public void Clear(string keyName)
{
StateStorage[keyName] = null;
}
}
StateValueForm.aspx
<div>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<br />
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
</div>
StateValueForm.aspx.cs
public partial class StateValueForm : System.Web.UI.Page, ICachingEvent
{
Logic logic;
IStateValue stateValue;
public event InitializeStorageContextEventHandler InitializeStorageContext;
protected override void OnPreInit(EventArgs e)
{
logic = new Logic(this);
base.OnPreInit(e);
}
protected void Page_Load(object sender, EventArgs e)
{
stateValue = new StateValue<string>("FirstKey");
}
protected void Button1_Click(object sender, EventArgs e)
{
stateValue.Value = Guid.NewGuid().ToString();
OnInitializeStorageContext();
Label1.Text = (string)stateValue.Value;
}
public void OnInitializeStorageContext()
{
if (InitializeStorageContext != null)
{
InitializeStorageContext(this, stateValue);
}
}
}
Referências: Exploring the Observer Design Pattern, Observer Design Pattern (Delegates/Events)
Licença CC BY-SA 4.0
Silvia Pinhão Lopes, 4.1.18
Sem comentários: