c# - Controller Instance created and destroyed for each request in MVC - WebAPI - What dependency scope to inject? -
i developing webapi
application.
and understand webapi
, mvc
, every time request comes, new instance of api-controller created , disposed after request served
.
also, inject dependency use castle-windsor
.
acontainer.register(component .for<idataprovider>() .implementedby<dataprovider>() .lifestylescoped()); acontainer.register(component .for<idataprovider>() .implementedby<dataprovider>() .lifestyleperwebrequest());
the question here confused using lifestylescoped
, lifestyleperrequest
. documentation pretty vast , lost.
can tell me whats impact of using each of them in webapi
context ?
if try think outside of web stack see lifestylescoped not confusing @ all. marks begin , end of object`s lifecycle. have seen implementation of unitofwork pattern scoped lifetime provides capability nested transactions roll - nested units of work - or multiple during same request/action.
lifestyleperwebrequest default lifestyle of controllers - not want controllers live more request, because there serve request , nothing more.
you can inject whatever want controller. of course must take care release components. example - make singleton lifestyle implementation of imyservice , inject controller (if make injected services private members thread safe because method stack frame different every method call). way have same instance of imyservice every request. @ end of app lifecycle or other 'application end' event must release everything. sql connections.
if take further down object resolution graph may turn out have iunitofwork has scoped lifetime , make , dispose several of them during imyservice.dothejob();
as conclusion - may inject whatever want controller remember release of objects have "greater lifetime scope" perwebrequest , not needed after particular request.
Comments
Post a Comment