抽象-如何为Dart/Flutter中的几种类型创建通用静态方法



好吧,我正试图为我的应用程序减少大量的样板代码。现在我看到了一些重构和严肃抽象的必要性。

我想为我的数据库代码抽象CRUD操作。现在我正在创建一个单独的文件,并为每个对象复制代码。我觉得肯定有更好的方法,但我不知道如何在使用泛型进行抽象的同时保持显式类型安全。

我目前正在为每个对象做这样的事情:
我相信你可以看到这会让人发疯,我的应用程序中有45多个对象。。。很多都比这更复杂。

abstract class HouseDatabaseAPI {
/// CREATE EVENT
/// Create house for authorized user
static Future<void> createHouse({required House newHouse}) async {
Box<House> houseBox = await Hive.openBox<House>('house_box');
await houseBox.put(newHouse);
}
/// READ EVENT
/// Get house for authorized user
static Future<House?> getHouse() async {
Box<House> houseBox = await Hive.openBox<House>('house_box');
House? house = houseBox.getAt(0);
return house;
}
/// UPDATE EVENT
/// Update house for authorized user
static Future<void> updateHouse({House? updatedHouse}) async {
Box<House> houseBox = await Hive.openBox<House>('house_box');
await houseBox.putAt(0, updatedHouse!);
}
/// DELETE EVENT
/// Delete house from the local machine
static Future<void> deleteGroup() async {
Box<House> houseBox = await Hive.openBox<House>('house_box');
await houseBox.deleteAt(0);
}
}

当然,我希望它是严格类型的,而不是动态的。我希望能够做的而不是大量的流量控制语句(粗略的伪代码(:


enum DatabaseAction {
create,
read,
update,
delete,
}
abstract class DatabaseRoutingAPI {

Future<T> globalDatabaseCreateAction({
DatabaseAction databaseAction,
Object object,
String databaseName,
}) async {
Box<T> houseBox = await Hive.openBox<T>(databaseName);
await houseBox.put(object);
}      
...
}

我将引导您从我的关于Hive数据处理的书签中找到一个好的来源->此处

在这里,我将尝试回答您的问题:

abstract class Database {
Box get box;
T get<T>(String id);
List<T> getAll<T>();
Future<void> delete(String id);
Future<void> deleteAll(List<String> keys);
Future<void> addUpdate<T>(String id, T item);
}
class DatabaseImplementing implements Database {
const DatabaseImplementing(this._box);

final Box _box;

@override
Box get box => _box;
@override
T get<T>(String id) {
try {
final data = box.get(id);

if (data == null) {
throw Exception('$T not in the box.');
}

return data;
} catch (_) {
rethrow;
}
}
@override
List<T> getAll<T>() {
try {
final data = box.toMap().values;

if (data.isEmpty) {
throw Exception('$T not in the box.');
}
return data.toList().cast<T>();
} catch (_) {
rethrow;
}
}
@override
Future<void> delete(String id) async {
try {
await box.delete(id);
} catch (_) {
rethrow;
}
}
@override
Future<void> addUpdate<T>(String id, T item) async {
try {
await box.put(id, item);
} catch (_) {
rethrow;
}
}
@override
Future<void> deleteAll(List<String> keys) async {
try {
await box.deleteAll(keys);
} catch (_) {
rethrow;
}
}
}

当然还有很多其他方法可以做到这一点。

最新更新