c# - Remove hacks from my odata generic base Controller? -
i have created genericcontroller move crud logics genericcontroller, , hacked stuff make work like:
- create tablefort set entity
- create interface indexedmodel hold guid make compraion
- hard code db context
how can remove hacks code keep code clean?
namespace airvinyl.api.controllers { public class genericcontroller<t> : odatacontroller t : class, indexedmodel { airvinyldbcontext db = new airvinyldbcontext(); private bool exists(long key) { return tablefort().any(p => p.id.equals(key)); } private dbset<t> tablefort() { return db.set<t>(); } protected override void dispose(bool disposing) { db.dispose(); base.dispose(disposing); } [enablequery] // enablequery allows filter, sort, page, top, etc. public iqueryable<t> get() { return tablefort(); } [enablequery] public singleresult<t> get([fromodatauri] long key) { iqueryable<t> result = get().where(p => p.id.equals(key)); return singleresult.create(result); } public async task<ihttpactionresult> post(t obj) { if (!modelstate.isvalid) { return badrequest(modelstate); } tablefort().add(obj); await db.savechangesasync(); return created(obj); } public async task<ihttpactionresult> patch([fromodatauri] long key, delta<t> delta) { if (!modelstate.isvalid) { return badrequest(modelstate); } var entity = await tablefort().findasync(key); if (entity == null) { return notfound(); } delta.patch(entity); try { await db.savechangesasync(); } catch (dbupdateconcurrencyexception) { if (!exists(key)) { return notfound(); } else { throw; } } return updated(entity); } public async task<ihttpactionresult> put([fromodatauri] long key, t obj) { if (!modelstate.isvalid) { return badrequest(modelstate); } if (key.equals(obj.id) == false) { return badrequest(); } db.entry(obj).state = entitystate.modified; try { await db.savechangesasync(); } catch (dbupdateconcurrencyexception) { if (!exists(key)) { return notfound(); } else { throw; } } return updated(obj); } public async task<ihttpactionresult> delete([fromodatauri] long key) { var entity = await tablefort().findasync(key); if (entity == null) { return notfound(); } tablefort().remove(entity); await db.savechangesasync(); return statuscode(httpstatuscode.nocontent); } } }
after long research , reading
public class genericcontroller<t> : odatacontroller t : class, indexedmodel { private readonly airvinyldbcontext db; private readonly dbset<t> set; public genericcontroller() { this.db = new airvinyldbcontext(); this.set = this.db.set<t>(); } private bool exists(guid key) { return this.set.any(p => p.id.equals(key)); } protected override void dispose(bool disposing) { db.dispose(); base.dispose(disposing); } [enablequery] // enablequery allows filter, sort, page, top, etc. public iqueryable<t> get() { return this.set.asqueryable(); } [enablequery] public singleresult<t> get([fromodatauri] guid key) { iqueryable<t> result = get().where(p => p.id.equals(key)); return singleresult.create(result); } public async task<ihttpactionresult> post(t obj) { if (!modelstate.isvalid) { return badrequest(modelstate); } this.set.add(obj); await db.savechangesasync(); return created(obj); } public async task<ihttpactionresult> patch([fromodatauri] guid key, delta<t> delta) { if (!modelstate.isvalid) { return badrequest(modelstate); } var entity = await this.set.findasync(key); if (entity == null) { return notfound(); } delta.patch(entity); try { await db.savechangesasync(); } catch (dbupdateconcurrencyexception) { if (!exists(key)) { return notfound(); } else { throw; } } return updated(entity); } public async task<ihttpactionresult> put([fromodatauri] guid key, t obj) { if (!modelstate.isvalid) { return badrequest(modelstate); } if (key.equals(obj.id) == false) { return badrequest(); } db.entry(obj).state = entitystate.modified; try { await db.savechangesasync(); } catch (dbupdateconcurrencyexception) { if (!exists(key)) { return notfound(); } else { throw; } } return updated(obj); } public async task<ihttpactionresult> delete([fromodatauri] guid key) { var entity = await this.set.findasync(key); if (entity == null) { return notfound(); } this.set.remove(entity); await db.savechangesasync(); return statuscode(httpstatuscode.nocontent); } } }
Comments
Post a Comment