I'm facing a conception problem. We could create and update our deals in two ways : using web forms (one to create deals, another one to edit them) and through an integration file (to allow mass creation & update).
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; }
}
I have a validation logic to implement : at deal creation, only active salesman are accepted ; in update, active salesman plus the previous salesman value (stored in DB) are accepted.
I wrote something like this :
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
}
}
}
}
Is it a good approach (MVC compliant) or not ? Does the validation attribute has to know witch kind of model it applies on ?
Thanks in advance for any advice :)