I’m currently working on adding some new features to the ServiceStack.OrmLite.SqlServer project. Unfortunately, I have run into an architectural hurdle and would like to propose a change to the GetColumnDefition method in OrmLiteDialectProviderBase.
The current definition for this method is:
public virtual string GetColumnDefinition(string fieldName, Type fieldType,
bool isPrimaryKey, bool autoIncrement, bool isNullable, bool isRowVersion,
int? fieldLength, int? scale, string defaultValue, string customFieldDefinition)
and just about every use of this method looks like this:
var columnDefinition = GetColumnDefinition(
fieldDef.FieldName,
fieldDef.ColumnType,
fieldDef.IsPrimaryKey,
fieldDef.AutoIncrement,
fieldDef.IsNullable,
fieldDef.IsRowVersion,
fieldDef.FieldLength,
fieldDef.Scale,
GetDefaultValue(fieldDef),
fieldDef.CustomFieldDefinition);
As you can see, every method parameter is derived from the field definition. My proposal is to change this to the following:
var columnDefinition = GetColumnDefinition(FieldDefinition fieldDef);
This change will allow the downstream dialect providers to have more information around the field definition, so that when it does have to be overwritten, there is access to properties like PropertyInfo, IsClustered, IsIndexed, IsUnique, etc.
I am posting this as a forum topic because I would like to know if there was a reason why this approach wasn’t initially taken? Plus, would there be any adverse effects from this change? I’m only familiar with MySql and SqlServer, so I’m not sure what advantages or disadvantages, if any, this would have on other providers.
I believe the below change could be immediately made without having to update everything plus backwards compatibility can be maintained:
public virtual string GetColumnDefinition(FieldDefinition fieldDef)
{
var fieldName = fieldDef.FieldName;
var isPrimaryKey = fieldDef.IsPrimaryKey;
var fieldType = fieldDef.FieldType;
var autoIncrement = fieldDef.AutoIncrement;
var isNullable = fieldDef.IsNullable;
var isRowVersion = fieldDef.IsRowVersion;
var fieldLength = fieldDef.FieldLength;
var scale = fieldDef.Scale;
var defaultValue = GetDefaultValue(fieldDef);
var customFieldDefinition = fieldDef.CustomFieldDefinition;
// Existing code ....
}
public virtual string GetColumnDefinition(string fieldName, Type fieldType,
bool isPrimaryKey, bool autoIncrement, bool isNullable, bool isRowVersion,
int? fieldLength, int? scale, string defaultValue, string customFieldDefinition)
{
var fieldDef = new FieldDefinition()
{
Name = fieldName,
FieldType = fieldType,
IsPrimaryKey = isPrimaryKey,
AutoIncrement = autoIncrement,
IsNullable = isNullable,
IsRowVersion = isRowVersion,
FieldLength = fieldLength,
Scale = scale,
DefaultValue = defaultValue,
CustomFieldDefinition = customFieldDefinition
};
return GetColumnDefinition(fieldDef);
}