概念の問題に直面しています。2 つの方法で取引を作成および更新できます。Web フォームを使用する方法 (1 つは取引を作成するため、もう 1 つは編集するため) と統合ファイルを使用する方法 (大量の作成と更新を可能にするため) です。
public class CreateDealViewModel
{
public int dealID { get; set; }
[ValidateSalesman]
public int SalesmanID { get; set; }
}
public class EditDealViewModel
{
public int dealID { get; set; }
[ValidateSalesman]
public int SalesmanID { get; set; }
}
public class IntegrationLine
{
public int DealID { get; set; }
[ValidateSalesman]
public int SalesmanID { get; set; }
public string Status { get; set; }
}
実装する検証ロジックがあります。取引の作成時に、アクティブなセールスマンのみが受け入れられます。更新では、アクティブなセールスマンと以前のセールスマンの値 (DB に格納されている) が受け入れられます。
私はこのようなものを書きました:
public class ValidateSalesman : ValidationAttribute
{
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
var container = validationContext.ObjectInstance;
if (container.GetType() == typeof(IntegrationLine))
{
if(((IntegrationLine)container).Status == "CREATION")
{
//Validation logic here
}
else
{
//Validation logic here
}
}
else if(container.GetType() == typeof(CreateDealViewModel))
{
//Validation logic here
}
else if(container.GetType() == typeof(EditDealViewModel))
{
//Validation logic here
}
}
}
}
それは良いアプローチですか(MVC準拠)ですか?検証属性は、それが適用されるモデルの種類を知る必要がありますか?
事前にアドバイスをありがとう:)