But this is useful to avoid browser restriction(although there a some other techniques in Dart), and this can be used as a template to provide a restful API for other db.
One interesting aspect of current implementation is such DAO class is designed as generic class.Compared to Hibernate/JPA, this approach is very simple.
[web client]
ICouchDbClientDAO<Expense> dao = new CouchDbClientDAO<Expense>(fromMap, fromJson, toJson);Here, the argument of CouchDbClientDAO's constructor takes fromMap, fromJson, toJson. This is required since I have not provided Json binding library yet, these functions must be defined manually for each entity classes(e.g, Expense).
loadButtom = new ButtonComp(this, "Load", (_) {
dao.fetchAll().then((List<Expense> es){ table.load(es);});
});
newButtom = new ButtonComp(this, "New", (_) {
Expense e = form.create();
});
saveButtom = new ButtonComp(this, "Save", (_) {
if (form.e != null) {
Expense e = form.save();
// this part is tricky..
// since e is fill from InputText, when it is an empty text, it will assign "" instead of null..
if (e.id == "") e.id = null;
if (e.rev == "") e.rev = null;
((e.id == null)?dao.insert(e):dao.update(e)).then((Expense e0){
e.id = e0.id;
e.rev = e0.rev;
table.addOrUpdate(e);
});
}
});
deleteButtom = new ButtonComp(this, "Delete", (_) {
Expense e = form.e;
if (e != null) {
dao.delete(e).then((ok){
if (ok) {
form.delete();
table.delete(e);
}
});
}
});
[server]
void main() {No other code are required to make such entity class persistent.
// register reflection factory
initClassMirrorFactory();
CouchDbDAO<Expense> couchDbDAO = new CouchDbDAO<Expense>(couchdb_host, couchdb_port, dbName, fromMap, toJson);
CouchDbHttpServer server = new CouchDbHttpServer(couchDbDAO, fromJson, toJson);
server.startHttpServer(http_server_host, http_server_port);
}
No comments:
Post a Comment