標準のドメイン層エンティティがあります。
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set;}
}
ある種の検証属性が適用されています。
public class Product
{
public int Id { get; set; }
[NotEmpty, NotShorterThan10Characters, NotLongerThan100Characters]
public string Name { get; set; }
[NotLessThan0]
public decimal Price { get; set;}
}
ご覧のとおり、これらの属性は完全に構成されています。ここで使用されている検証フレームワーク (NHibernate Validator、DataAnnotations、ValidationApplicationBlock、Castle Validator など) は重要ではありません。
クライアント レイヤーには、ドメイン エンティティ自体を使用せずに、ビュー レイヤーが使用する ViewModel (別名 DTO) にマップする標準的なセットアップもあります。
public class ProductViewModel
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set;}
}
次に、クライアント/ビューでいくつかの基本的なプロパティ レベルの検証を実行できるようにしたいとしましょう。
これを行う唯一の方法は、ViewModel オブジェクトで検証定義を繰り返すことです。
public class ProductViewModel
{
public int Id { get; set; }
// validation attributes copied from Domain entity
[NotEmpty, NotShorterThan10Characters, NotLongerThan100Characters]
public string Name { get; set; }
// validation attributes copied from Domain entity
[NotLessThan0]
public decimal Price { get; set;}
}
ViewModel (DTO) レイヤーでビジネス ロジック (プロパティ レベルの検証) を繰り返したので、これは明らかに満足のいくものではありません。
では、何ができるでしょうか?
AutoMapper のような自動化ツールを使用して Domain エンティティを ViewModel DTO にマップすると仮定すると、マップされたプロパティの検証ロジックを ViewModel に転送するのもクールではないでしょうか?
質問は次のとおりです。
1) これは良い考えですか?
2) もしそうなら、それはできますか? そうでない場合、代替手段は何ですか?
ご意見をお寄せいただきありがとうございます。