Quantcast
Channel: Fluent Validation for .NET
Viewing all articles
Browse latest Browse all 1917

New Post: Checking for Immutable property changes

$
0
0
Hi Jeremy and all,

I am stuck on a particular problem. I am using Fluent Validation to validate my entities before they hit the database and passing the ObjectStateEntry in to check the current state etc and have rule logic.

I have some properties that are immutable and even though i'll try and prevent my View models etc from being able to modify these properties, I need a domain level check to be sure. All I need is to be able to get the property name as a string within a Must validation. I tried "{Property Name}" like I have done in some Custom Error Messages but it doesn't seem to resolve with this.

Is there an override I could use to implement this?

Thanks, Pete
public class SomeClassValidator : AbstractValidator<SomeClass>
{
      public SomeClassValidator(ObjectStateEntry entry)
      {
             switch(entry.State)
             {
                   case EntityState.Added: // Some Checks etc
                           break;
                   case EntityState.Modified:
                             var currentValues = entry.CurrentValues;
                             var originalValues = entry.OriginalValues;

                           RuleFor(p => p.ImmutablePropertyX).Must(x => !HasBeenIllegallyModified("{Property Name}", currentValues, originalValues);
                           RuleFor(p => p.ImmutablePropertyY).Must(y => !HasBeenIllegallyModified({Property Name}, currentValues, originalValues);
                           break;
             }
      }
}

Viewing all articles
Browse latest Browse all 1917

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>