0

I have a question about Repository design pattern, exactly how to interact with many data sources.

For example, i have these repositories:

  • UserRepositorySql
  • UserRepositoryMongo
  • UserRepositoryXml

(Is this even correct? i mean to create an repository for each data source?)

What is better:

  • An abstract class called UserRepository and the other ones extend from UserRepository.

  • Just an interface for these 3 Repositories.

Im not clear to how to retrieve an specific repository for an specific data source, if i use an abstract class, i could implement an UserFactory (factory pattern) class that returns me the specific repository object. (Is this even correct ?)

But how could i retrieve an specific repository if i use an interface? (Since factory pattern needs an parent abstract class, am i right?)

Also when i say "an specific repository" i mean receive a repository with its specific data source.

Thank you so much, redigaffi.

1 Answer 1

1

What is better:

An abstract class called UserRepository and the other ones extend from UserRepository.

Just an interface for these 3 Repositories.

Start with whatever's simplest. IMHO, this is the interface. Add an abstract base class if the need arises.

But how could i retrieve an specific repository if i use an interface?

Use a factory that contains logic to determine which kind of repository to return.

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

2 Comments

Also the idea to use Abstract classes is not bad? my thought were the only "legacy" way is to use interface's. Thank you for your answer, also both ways could be a valid solution?
Sure. Each approach has its pros and cons. The abstract base class will give you the ability to define shared behaviour, whereas the interface will not.

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.