/// <summary> /// Visits the property. /// </summary> /// <remarks> /// Infer column name and whatever this propery can be null or not /// Also catch common mistake of try to use [Property] on an entity, instead of [BelongsTo] /// Ensure that joined properties have a joined table. /// </remarks> /// <param name="model">The model.</param> public override void VisitProperty(PropertyModel model) { if (model.PropertyAtt.Column == null) { model.PropertyAtt.Column = model.Property.Name; } // Append column prefix model.PropertyAtt.Column = columnPrefix + model.PropertyAtt.Column; Type propertyType = model.Property.PropertyType; if (NHibernateNullablesSupport.IsNHibernateNullableType(propertyType) && (model.PropertyAtt.ColumnType == null || model.PropertyAtt.ColumnType.Length == 0)) { model.PropertyAtt.NotNull = false; model.PropertyAtt.ColumnType = NHibernateNullablesSupport.GetITypeTypeNameForNHibernateNullable(propertyType); } if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(Nullable <>) && String.IsNullOrEmpty(model.PropertyAtt.ColumnType)) { model.PropertyAtt.NotNull = false; model.PropertyAtt.ColumnType = ObtainNullableTypeNameForCLRNullable(propertyType); } if (ActiveRecordModel.GetModel(propertyType) != null) { throw new ActiveRecordException(String.Format( "You can't use [Property] on {0}.{1} because {2} is an active record class, did you mean to use BelongTo?", model.Property.DeclaringType.Name, model.Property.Name, propertyType.FullName)); } JoinedTableModel joinedTable = ObtainJoinedTableIfPresent(model.Property, model.PropertyAtt); if (joinedTable != null) { joinedTable.Properties.Add(model); } }
private static bool IsChildClass(ActiveRecordModel model, ActiveRecordModel child) { // generic check if (child.Type.BaseType.IsGenericType) { var baseType = child.Type.BaseType; while (baseType != null && baseType.IsGenericType) { if (baseType.GetGenericTypeDefinition() == model.Type) { return(true); } baseType = baseType.BaseType; } } // Direct decendant if (child.Type.BaseType == model.Type) { return(true); } // Not related to each other if (!model.Type.IsAssignableFrom(child.Type)) { return(false); } // The model is the ancestor of the child, but is it the direct AR ancestor? Type arAncestor = child.Type.BaseType; while (arAncestor != typeof(object) && ActiveRecordModel.GetModel(arAncestor) == null) { arAncestor = arAncestor.BaseType; } return(arAncestor == model.Type); }
public override void VisitProperty(PropertyModel model) { if (model.PropertyAtt.Column == null) { model.PropertyAtt.Column = model.Property.Name; } if (typeof(INullableType).IsAssignableFrom(model.Property.PropertyType)) { model.PropertyAtt.NotNull = false; if (model.Property.PropertyType == typeof(NullableBoolean)) { model.PropertyAtt.ColumnType = "Nullables.NHibernate.NullableBooleanType, Nullables.NHibernate"; } else if (model.Property.PropertyType == typeof(NullableByte)) { model.PropertyAtt.ColumnType = "Nullables.NHibernate.NullableByteType, Nullables.NHibernate"; } else if (model.Property.PropertyType == typeof(NullableChar)) { model.PropertyAtt.ColumnType = "Nullables.NHibernate.NullableCharType, Nullables.NHibernate"; } else if (model.Property.PropertyType == typeof(NullableDateTime)) { model.PropertyAtt.ColumnType = "Nullables.NHibernate.NullableDateTimeType, Nullables.NHibernate"; } else if (model.Property.PropertyType == typeof(NullableDecimal)) { model.PropertyAtt.ColumnType = "Nullables.NHibernate.NullableDecimalType, Nullables.NHibernate"; } else if (model.Property.PropertyType == typeof(NullableDouble)) { model.PropertyAtt.ColumnType = "Nullables.NHibernate.NullableDoubleType, Nullables.NHibernate"; } else if (model.Property.PropertyType == typeof(NullableGuid)) { model.PropertyAtt.ColumnType = "Nullables.NHibernate.NullableGuidType, Nullables.NHibernate"; } else if (model.Property.PropertyType == typeof(NullableInt16)) { model.PropertyAtt.ColumnType = "Nullables.NHibernate.NullableInt16Type, Nullables.NHibernate"; } else if (model.Property.PropertyType == typeof(NullableInt32)) { model.PropertyAtt.ColumnType = "Nullables.NHibernate.NullableInt32Type, Nullables.NHibernate"; } else if (model.Property.PropertyType == typeof(NullableInt64)) { model.PropertyAtt.ColumnType = "Nullables.NHibernate.NullableInt64Type, Nullables.NHibernate"; } else if (model.Property.PropertyType == typeof(NullableSByte)) { model.PropertyAtt.ColumnType = "Nullables.NHibernate.NullableSByteType, Nullables.NHibernate"; } else if (model.Property.PropertyType == typeof(NullableSingle)) { model.PropertyAtt.ColumnType = "Nullables.NHibernate.NullableSingleType, Nullables.NHibernate"; } } if (ActiveRecordModel.GetModel(model.Property.PropertyType) != null) { throw new ActiveRecordException(String.Format( "You can't use [Property] on {0}.{1} because {2} is an active record class, did you mean to use BelongTo?", model.Property.DeclaringType.Name, model.Property.Name, model.Property.PropertyType.FullName)); } }