私の mvc3 コントローラーでは、特定のオブジェクトがいくつかのメソッドで使用されています。コントローラーのメンバー変数として宣言し、それにアクセスするためのプロパティを提供する必要がありますか? しかし、アクション メソッドの 1 つがクライアントから呼び出されるたびに何が起こるのでしょうか? オブジェクトは何度も作成されますか? その場合、上記の方法に特に利点はありますか? この場合、MySpclClass をシングルトン クラスとして宣言するのは適切なオプションですか?
要するに、この方法を使用する利点はありますか?
public class MyController : Controller
{
MySpclClass myObject=new MySpclClass();
public ActionResult DoFirst(int id)
{
....................
myObject.doOneThing();
....................
}
public ActionResult DoSecond(int id)
{
....................
myObject.doAnotherthing();
....................
}
}
この方法について:
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();
....................
}
}
そして、これはどうですか:
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();
....................
}
}
編集: MySpclClass をシングルトン クラスとして宣言していますか? Rajansoft1 が提案したように、この場合は適切なオプションですか? これに関する提案が必要です。