7

ViewModel アプローチを使用して ASP.Net MVC アプリケーションを構築し、ドメイン エンティティを UI で使用される「モデル」から分離しています。ViewModel クラスの命名には、次の規則を使用しています。ViewModelName = ViewName + "ViewModel". 例えば:

Index + ViewModel = IndexViewModel

これまでのところ、これはかなり一般的なパターンであり、StackOverflow などでこのトピックに関する多くのガイダンスがあります。私の質問は、ViewModel で使用される子オブジェクトに関するものです。ViewModel がドメイン モデル オブジェクトと同じプロパティを持つクラスを必要とする場合、ViewModel 内にドメイン モデルを含めるだけです。例えば:

public class PersonViewModel
{
    public int PersonID { get; set; } 
    public Address Address { get; set; }
    public string SomeOtherProperty { get; set; }
}

ただし、ドメイン モデルとは異なるプロパティを持つ子オブジェクトが必要な場合に、どの命名規則を使用すればよいかわかりません。たとえば、Address ドメイン モデルにあるもの以外に、Address にいくつかの追加プロパティが必要な場合、何と呼べばよいでしょうか? 私は AddressViewModel を次のように考えました:

public class PersonViewModel
{
    public int PersonID { get; set; } 
    public AddressViewModel Address { get; set; }
    public string SomeOtherProperty { get; set; }
}

しかし、それは私には正しくありません。私の直感では、ViewModel サフィックスはトップ レベルの ViewModel のみに使用する必要があります。

このシナリオで使用する命名規則について、他の開発者からの提案を探しています。具体的には、この場合、子オブジェクトを何と呼びますか?

4

1 に答える 1