My idea is to not not expose/use service's Proxy everywhere throughout the application, rather exercising all service communications(using client Proxy) from a single class. To achieve this I have created below class: -
namespace MyOrganization.EventSender
{
using MyOrganization.EventSender.DTOs;
using MyOrganization.EventSender.Proxy;
using MyOrganization.EventSender.Utilities;
using log4net;
using Newtonsoft.Json;
using System;
using System.ServiceModel;
using System.ServiceProcess;
/// <summary>
/// The User Session Detail Sender
/// </summary>
public class SessionDataSender : ISessionDataSender
{
private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
private SessionDataDTO _sessionDataDTO;
public SessionDataSender(SessionDataDTO sessionDataDTO)
{
_sessionDataDTO = sessionDataDTO;
}
public bool Send()
{
try
{
Proxy.MessageProcessorProxy.SessionDetails sd = new Proxy.MessageProcessorProxy.SessionDetails();
sd.ChangeTime = _sessionDataDTO.ChangeTime;
sd.ChangeType = _sessionDataDTO.ChangeType;
sd.Username = _sessionDataDTO.Username;
sd.SessionId = _sessionDataDTO.SessionId;
log.Info($"Sending SessionEvent [{sd.ChangeType}] to Web-Srver.");
var client = new Proxy.MessageProcessorProxy.MessageProcessorClient();
var response = client.LogEventDetails(sd);
Proxy.ProxyFactory.CloseProxy(client as ICommunicationObject);
log.Info($"Web-Server Response: [{response.ResponseMessage}]. Reference: [ {JsonConvert.SerializeObject(sd)} ] ");
return true;
}
catch (Exception ex)
{
log.Error($"Exception: [{ex.ToString()}]");
return false;
}
}
}
}
Now, my questions are: -
- Is it good to map every DTOs into Proxy's DataContract? As my this class will grow with more service calls with different DTOs.
- How this class should be architect so that it doesn't violate SRP/Design-patterns?