mardi 20 décembre 2016

Empty interface for "grouping" method's output

I have a method to handle output from external service, this output is DataSet and contains various columns depends on parameters. For example ([] means DataSet.Rows):

ExternalService.GetOutput("Foo") returns [{A: 1, B: 2,}, {A: 1, B: 3}]
ExternalService.GetOutput("Bar") returns [{C: 1, D: 2, E: 5}]
ExternalService.GetOutput("Sth") returns [{F: 2}, {F: 1}, {F: 18}]

so, as you can see returned properties can have nothing in common (it's kind of dictionary), and returned output depends on parameter. Important thing is that this parameters : "Foo", "Bar", "Sth", are rather static and I have custom classes to handle each of the case.

My solution is to create such classes:

public class MyResponse {
    public List<IOutputItem> Items { get; set; }
}
public interface IOutputItem {
}
public class FooOutputItem : IOutputItem {
    public int A { get;set;}
    public int B { get;set;}
}
//same for Bar and Sth

with using them this way:

public MyResponse GetExternalOutput(string parameter) {
    var provider = getCustomProvider(parameter);
    return provider.GetOutput();
}
public class FooProvider {
    public MyResponse GetOutput() {
        List<FooOutputItem> results = MapDataSet(ExternalService.GetOutput("Foo"));
        return new MyResponse { Items = results };
    }
}

Question

As you can see I've used there an empty interface to group possible outputs of GetExternalOutput method, but I know that using empty interface is usually not so good idea. I've read about some "marker interface", and possible replace them with custom parameters, but I don't see such possibility here. So the question is: Is this approach correct? Or should I rather do this different way?

Aucun commentaire:

Enregistrer un commentaire