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
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
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