Firstly I have created standard repository using JpaRepository for my entity Foo class to store it in database.
public interface FooRepository extends JpaRepository<Foo, Long> {
}
Furthermore as my second data access object I would like to use my own implementation of this simply CRUD operation interaface, which would store data in file.
public interface FooDao {
Collection<Foo> getAll();
Foo getById(Long id);
void removeById(Long id);
void update(Foo foo);
void insert(Foo foo);
}
And separately it works fine when I have both variable manually declared like this
@Autowired
private FooRepository fooRepository;
@Autowired
private FooDao fooDao;
From services I use some kind of switch statement to identify which data source currently user chose, using helper variable:
private String datasource = "db"; // or "file"
But this solution require using if statement to determine which data access object I need to use, what duplicate code and of course is not elegant.
public Foo getOne(Long id){
Foo result = null;
if(datasource.equals("db"))
result = fooRepository.findOne(id);
else if(datasource().equals("file"))
result = fooDao.getById(id);
return result;
}
How can I change dynamically on runtime between those different and not compatible interfaces? How can I make them compatible to use them interchangeable, how properly write some kind of wrapper?