2

I've already read this question on CodeReview. I was hoping for general advice.

I'm writing a service which will go to numerous data sources. Each source requires getting copious amounts of data and changing it to a single format. What is the best pattern for a task like this?

I currently have a "base" class which exposes a static GetData method, which the other classes inherit from and implement. This doesn't seem like the cleanest approach, so I was wondering what other approaches might suit my needs?

5
  • 2
    Why use some "pattern" when simple interface with multiple implementations is fine enough. Can you please explain why this approach "doesn't seem like the cleanest"?
    – Euphoric
    Commented Mar 3, 2014 at 5:51
  • I think the Strategy pattern would work for you as suggested by one of the answers on the aforementioned question. You are probably already doing it with the "base" class, maybe not as complete with respect to the definition.
    – Awemo
    Commented Mar 3, 2014 at 9:43
  • @Euphoric - Perhaps it is the cleanest. I just thought there would be a pattern better suited to this task. Normally I would just use an interface, but as interfaces can't do static methods, I'm left with a base class which exposes the method and returns null, so that the other classes can override. Seems like a code smell to me.
    – Faraday
    Commented Mar 3, 2014 at 15:14
  • @Vijay Maybe you are looking for an abstract class? And I wonder how class can override static method, because that is not possible.
    – Euphoric
    Commented Mar 3, 2014 at 16:54
  • Perhaps override was the wrong term, I'm using the new modifier.
    – Faraday
    Commented Mar 3, 2014 at 17:38

2 Answers 2

2

Sounds like you need a Repository pattern, maybe backed by a Strategy which would choose between different implementations.

How'd you actually do it is exposing a generic interface with your basic CRUD operations and make an implementation for each data source while letting the strategy pattern to decide on the implementation. If you're using dependency injection this might get a little tricky if deciding on which data source you're using is not easily deducted from the beginning.

2
  • Hey, is there a resource you could point me to for a basic example of this?
    – Faraday
    Commented Mar 3, 2014 at 15:15
  • I'm pretty sure Repository is primarily about manipulating the data. Using it just for fetching data is bit overkill.
    – Euphoric
    Commented Mar 3, 2014 at 16:56
0

It seems like there are two parts to your question:

  • Getting the data from multiple sources
  • Transforming the data to the expected format

The task of getting the data from the sources would be handled by the repository. Part of fetching the data is transforming it to it's expected format. The question you have to answer is: "What is the expected class?".

If this data is only used as this final singular type, then each repository will return the single type. A call to MyRepository will internally call each of the Source Repositories.

public class MyRepository {
  ISourceRepository[] Sources;

  private getFromSources(); 
  public getData(); //uses getFromSources
}

Sometimes the data is useful in it's original form as well as the final type, if this is the case, the repositories should return separate types, which you can then transform to the new type with something like the visitor pattern:

public class MyDataFormatter {
  public MyData format(SourceOne data);
  public MyData format(SourceTwo data);
  public MyData format(SourceThree data);
}

public class MyDataRepository {
  public IRepository Repos;
  public function getData() {
    get data from each repo in it's original format
    call MyDataFormatter.format(data)
  }
}

adapted from this link

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.