What I'm after, personally, is validation by convention - I don't want two copies of the same value, or worse, two different representations of the same value - it's an invitation to create buggy mappings.
For example, you can create a property with Nullable=True and Column.NotNull=True - which doesn't make any sense... ValidateRequired is also redundant in most cases, so you're maintaining this setting in triplicate. (at least, Nullable could drive the default for the other two settings.)
You can also create a property with Column.Length=50 and ValidateMaxLength=100 - which does not make sense... again, Column.Length could easily drive the default.
I managed to implement this myself:
Code: Select all
//////////////////////////////
//
// Apply mapping conventions
//
//////////////////////////////
private void ConfigureMappings(HibernateClass cls) {
foreach (HibernateProperty property in cls.Properties) {
var type = codeProvider.GetNullableType(property.Nullable, property.Type);
if (type == "string") {
if (property.Column.Length > 0) {
property.ValidateMaxLength = property.Column.Length;
} else {
property.ValidateMaxLength = null;
}
property.ValidateRequired = !property.Nullable;
property.Column.NotNull = !property.Nullable;
}
}
}
It ties into the class generation loop like so:
Code: Select all
foreach (HibernateClass cls in _namespace) {
ConfigureMappings(cls); // add the call here...
if (FilePerClass) {
Anyone wants to use this,
be warned: this script is
destructive - apparently, the changes made to these objects will persist in the IDE! In other words, you can no longer manually control ValidateMaxLength, ValidateRequired or Column.NotNull.
You've been warned!
Note to the programmers: please consider passing
clones of the model objects to the template, so that templates can't destroy the model project.