Repository navigation
Description
MessageHandler base class a little more for some of this code adding some code.
public abstract class MessageHandler
{
public abstract string DeserializeErrorMessage { get; }
public abstract void Run(NamedPipeMessages.Message message);
protected void WriteToClient(NamedPipeMessages.Message message, NamedPipeServer.Connection connection, ITracer tracer)
{
if (!connection.TrySendResponse(message))
{
tracer.RelatedError("Failed to send line to client: {0}", message);
}
}
}
This will make each MessageHandler object responsible to create its own request object in the Run method and have its own specific message for deserialization errors and the request object would not need to be created and passed to the constructor.
Then have a method for creating the specific message handler that would be what gets overridden in the Windows class
public virtual MessageHandler CreateMessageHandler(ITracer tracer, RepoRegistry repoRegistry, NamedPipeServer.Connection connection, string messageHeader)
{
switch (messageHeader)
{
case NamedPipeMessages.RegisterRepoRequest.Header:
return new RegisterRepoHandler(tracer, this.repoRegistry, connection);
...
default:
return null;
}
}
Then this method would go away and you would have this in the HandleRequest
MessageHandler handler = this.CreateMessageHandler(...);
if (handler == null)
{
<log the unknown request>
return;
}
try
{
handler.Run(message);
}
catch (SerializationException ex)
{
activity.RelatedError("{0}: {1}", handler.DeserializeErrorMessage, ex.Message);
}
I hope that makes sense and let me know if you think this is abstracting things too much. The thing I didn't like was the out for the deserializeError and how much it needed to be set.
Also looks like we can move at least the ITracer tracer and NamedPipeServer.Connection connection into the base class and constructor since all the derived classes have them.
Activity