- NativeScript for Angular Mobile Development
- Nathan Walker Nathanael J. Anderson
- 496字
- 2021-07-02 18:41:48
Implementing DatabaseService
Our DatabaseService needs to provide several things:
- A persistent store to save and retrieve any data our app needs.
- It should allow any type of data to be stored; however, we will specifically want it to handle JSON serialization.
- Static keys of all the data we will want to store.
- A static reference to a saved user? Well, yes it could. However, this brings up a point that we will address in a moment.
Regarding the first item, we can use NativeScript's application-settings module. Under the hood, this module provides a consistent API to work with two native mobile APIs:
- iOS: NSUserDefaults: https://developer.apple.com/reference/foundation/userdefaults
- Android: SharedPreferences: https://developer.android.com/reference/android/content/SharedPreferences.html
Regarding serializing JSON data, the application-settings module provides a setString and getString method, which will allow us to use it in conjunction with JSON.stringify and JSON.parse.
Using string values throughout your codebase in several different spots to refer to the same key that should remain constant can become error prone. Because of this, we will keep a typed (for type safety) static hash of valid keys that our app will use. We may only know one at this point in time (authenticated user as 'current-user') but creating this will provide a single spot to scale these out over time.
Four? We will address four in a moment.
Open app/modules/core/services/database.service.ts and modify it to provide a similar API to the web's localStorage API for simplicity:
// angular
import { Injectable } from '@angular/core';
// nativescript
import * as appSettings from 'application-settings';
interface IKeys {
currentUser: string;
}
@Injectable()
export class DatabaseService {
public static KEYS: IKeys = {
currentUser: 'current-user'
};
public setItem(key: string, value: any): void {
appSettings.setString(key, JSON.stringify(value));
}
public getItem(key: string): any {
let item = appSettings.getString(key);
if (item) {
return JSON.parse(item);
}
return item;
}
public removeItem(key: string): void {
appSettings.remove(key);
}
}
This service now provides a way to store an object via setItem, which ensures the object is properly stored as a string via JSON.stringify. It also provides a way to retrieve values via getItem, which also handles the serialization back to an object for us via JSON.parse. We also have the remove API to simply remove values from our persisted store. Lastly, we have a nice static reference to all the valid keys that our persistent store will keep track of.
Now, what about that static reference to the saved user?
We want to be able to easily access our authenticated user from anywhere in the app. We could provide a static reference in our DatabaseService for simplicity, but our aim here is to have a clear separation of concerns. Since we know we will want the ability to show a modal asking the user to register and unlock those recording features, a new service to manage this makes sense. Since we have designed scalable architecture, we can easily add another service into the mix, so let's do that now!
- Effective C#:改善C#代碼的50個有效方法(原書第3版)
- JavaScript+Vue+React全程實例
- Mastering Predictive Analytics with Python
- 快速念咒:MySQL入門指南與進階實戰
- 組態軟件技術與應用
- 青少年信息學競賽
- 持續集成與持續交付實戰:用Jenkins、Travis CI和CircleCI構建和發布大規模高質量軟件
- Web Developer's Reference Guide
- 石墨烯改性塑料
- Android移動應用項目化教程
- R的極客理想:量化投資篇
- R語言實戰(第2版)
- 每個人的Python:數學、算法和游戲編程訓練營
- Java EE輕量級解決方案:S2SH
- Python人工智能項目實戰