1

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 :)

Skualys
  • 11
  • 2
  • You got to check this question which is running now on similar topic - http://stackoverflow.com/questions/31110329/mvc-model-validation-from-database – ramiramilu Jul 05 '15 at 15:04
  • Your approch is interesting (generic Validation Attribute and use of a repository for DB access). But i still have a doubt : in your exemple, you pass the value to check to the validator. In my case, I should send two parameters (the value, and creation/update status), so I still have to check my container type (CreateDealModelView, EditDealModelView or IntegrationFile). Is there anything wrong doing it ? Or should I send the whole container to the ValidatorCommand ? – Skualys Jul 05 '15 at 16:51
  • In my approach, I am only sending Command Type. You should also do the same. Say create commands - CreateCommand, EditCommand etc., and then pass it to the validation attribute. – ramiramilu Jul 05 '15 at 17:28
  • Ok, but how to know if I should call EditCommand or CreateCommand for the IntegrationFile ? (which could be with a creation or update status). – Skualys Jul 05 '15 at 17:32
  • For example - `[Validate(IIntegrationLineCommand)] public int alesmanID { get; set; }` – ramiramilu Jul 05 '15 at 17:35
  • I still have two problem with that : - What to I do in my IntegrationLineCommand ? Because I need two parameters (status & value), and I your example you just send the value - It's not really DRY in my case because some code will be duplicated between Edit/Create deal commands, and IntegrationFile command. – Skualys Jul 05 '15 at 17:49

0 Answers0