microsoft / microsoft/VFSForGit

Refactor RequestHander in GVFS.Service

Open
#1,032 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

pr: refactoring
Dominant language
C#
Stars
6.1k
Forks
474
Avg merge
2d 4h
Merged PRs (30d)
8

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the MessageHandler base class and the HandleRequest flow in GVFS.Service, then trace the existing derived message handlers and their constructors. Compare how deserialization errors, ITracer, and NamedPipeServer.Connection are currently passed and set. Done means the proposed handler factory and base-class responsibilities are implemented consistently without the existing out parameter.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
backend
Issue type
Refactor
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.