This project is read-only.

Automatic Setter Property Validation, All or Nothing?

Oct 29, 2009 at 7:43 PM

I would like to be able to disable automatic property validation (postsharp injection) for specific properties/methods.

LINQ->SQL entity property setters are unfortunately generated in such a way as they set themselves to NULL as an interim step of the update process (they NULL out the original entity value before updating to the new value).

The result of this is that the property is essentially updated twice.  First it sets itself to NULL indirectly via a LINQ EntitySet Remove call, then it updates to the value.

Now we can easily argue that this behavior is stupid, but it's auto-generated code from SQLMetal that I can't really touch.

The problem is, if I decorate these properties via AddValidation calls of NotNull, and allow PostSharp validation... the property setter NotNULL validation *always* fires because of that interim step.  So I can set an entity to a non-null value, and when that entity NULLs itself out as the first step of updating itself, the validation fires and an exception is thrown.

I'd like to be able to call an AddValidation overload where I specify with a boolean whether or not I want property setter validation to run. 

I don't want to globally disable property setter validation for all properties... in most cases, I like the setters throwing exceptions on bad data.

Is there another way I've missed to do this?

Nov 2, 2009 at 9:59 AM

The simplest solution to this would be to write your own NotNull validator that you could switch on or off via a static flag. The implementation for the NotNull validator is pretty simple so you shouldn't have any difficulty in copying it. Let me know if you have any issues.


Nov 6, 2009 at 11:01 PM

I'm not sure I follow on the static flag...

If I write my own validator, I'd like to do is switch on or off validation based on *where* the validation is called.   It seems like there are two disctinct validation "modes", via PostSharp, and called manually.

If the validation is being called in "intercept" (I think that is the correct term) mode, it would be nice if the IValidationContext provided that information, so that any validator I write could shortcut itself, like:

<font size="2" color="#0000ff"><font size="2" color="#0000ff">


</font></font><font size="2" color="#0000ff">



new Validator<T>((v, c) => { if (!(c.IsIntercept) && null == v) throw new ValidateNotNullException(); });

This way I could have a validation type that won't fire itself if in a setter, while others still will.

Nov 10, 2009 at 1:46 PM

ok, I understand now. This looks doable. I'll try to take a look sometime soon but I'm very busy on other projects right now. If you implement it yourself please send me a patch and I'll add it to the source. Thanks.