Example #1
0
        public static IEnumerable <EntityProperty> TransformUsisToUniqueIds(this IEnumerable <EntityProperty> properties)
        {
            var suppliedProperties = properties.ToList();

            // Return an empty enumerable if there aren't any properties in the source
            if (!suppliedProperties.Any())
            {
                yield break;
            }

            var containingEntity = suppliedProperties
                                   .Select(x => x.Entity)
                                   .FirstOrDefault(x => x != null);

            if (containingEntity == null)
            {
                throw new InvalidOperationException("None of the properties supplied have an associated Entity.");
            }

            foreach (var entityProperty in suppliedProperties)
            {
                // If column is an USI column...
                if (UniqueIdSpecification.IsUSI(entityProperty.PropertyName))
                {
                    string uniqueIdPropertyName = entityProperty.PropertyName.ConvertToUniqueId();

                    // Find a corresponding UniqueId column, if it exists
                    var correspondingUniqueIdProperty = containingEntity
                                                        .Properties
                                                        .SingleOrDefault(p => p.PropertyName.EqualsIgnoreCase(uniqueIdPropertyName));

                    if (correspondingUniqueIdProperty != null)
                    {
                        // Swap the UniqueId column in for the USI
                        yield return(correspondingUniqueIdProperty);

                        continue;
                    }

                    // Replace the USI column with a newly created UniqueId column
                    yield return
                        (new EntityProperty(uniqueIdPropertyName,
                                            new PropertyType(DbType.AnsiString, 32, entityProperty.PropertyType.IsNullable),
                                            string.Format("A unique alpha-numeric code assigned to a {0}.", entityProperty.Entity.Name)));
                }
                else if (UniqueIdSpecification.IsUniqueId(entityProperty.PropertyName))
                {
                    string usiPropertyName = UniqueIdSpecification.GetUsiPropertyName(entityProperty.PropertyName);

                    // Find a corresponding USI column, if it exists
                    var correspondingUsiProperty = containingEntity
                                                   .Properties
                                                   .SingleOrDefault(p => p.PropertyName.EqualsIgnoreCase(usiPropertyName));

                    // If a corresponding USI property exists, then skip the UniqueId (it's been returned for use wherever the USI had been used)
                    if (correspondingUsiProperty != null)
                    {
                        continue;
                    }

                    yield return(entityProperty);
                }
                else
                {
                    yield return(entityProperty);
                }
            }
        }