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(); }
public SchemaMigrator(IDataModelHelper dataModel, IEntityNamingConvention convention) { m_dataModel = dataModel; m_convention = convention; }