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? Is declaring MySpclClass as singleton class, a good option in this case?
In short, is there any advantage in using this method:
public class MyController : Controller
{
MySpclClass myObject=new MySpclClass();
public ActionResult DoFirst(int id)
{
....................
myObject.doOneThing();
....................
}
public ActionResult DoSecond(int id)
{
....................
myObject.doAnotherthing();
....................
}
}
over this method:
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();
....................
}
}
And what about this:
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();
....................
}
}
EDIT: Is declaring MySpclClass as singleton class, a good option in this case as Rajansoft1 suggested ? Need suggestions on this.