mercredi 2 mai 2018

C# Refactor class that has multiple methods with the same signature

I have a class with multiple methods that have the same signature:

string MethodA(int id);
string MethodB(int id);
string MethodC(int id);

The implementation of these methods differs obviously, but I'm trying to make this more SOLID. I got a long way with Composite pattern, where a Composite class would implement the IDoSomething interface below:

public Interface IDoSomething
{
   string DoSomething(int id);
}

public class CompositeClass : IDoSomething
{
    private readonly IEnumerable<IDoSomething> somethings;
    public CompositeClass(IEnumerable<IDoSomething> somethings)
    {
        this.somethings = somethings;
    }

    public string DoSomething(int id)
    {
        foreach (var s in somethings)
        {
            return s.DoSomething(id);
        }
    }
}

Problem is the result to return. This is different for each call. I could change the signature to string[] but that seems like a hack. Any ideas?

Aucun commentaire:

Enregistrer un commentaire