Some questions about constraints:
Maybe pull the constraints classes into the namespace Xtensive.Integrity.Constraints; (remove the Aspects.. its not clear that you have to add this namespace for constraints (or add them to the Xtensive.Storage namespace, because you need them always when defining your persistent classes) Marco (edit: added another remark) Updated at 04.12.2009 22:35:56This is the declaration of the persistent property:
I expected an AggregateException, but the code ..FirstName = too loonggg stringg..... is raising the exception.. Updated at 05.12.2009 9:42:12
This is the property.
I expected an LengthConstraint is automatically added... maybe add an overload to [Field(Length=50, Constraint=true) and the will add a constraint like [LengthConstraint(Max=50)] Updated at 06.12.2009 21:10:05
the SetValue exception is raised after assigning the value. So when you use a disabled validation region (to collect all the validation errors instead of only the first error) you have a problem, because if an user enters a too long string value, the SetValue exception is raised instead of an normal validation error... So how would you solve this? Updated at 07.12.2009 16:13:28No, that will not help me and all the other (good and bad) programmers :lol: You should get an exception when saving too long texts, maybe you should move the XxxFieldAccessor<t>.SetValue constraints (or at least the Lenght constrain, don't know the others) to transaction.Complete() method.. That way you have an option to call <entity>.CheckConstaints() before completing the transaction Thanks Updated at 07.12.2009 22:24:21Truncating can be a solution in some situations, but especially in the case of Name / FirstName / LastName you dont want to automatically truncate the value.. Lets return to my first remark: - less technical exception when using a too long string.. see this stacktrace failed: System.InvalidOperationException : 'Length' constraint violation on field 'FirstName (FieldInfo)'. Maybe just return the same Exception message (and type) as in the LengthConstraint? Normally you set a maxlength in the UI (using the FieldInfo information from the Entity), but when using the upcoming Entity-DTO-Mapper (especially in a service environment) you are not in control of the UI.. Are there more constraints in this layer? like DateTime (sql server limitation)? This thread was imported from our support forum. The original discussion may contain more detailed answer. |
Alex (Xtensive) wrote:About localization: that's the only reason why we're keeping http://code.google.com/p/dataobjectsdot ... tail?id=73 not closed yet. As you may find, few days ago I asked to finish this ASAP. About length constraint: may be you're getting the exception while validation is disabled? Stack trace shows actually it is thrown by our integrated constraints (they're called constraints as well ;) ), but not by [LengthConstraint] attribute. So I suspects validation in aspect is skipped because you're in Validation.Disable() block. That's why exception type here is different. About Xtensive.Integrity.Constraints: in near future there will be no Xtensive.Integrity at all - http://code.google.com/p/dataobjectsdot ... ail?id=488 Alex (Xtensive) wrote:There are two different types of constraints:
That's why 1st-type constraints do not involve 2nd-type: 1st are much stronger, there is no way to violate them temporarily. So I'm not sure what to do... On one hand, you'd like to get the same exception; on another, you disable the 2nd type of constraints, so even if they would be there, you have the chance to get just an exception generated by 1st type constraint. Can you advise something here? If you're looking for error message, may be this might help (Persistent.GetErrorMessage method code):
Alex (Xtensive) wrote:Btw, if you won't specify the length for the string field, everything should work as you want - underlying column type will be nvarchar(max), but validation constraint will allow you to persist strings of specified length only. But I'm not sure if this is acceptable in your case. Alex Kofman wrote:I suggest following solution: 1. Internally cut string field value to maximum possible length before persist 2. Use field constraints in usual way However, this approach brings some disadvantages also:
So making this behavior optional can be partial solution of the problem. Marco, would such option help you or not? Alex (Xtensive) wrote:We can't move these constraints to tx.Complete():
v3.9 has a nice attribute: [Corrector] (or something like this ;) ). If there would be such an attribute (btw, it's pretty easy to implement it on PostSharp), the following code would solve the problem:
What do you think about this? Likely, Alex Kofman was talking about nearly this approach. Alex (Xtensive) wrote:As you see, the idea is to: a) Ensure the underlying value will fit into the storage column anyway b) Add regular validation constraint checking this. Btw, likely, it will be simple to support of behavior like this right by [LengthConstraint(..., Truncate = true)]. Alex (Xtensive) wrote:E.g. it can truncate "Marco Dissel" (12 chars) to "Marco Diss..." (11 chars, = 10+1). It does not matter what value to persist further, since validation constraint is anyway broken, so you won't be able to commit it. But such values looks a bit nicer in UI, if they're fetched later in the same transaction. Alex Kofman wrote:I've just implemented localizable error messages in property constraints. It looks like this now:
Alex Kofman wrote:
I've created appropriate issue: http://code.google.com/p/dataobjectsdot ... ail?id=517 First of all I plan to remove that check from field accessor, so it will not throw exception on property setting. This will help you in case you don't call Session.Persist() (i.e. execute any query) between assignment property value and performing validation. Alex (Xtensive) wrote:> Are there more constraints in this layer? like DateTime (sql server limitation)?
So for now we check just nullability & length (everything else will lead to delayed persist failure). And, as you see, constraints there aren't database specific. |