Friday, February 10, 2012
Support » Support Forum
Register Login
HomeWhat is NDO?- Executive Overview- NDO for DevelopersHow NDO Works- Persistent Classes- Mapping- Queries- Reverse Engineering- Inheritance, Polymorphism- Distributed ApplicationsLicensingSupport- Support Forum- FAQ- Solutions- Downloads- E-Mail Support- Tutorial VideosOnline ShopContact
Welcome to the NDO support forum!
Please register/login at the site if you want to post a thread. We sometimes copy support mails of common interest to the forum. They appear with anonymous sender. Please post your messages in English language if possible. You can do your posts also in German, we will translate your post and our answer so that all users of the forum can read your posts. The forum software requires that javascript is enabled. Please do not insert licence information or licence keys in forum posts. If you know a good answer to a post of another user, feel free to reply! The forum is monitored. We reserve the right to remove posts we consider not to be useful for other forum users.
Hello,
1.) Is there any way to perform Custom Validation on a persistent object prior to that object being saved to the back-end database?
2.) For a persistent field does the user have the option of further identifying that field as being required or not? In other words, I have two fields that are being persisted. The first field is required and must be present and have a valid value in the object prior to saving. The other field is NOT required and should not cause any failures should it not be set in the object. I don't want any of this checked by the back-end database, but would prefer that it be checked as part of some standard validation process on the object. I have seen several other products work in such a way that attributes are provided on persistent fields indicating whether they are required or not and a standard validation process is executed on that object prior to saving (all internal) looking at the attributes. If the attribute indicates "required" and that particular field has not been set the appropriate object state is set to invalid and the object is not saved; thus, allowing the user to catch such a problem and report it to the end user to solve.
Hi Sean,
you can implement the IPersistenceNotifiable interface to perform a check for consistency with you business rules in the OnSaving method, which is called by NDO just before storing an object in the database.
To check any conditions like null values or string sizes, I'd suggest writing a class which can performs this checks on base of the mapping information. That kind of checking class would use reflection to get the information from the objects.
To get the definitions from the mapping file, like the ColumnLength property, you can use the NDO Mapping API. You get the top object of the mapping object tree with persistenceManager.NDOMapping.
You can tell NDO in the mapping entry of a certain field whether the column representing the field may be null. Use the AllowDbNull property for this purpose. NDO uses this information while building the database structure.
Additionally you can add any information to the mapping entries via free assignable properties. In the NDO Visual Mapping Editor right click at an entry and choose "Add Property". These properties appear in the NDO Mapping object tree.
You might start with a code like that:
public class ObjectChecker{ public ObjectChecker(NDO.Mapping.Class classMapping) { this.classMapping = classMapping; } Class classMapping; string errorInfo; public bool CheckAnObject(object o) { this.errorInfo = string.Empty; Type t = o.GetType(); foreach(Field field in this.classMapping.Fields) { FieldInfo fi = t.GetField(field.Name, BindingFlags.NonPublic | BindingFlags.Instance); object fieldValue = fi.GetValue(o); if (!field.AllowDbNull) { if (fieldValue == null) { this.errorInfo = "Field " + fi.Name + " should have a value."; return false; } } if (fi.FieldType == typeof(string) && field.ColumnLength != 0) { if (((string)fieldValue).Length > field.ColumnLength) { this.errorInfo = "Information in field " + fi.Name + " is too large. There are only " + fieldColumnLength + " characters allowed."; return false; } } } return true; } public string ErrorInfo { return this.errorInfo; }}Best Regards,Mirko