1

私はこれについて研究してきました。「MVCエリア」を見つけましたが、それでも探していることができませんでした。

私の見解:見解/学生/コース学生/情報学生/ステータス学生/一般的な状況など...

コントローラー:

コントローラー/学生

私がやりたいことは:

1つの「Student」コントローラーに多くのビューからのすべてのコードを含めたくありません。コントローラを複数のファイルに「分割」する方法に関するヒントはありますか?私は最も単純なアプローチを見ているだけで、プロジェクトに大きな変更を加えたくありません。

私はMVC4を使用しています。

前もって感謝します!..

PnP

4

3 に答える 3

2

部分的なControllerクラスを作成して、1つのコントローラーを多数の物理ファイルに分割しないのはなぜですか?

また、「多くの視点からのコード」とはどういう意味ですか?別のサービスレイヤーを使用し、そこでビジネスロジックを実行していますか。これがベストプラクティスだからです。コントローラーは、次のようなコードで非常に軽量になるように設計されています。

public ActionMethod DoSomething()
{
    StudentViewModel vm = _studentService.GetSomeData();
    return View(vm);
}
于 2012-11-16T21:02:12.343 に答える
1

部分クラスStudentControllerを実行できます。

フォルダ/ファイルは次のようになります。

Controllers
  StudentController
    StudentController.Status.cs
    StudentController.GeneralSituation.cs
    StudentController.Course.cs

コードは次のようになります。

StudentController.Status.cs:

public partial class StudentController
{
    [Actions relevant for Status of a student]
}

StudentController.GeneralSituation.cs:

public partial class StudentController
{
    [Actions relevant for General Situation of a student]
}
于 2012-11-16T21:06:11.447 に答える
0

エリアが機能しない理由は何ですか?あなたが説明したことから、なぜ彼らがそうしないのか私にはよくわかりません。

- Areas
    - Students
      - Controllers
          HomeController           Handles base /Students/ route
          InformationController    ~/Students/Information/{action}/{id} 
          StatusController         ~/Students/Status/{action}/{id}
          ...
      - Models
      - Views
          Home/
          Information/
          Status/
          ...
          Shared/        Stick common views in here

1つのモンスターコントローラー(またはパーシャル)を設定している場合、コントローラーには実際の「コードの表示」がほとんど含まれていないはずです。モデルを表示するためにすべてを任せます-コントローラーは、ビューデータを構築するために必要なリソースを渡すだけで、コントローラーを薄く保ちます。

つまり、

public class StudentController
{
    ...

    // Actually I prefer to bind the id to a model and handle 404
    // checking there, vs pushing that boiler plate code further down 
    // into the controller, but this is just a quick example.

    public ActionResult Information(int id)
    {
        return View(new InformationPage(this.StudentService, id));
    }
}

次に、InformationPageそのビューに適用可能なすべての情報の構築を処理するモデルの1つです。

public class InformationPage
{
     public Student Student { get; set; }

     public InformationPage(StudentService service, int studentId)
     {
         Student = service.FindStudent(studentId);
         ... Other view data ...
     }
}
于 2012-11-16T21:35:56.503 に答える