In my Angular app, I need to create a persistence service interface that calls a concrete implementation based on the persistence mechanisms available in my browser. I am thinking of implementing this using a generic storageService that calls a specific storageService that understands a storage mechanism.
For example, the generic storageService will provide the following interface:
angular
.module('app.storage')
.factory('storageService', storageService);
function storageService() {
return {
saveItem: saveItem,
getItem: getItem
}
...
}
There will be multiple implementations of the storageService, e.g. localStorageService, indexedDbStorageService, webSqlStorageService.
What is the best way to dynamically inject a concrete storage service based on browser features. For example,
if (isWebSqlAvailable()) {
// inject webSqlStorageService
}
else if (isIndexedDbAvailable() {
// inject indexedDbStorageService
}
else if (isLocalStorageAvailable() {
// inject localStorageService
}