private PropertyInfo ImplementReadPrimaryKeyAndGetPkProperty( IClassBuilder entityClass, EntityBehavior behavior, Type entityType) { var pkProperty = ReflectionUtil.GetProperty(entityType, behavior.PrimaryKeyProperty); if (pkProperty == null) { throw new InvalidOperationException($"{entityType.Name} is missing {behavior.PrimaryKeyProperty} property"); } var idField = entityClass.HasField(pkProperty.PropertyType); var idProp = entityClass.HasProperty(pkProperty.Name, pkProperty.PropertyType).WithModifier("public"); idProp.HasGetter().Write("return ").Write(idField).EndStatement(); idProp.HasSetter().Assign(idField, a => a.Write("value")).EndStatement(); var primaryKeyValueProperty = entityClass.HasProperty(nameof(IEntity.PrimaryKeyValue), typeof(object)) .WithModifier("public"); primaryKeyValueProperty.HasGetter().Write("return ").Write(idProp).EndStatement(); var primaryKeyValueSetter = primaryKeyValueProperty.HasSetter(); primaryKeyValueSetter.Write(idProp).Write(" = ").Write("(").Write(pkProperty.PropertyType).Write(")").Write(primaryKeyValueSetter.ValueParameter).EndStatement(); var readPkMethod = entityClass.HasMethod(nameof(IEntity.ReadPrimaryKey)).WithModifier("public").Returns <bool>(); var readerParam = readPkMethod.WithParam <IDataRecord>("reader"); readPkMethod.Body.If( c => c.Write(readerParam) .Write(".") .Invoke( nameof(IDataRecord.IsNull), p => p.WithParam(code => code.Write("\"").Write(pkProperty.Name).Write("\""))), then => then.Write("return false").EndStatement()).NewLine(); readPkMethod.Body.Assign( idField, a => a.Write(readerParam) .Write(".Get<") .Write(pkProperty.PropertyType) .Write(">(\"") .Write(pkProperty.Name) .Write("\")")).EndStatement(); readPkMethod.Body.Write("return true").EndStatement(); return(pkProperty); }
private IEnumerable <PropertyInfo> ImplementProperties(IClassBuilder entityClass, Type setupInterfaceType, EntityBehavior behavior) { foreach (var sourceProperty in ReflectionUtil.GetAllProperties(setupInterfaceType)) { if (sourceProperty.Name == behavior.PrimaryKeyProperty) { continue; } if (!sourceProperty.CanRead) { continue; } try { if (string.IsNullOrWhiteSpace(m_setup.EntityNamingConvention.GetColumnName(sourceProperty))) { if (m_setup.EntityNamingConvention.TryGetRefEntityType(sourceProperty) == null) { ImplementDefaultProperty(entityClass, sourceProperty); } continue; } else { if (!sourceProperty.CanWrite) { throw new InvalidOperationException( $"Property {sourceProperty.DeclaringType?.Name}.{sourceProperty.Name} seems to be a column, but has no setter"); } if ((sourceProperty.Name.Length > 2) && sourceProperty.Name.EndsWith("Id") && !Attribute.IsDefined(sourceProperty, typeof(NotFkAttribute))) { var typedPropName = sourceProperty.Name.Substring(0, sourceProperty.Name.Length - 2); var typedProp = ReflectionUtil.GetProperty(setupInterfaceType, typedPropName); if ((typedProp == null) || (m_setup.EntityNamingConvention.TryGetRefEntityType(typedProp) == null)) { throw new InvalidOperationException($"Property {sourceProperty.DeclaringType}.{sourceProperty.Name} seems to be a foreign key, but there is not any property {typedPropName} of Entity type to describe the reference. Add {typedPropName} or mark the property with {typeof(NotFkAttribute)}"); } } ImplementColumnProperty(entityClass, sourceProperty); } } catch (Exception ex) { throw new InvalidOperationException($"Failed to map property {sourceProperty.DeclaringType}.{sourceProperty.Name}", ex); } yield return(sourceProperty); } }