Note that there are some explanatory texts on larger screens.

plurals
  1. POMVC controller: Common object in methods
    primarykey
    data
    text
    <p>In my mvc3 controller, a particular object is used in several methods. Should I declare it as a member variable of the controller and provide properties to access that? But what happens with each time one of the action method is called from the client? Will the object be created again and again? Is there any particular advantage for the above mentioned method, if that is the case? <strong>Is declaring MySpclClass as singleton class, a good option in this case?</strong> </p> <p>In short, is there any advantage in using this method: </p> <pre><code> public class MyController : Controller { MySpclClass myObject=new MySpclClass(); public ActionResult DoFirst(int id) { .................... myObject.doOneThing(); .................... } public ActionResult DoSecond(int id) { .................... myObject.doAnotherthing(); .................... } } </code></pre> <p>over this method:</p> <pre><code>public class MyController : Controller { public ActionResult DoFirst(int id) { MySpclClass myObject=new MySpclClass(); .................... myObject.doOneThing(); .................... } public ActionResult DoSecond(int id) { MySpclClass myObject=new MySpclClass(); .................... myObject.doAnotherthing(); .................... } } </code></pre> <p>And what about this:</p> <pre><code> public class MyController : Controller { MySpclClass myObject; public ActionResult DoFirst(int id) { myObject=new MySpclClass(); .................... myObject.doOneThing(); .................... } public ActionResult DoSecond(int id) { myObject=new MySpclClass(); .................... myObject.doAnotherthing(); .................... } } </code></pre> <p><strong>EDIT:</strong> Is declaring MySpclClass as singleton class, a good option in this case as Rajansoft1 suggested ? Need suggestions on this.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload