2

UI と BusinessLayer の 2 つのレイヤーを持つ従来の asp.net Web アプリケーションがあります。UI プロジェクトは ASP.NET Web サイト タイプであり、BL はクラス ライブラリ タイプです。BL プロジェクトには、Customer、User、Empoloyee などのアプリのエンティティ用のクラスがあります。各クラスには、データベースから読み取るためのメソッドと、DataReader からオブジェクト プロパティを入力するためのメソッドがあります。つまり、Customer クラスには Customer オブジェクトと Data Access Methods が一緒に含まれています。 .

ここで、MVC もサポートするように Web アプリを変更しました。古いサイト (Web フォーム) は以前と同じように機能し、私が作成しているサイトへの新しいアップグレード (サイトを管理するための管理機能の追加) は ASP.NET MVC3 にあります。ルーティングとすべてが正常に機能します。しかし、プロジェクトの構造/保守性が心配です。

新しい MVC 部分では、CustomerViewModel、EmployeeViewModel などのいくつかのエンティティに対して ViewModel を作成する必要がありました。CustomerService" "という別のクラスを作成GetCustomerViewModelし、そのメソッド内で既存の BusinessLayer から を呼び出し、GetCustomerMethod(既存の BL プロジェクトで言及されているエンティティ タイプの) オブジェクトからプロパティ値を読み取り、それを に割り当てCustomerViewModelます (いくつか調べます)。 AutoMapper はこの後で) オブジェクトをサンプルし、このメソッドからそれを返します。My View は、このオブジェクトを使用して UI にデータを表示します。" " クラスを作成した理由CustomerServiceは、CustomerViewModel オブジェクトに値を設定する前に、if 条件チェックまたはビジネス検証を行う必要がある場合があるためです。コントローラーが薄くなるように、それを「中間層/サービス層」と見なします。

私のカスタマーコントローラーから

public ActionResult Details(int id)
{

   MyProject.MVCViewModel.CustomerViewModel objCustomerVM;
   objCustomerVM=MyProject.MVCMiddleLayer.CustomerService.GetCustomerViewModel(id);

   return View(objCustomerVM); 
}

私のCustomerViewModelで

 public static CustomerViewModel GetCustomerViewModel(int customerId)
    {
       //Create an object of new ViewModel
       CustomerViewModel objCustomerViewModel  = new CustomerViewModel ();

       //Get an object from Existing BL of Customer of type ExistingBL.Customer
       ExistingBL.Customer objCustOld=new Customer(customerId); 

        //Check some properties of the customer object and set values to the new ViewModel object
          if(objCustOld.Type=="normal")
          {
            objCustomerViewModel.Priority=2; 
          }
          else if(objCustOld.Type=="abnormal")
          {
            objCustomerViewModel.Priority=1;
            objCustomerViewModel.Message ="We love you";
          }
         //Some other checking like this....
    return objCustomerViewModel;
   }

これは間違ったアプローチですか?私のコードは乱雑になりますか? ViewModel は既存の BL エンティティから (ほぼ) 重複したコードであるため、満足していません。このシナリオに対処する最善の方法は何ですか。この場合、リポジトリ パターン (ほとんどの例で見た) の使用について確信が持てませんか? 私はそれを行う必要がありますか?それは私のコードをどのように改善しますか?

4

2 に答える 2

0

私がとるアプローチは、リポジトリ パターンに似ています。いくつかの重要なポイントを概説します

  1. 書き換える唯一のものはUIロジック(モデルオブジェクトの表示)であり、UIテクノロジーが異なるため(asp.netとMVC)、それで問題ありません。

  2. 後で依存性注入を実行できるように、インターフェイスの作業を開始することをお勧めします。MVC での依存性注入の最大のメリットは、NUnit テスト ケースを作成するときです。

public static ICustomerViewModel GetCustomerViewModel(int customerId) { // 具体的な実装ではなく DI を使用 ICustomerViewModel objCustomerViewModel = new CustomerViewModel ();

   //use DI, rather than concerete implementation
   ExistingBL.ICustomer objCustOld=new Customer(customerId); 
   .
   .
   .
return objCustomerViewModel;

}

モック フレームワークの助けを借りて、非常に簡単にモック オブジェクトを作成できるようになりました。

于 2011-12-23T05:01:17.587 に答える
0

多かれ少なかれ、私の ViewModel クラスは属性のみを持つプロパティの再定義であり、これは単なる別のオーバーヘッド レイヤーであると誰かが主張するかもしれませんが、単純な理由でこれを行います。何も壊さずに適切な Web 検証の属性を追加できます (DataLayer は他のアプリと共有可能)。

User オブジェクトを公開する DataLayer クラスを指定すると、次のようになります。

public class DalUser {
  public int Id { get; set;}
  public int Age { get; set;}
  public string Name { get; set;}
  public string Surname { get; set;}

  // Business method for reading/writing/deleting

}

私のビューモデルは次のようなものです:

public class VmUser : DalUser 
{
   [Display(Name="ID Code")]
   public override int Id { get; set; }

   [Display(Name="Age")]
   [Required]
   public override int Age { get; set; }

}

これは私に2つの目標をもたらします.前者は他の何かを壊すことを心配せずに属性を使用できることです.後者はユーザーからいくつかのフィールドを隠してフィールドインジェクションを防ぐことができます.単純なサブクラス化ではありません)。

これは私の企業内で非常に便利であることが証明されています (私たちは EntitySpaces を使用する運命にあります)。これは、ES で生成されたクラスを部分的に再利用するために私が見つけた、あまり見苦しくない方法の 1 つです。

于 2011-12-23T09:08:40.720 に答える