2

In my application, I have to create a repository which will give me the data objects. Now based on the application mode(user input) I have to get the data from file or from db. So it involves two data sources inside the repository.

What would be the best approach for handling multiple data sources in a repository ?

Thanks!!!

2 Answers 2

1

What would be the best approach for handling multiple data sources in a repository ?

Having multiple implementations of repository. One per data source

E.g. DataBaseDataObjectRepository and FileDataObjectRepository that all implement IDataObjectRepository

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for the response. I am also thinking of same solution. There is one complexity. There are few methods in both DataBaseDataObjectRepository & FileDataObjectRepository which both uses file data source. Do I create a common class and then derive FileDataObjectRepository & DataBaseDataObjectRepository classes's?
> "Do I create a common class and then derive FileDataObjectRepository & DataBaseDataObjectRepository classes's?" I wouldn't use base classes for it. But it is up to you how to reuse code.
So what would be the other good approach to use in that scenario?
If your repository works with one type of data object. E.g. DataObjectA, I just cannot imagine a scenario when it can work with two different data sources.
@Deepak a repository should only be responsible from loading the data from a store (be it a database, a filesystem, or whatever), and persist the data back. You should not make any operations on that data, so indeed, having shared code between a Db and a FileSystem repository sounds a bit weird... but if you have designed your application like that (because patterns are not set in stone), then you can either use a base class, an external injected object, use extension methods, or whatever other means of reusing code you prefer.
|
0

I think an option is that you can implement different repositories such as DatabaseRepository, FileRepository, as the naming implies it only deals with a type of source. Then you can have a DataService, which you can inject the different repositories in. Then in the service you can implement the logic to handle the scenario that it needs to access multiple repositories. The application layer interacts with the DataService.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.