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 void ImplementDbEntityNameProperty(IClassBuilder entityClass, Type entityType) { var entityName = NamingHelper.GetEntityName(entityType); var getter = entityClass.HasProperty <string>(nameof(IEntity.DbEntityName)).WithModifier("public").HasGetter(); getter.Write("return ").String(entityName).EndStatement(); }
private void ImplementPkTypeProperty(IClassBuilder entityClass, PropertyInfo pkProperty) { entityClass.HasProperty <Type>(nameof(IEntity.PrimaryKeyType)) .WithModifier("public") .HasGetter() .Write("return typeof(") .Write(pkProperty.PropertyType) .Write(")") .EndStatement(); }
private void ImplementIsDirtyProperty(IClassBuilder entityClass) { var isDirtyField = entityClass.HasField <bool>("isDirty"); var prop = entityClass.HasProperty <bool>("IsDirty").WithModifier("public"); prop.HasGetter().Write("return ").Write(isDirtyField).EndStatement(); var setter = prop.HasSetter(); setter.Assign(isDirtyField, asi => asi.Write(setter.ValueParameter)); }
protected override bool ImplementProperty(IClassBuilder impl, PropertyInfo propertyInfo, INamedReference serviceLocatorField) { impl.HasProperty(propertyInfo.Name, propertyInfo.PropertyType) .WithModifier("public") .HasGetter() .Write("return \"") .Write(propertyInfo.Name) .Write("\"") .EndStatement(); return(true); }
private void DefaultImplementProperty(IClassBuilder cls, PropertyInfo property) { var storageField = cls.HasField($"storage{property.Name}_{Guid.NewGuid():N}", property.PropertyType); cls.WithConstructor() .Body.Assign( storageField, assignment => assignment.Write("default(").Write(property.PropertyType).Write(")")); var prop = cls.HasProperty(property.Name, property.PropertyType).WithModifier("public"); prop.HasGetter().Write("return ").Write(storageField).EndStatement(); prop.HasSetter().Assign(storageField, a => a.Write("value")); }
private void ImplementDefaultProperty(IClassBuilder entityClass, PropertyInfo sourceProperty, bool setDirtyFlag = false) { var backingField = entityClass.HasField(sourceProperty.PropertyType); var property = entityClass.HasProperty(sourceProperty.Name, sourceProperty.PropertyType).WithModifier("public"); property.HasGetter().Write("return ").Write(backingField).EndStatement(); var setter = property.HasSetter(); if (setDirtyFlag) { setter.If( c => c.Write(backingField).Write(" == ").Write(setter.ValueParameter), then => then.Write("return").EndStatement()); setter.Write("IsDirty = true").EndStatement(); } setter.Assign(backingField, a => a.Write(setter.ValueParameter)).EndStatement(); }
private void ImplementReferenceEntityProperties(IClassBuilder entityClass, Type entityType, IEntityNamingConvention convention) { var getReferencePropertiesMethod = entityClass.HasMethod(nameof(IEntity.GetReferenceProperties)) .Returns <IEnumerable <Tuple <string, Type> > >() .WithModifier("public"); var openPropertyMethod = entityClass.HasMethod(nameof(IEntity.OpenProperty)).Returns <IEntitySet>().WithModifier("public"); var openPropertyNameParam = openPropertyMethod.WithParam <string>("propertyName"); foreach (var property in ReflectionUtil.GetAllProperties(entityType)) { var foreignEntityType = convention.TryGetRefEntityType(property); if (foreignEntityType == null) { continue; } var refNameTypeField = entityClass.HasField <Tuple <string, Type> >().WithModifier("private").WithModifier("readonly").WithAssignment(a => a.InvokeConstructor( typeof(Tuple <string, Type>), inv => inv.WithParam(p => p.String(property.Name)) .WithParam(p => p.Typeof(foreignEntityType)))); getReferencePropertiesMethod.Body.Write("yield return ").Write(refNameTypeField).EndStatement(); INamedReference backingField; if (typeof(IEnumerable).IsAssignableFrom(property.PropertyType)) { backingField = entityClass.HasField <EntityList>() .WithAssignment(a => a.InvokeConstructor(typeof(EntityList), x => { })); entityClass.HasProperty(property.Name, property.PropertyType) .WithModifier("public") .HasGetter() .Write("return ") .Write(typeof(System.Linq.Enumerable)) .Write(".") .Write("OfType<") .Write(foreignEntityType) .Write(">(") .Write(backingField) .Write(")") .EndStatement(); } else { backingField = entityClass.HasField <EntityHolder>() .WithAssignment(a => a.InvokeConstructor(typeof(EntityHolder), x => { })); entityClass.HasProperty(property.Name, property.PropertyType) .WithModifier("public") .HasGetter() .Write("return ") .Write(backingField) .Write(".Value") .Write(" as ") .Write(foreignEntityType) .EndStatement(); } openPropertyMethod.Body.If( c => c.Compare(openPropertyNameParam, b => b.String(property.Name)), then => then.Write("return ").Write(backingField).EndStatement()).NewLine(); } openPropertyMethod.Body.Write("throw ") .InvokeConstructor( typeof(InvalidOperationException), i => i.WithParam(p => p.String("Unknown property requested"))) .EndStatement(); getReferencePropertiesMethod.Body.Write("yield break").EndStatement(); }