Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC - validation - should ViewModel have access to DB

I have a view model which should check that label of a new entity is unique (not in DB yet).

At the moment I've done it in the view model class:

     public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
        if (PowerOrDuty != null)
        {
            if (PowerOrDuty.Identifier == null)
            {
                using (var db = new PowersAndDutiesContext()) 
                {
                    var existingLabels = db.PowersAndDuties.Select(pod => pod.Label);
                    if (existingLabels.Contains(PowerOrDuty.Label))
                    {
                        yield return new ValidationResult("Cannot create a new power or duty because another power or duty with this label already exists");
                    }
                }                    
            }
           ......

Please note that this is a small internal app with small DB and my time is limited, so the code is not perfect.

I feel that DB access from view models might be a bad practice. Should view model have direct DB access? Should it be able to call a repository to get the available labels? Should validation requiring DB access be done in a controller instead?

like image 261
Maxim Eliseev Avatar asked Nov 08 '13 13:11

Maxim Eliseev


2 Answers

I think that accessing DB from VM is not wrong... AFAIK it is not breaking MVC concept (since it is a presentation layer concept). Said that, it could be better if you have the Validate method provided by a Service Layer.

But all the logic related to the content of the ViewModel, it is better kept in the VM than in the Controller. Cleaner controllers is better.

like image 30
Romias Avatar answered Sep 20 '22 11:09

Romias


Should view model have direct DB access?

I think this should be avoided at all cost

Should it be able to call a repository to get the available labels ?

This is not the concern of a ViewModel. This would introduce some complexity in the testing of your ViewModel (which should almost need none) I guess it is a sign of trouble coming.

Should validation requiring DB access be done in a controller instead ?

Maybe, if by "DB" you mean "Repository". But what comes to mind is a separate custom validation class that you will be able to (un)plug, test, and reuse, in another controller for ajax validation, etc

like image 137
jbl Avatar answered Sep 19 '22 11:09

jbl