c# - WebApi Controller: Accessing database more than once, using Controller's own method? -


i writing a, let's say, nodecontroller project of mine. linked database, , of methods take id node access in ef database. node can have dependency on node, meaning that, if want to, say, "execute" 1 node, must check whether dependency-node has been executed or not, , fail, if not dependencies have been executed.

i present partial versions of 2 of methods, whereas 1 uses other, first, there our controller's constructor, mentioned later on:

private readonly nodedbcontext _context;  public nodecontroller() {     _context = new nodedbcontext(); } 

is correct way use dbcontext, or should limit ourselves using "using" statements whenever perform database-related (getting or putting, in our case)?

[responsetype(typeof(bool))] [httpget] [route("node/{id}/executed")] public async task<ihttpactionresult> executed(int id) {     var node = await nodecontrollerhelper.getnode(_context, id);     if (node == null) return badrequest("there no such node");     return ok(node.executed); } 

as can see, method above uses "_context", nodedbcontext, inheriting dbcontext. correct me pass field-ified dbcontext method retrieve node object database? below method take use of both "nodecontrollerhelper.getnode" method as as executed(int id) method:

[httpput] [route("node/{id}/executed")] public async task<ihttpactionresult> executed(int id, putmessage message) {     //interpret input     var node = await nodecontrollerhelper.getnode(_context, id);     if (node == null) return badrequest("there no such node");      foreach (var condition in node.conditions)     {         bool executed = false;         response = await executed(condition.id).result.executeasync(new cancellationtoken(false));         executed = jsonconvert.deserializeobject<bool>(await response.content.readasstringasync());     if (!executed)     { // precondition has not been executed         await nodecontrollerhelper.unlocknodeifselflockedandsave(_context, node);         return badrequest("one or more preconditions not fulfilled.");     } } 

as can seen, first .getnode call made, meaning there 1 call dbcontext in there, , also, there "executed(condition.id)" call, of course ends calling .getnode method once more. seem have hit sort of rock, put /executed method appears fail on more half of occasions, manages squeeze by, beyond understanding.

i have more general question in regards how can best ensure happiness on ranks of our controller code;

  • what best practice managing our dbcontext? currently, being instantiated in constructor of controller, set private readonly field , dispersed down "deeper" method calls. seems make troubles. when debugging, once reach second call of .getnode method, , make step go past call 'get'ting database's table, debugging mysteriously stops, exception thrown, , client use call api doesn't receive httpresponsemessage. alternative have "using" statements create own short-lived dbcontext, tried implementation earlier , seemed have same issue @ second .getnode call.

below getnode method, reference:

public static async task<node> getnode(nodedbcontext context, int id) {     var node = await context.nodes.findasync(id);     return node; } 

well damn, post ended being longer, had wanted present, although code-cutting has occured well. hope out there sees wrongly done us, , can correct in our perhaps fundamental mistake of implementation...

thank lot in advance, kirluu


Comments

Popular posts from this blog

How can we post XML strings along with text strings in iOS -

How to run C# code using mono without Xamarin in Android? -

c# - cefsharp app remember password option -