Beispiel #1
0
 /// <summary>
 /// Copies the specified mapping.
 /// </summary>
 /// <param name="mapping">The mapping.</param>
 public void Copy(IMapping mapping)
 {
     if (mapping is null)
     {
         return;
     }
     foreach (var prop in mapping.IDProperties.Where(x => !IDProperties.Any(y => y.Name == x.Name)))
     {
         CopyProperty(prop);
     }
     foreach (var prop in mapping.ReferenceProperties.Where(x => !ReferenceProperties.Any(y => y.Name == x.Name)))
     {
         CopyProperty(prop);
     }
     foreach (var prop in mapping.MapProperties.Where(x => !MapProperties.Any(y => y.Name == x.Name)))
     {
         CopyProperty(prop);
     }
     foreach (var prop in mapping.ManyToManyProperties.Where(x => !ManyToManyProperties.Any(y => y.Name == x.Name)))
     {
         CopyProperty(prop);
     }
     foreach (var prop in mapping.ManyToOneProperties.Where(x => !ManyToOneProperties.Any(y => y.Name == x.Name)))
     {
         CopyProperty(prop);
     }
 }
Beispiel #2
0
        public virtual TModel BuildFrom(IDbReader reader)
        {
            if (!reader.Read())
            {
                return(null);
            }

            var model = new TModel();

            Properties.ForEach(property =>
            {
                if (!reader.IsDbNull(property.Name))
                {
                    property.SetValue(model, reader[property.Name], null);
                }
            });

            ManyToOneProperties.ForEach(property =>
            {
                var propertyName = property.Name + "Id";

                if (!reader.IsDbNull(propertyName))
                {
                    var manyToOneObject = Activator.CreateInstance(property.PropertyType);
                    var idProperty      = property.PropertyType.GetRuntimeProperty("Id");
                    idProperty.SetValue(manyToOneObject, reader[propertyName], null);

                    property.SetValue(model, manyToOneObject, null);
                }
            });

            return(model);
        }
Beispiel #3
0
 /// <summary>
 /// Copies the property.
 /// </summary>
 /// <param name="prop">The property.</param>
 public void CopyProperty(IManyToOneProperty prop)
 {
     if (prop is null)
     {
         return;
     }
     ManyToOneProperties.Add(prop.Convert <TClassType>(this));
 }
Beispiel #4
0
 /// <summary>
 /// Determines whether the mapping contains a property.
 /// </summary>
 /// <param name="propertyName">Name of the property.</param>
 /// <returns><c>true</c> if the mapping contains the specified property; otherwise, <c>false</c>.</returns>
 public bool ContainsProperty(string propertyName)
 {
     return(IDProperties.Any(x => x.Name == propertyName) ||
            ReferenceProperties.Any(x => x.Name == propertyName) ||
            MapProperties.Any(x => x.Name == propertyName) ||
            ManyToManyProperties.Any(x => x.Name == propertyName) ||
            ManyToOneProperties.Any(x => x.Name == propertyName));
 }
Beispiel #5
0
        /// <summary>
        /// Sets a property as a many to one type.
        /// </summary>
        /// <typeparam name="TDataType">The type of the data type.</typeparam>
        /// <param name="expression">Expression pointing to the property</param>
        /// <returns>The many to many object</returns>
        public ManyToOneSingle <TClassType, TDataType> ManyToOne <TDataType>(System.Linq.Expressions.Expression <Func <TClassType, TDataType> > expression)
            where TDataType : class
        {
            if (expression is null)
            {
                throw new ArgumentNullException(nameof(expression));
            }

            var ReturnValue = new ManyToOneSingle <TClassType, TDataType>(expression, this);

            ManyToOneProperties.Add(ReturnValue);
            return(ReturnValue);
        }
Beispiel #6
0
        private void AddManyToOneRecords(TModel model, IDictionary <string, object> dictionary)
        {
            ManyToOneProperties.ForEach(propertyInfo =>
            {
                var dbColumnName    = propertyInfo.Name + "Id";
                var manyToOneObject = propertyInfo.GetValue(model);
                if (manyToOneObject == null)
                {
                    if (!dictionary.ContainsKey(dbColumnName))
                    {
                        dictionary.Add(dbColumnName, null);
                    }
                    return;
                }

                var manyToOneObjectType = manyToOneObject.GetType();
                var idPropertyInfo      = manyToOneObjectType.GetRuntimeProperty("Id");
                var idValue             = idPropertyInfo.GetValue(manyToOneObject, null);

                if (idPropertyInfo.PropertyType == typeof(Guid))
                {
                    if ((Guid)idValue == Guid.Empty)
                    {
                        idValue = null;
                    }
                }
                else if (idPropertyInfo.PropertyType == typeof(int))
                {
                    if ((int)idValue == 0)
                    {
                        idValue = null;
                    }
                }
                else if (idPropertyInfo.PropertyType == typeof(long))
                {
                    if ((long)idValue == 0)
                    {
                        idValue = null;
                    }
                }
                if (!dictionary.ContainsKey(dbColumnName))
                {
                    dictionary.Add(dbColumnName, idValue);
                }
            });
        }
Beispiel #7
0
        public virtual TModel BuildFrom(IDataReader reader)
        {
            if (!reader.Read())
            {
                return(null);
            }

            var model = new TModel();

            Properties.ForEach(property =>
            {
                var ordinal = reader.GetOrdinal(property.Name);
                if (reader.IsDBNull(ordinal))
                {
                    return;
                }

                SetPropertyValue(ref property, model, reader, ordinal);
            });

            ManyToOneProperties.ForEach(property =>
            {
                var propertyName = property.Name + "Id";
                var ordinal      = reader.GetOrdinal(propertyName);

                if (!reader.IsDBNull(ordinal))
                {
                    var manyToOneObject = Activator.CreateInstance(property.PropertyType);
                    var idProperty      = property.PropertyType.GetRuntimeProperty("Id");
                    SetPropertyValue(ref idProperty, manyToOneObject, reader, ordinal);

                    property.SetValue(model, manyToOneObject, null);
                }
            });

            return(model);
        }
Beispiel #8
0
        /// <summary>
        /// Reduces this instance based on parent mapping properties.
        /// </summary>
        /// <param name="parentMapping">The parent mapping.</param>
        /// <param name="logger">The logger.</param>
        public void Reduce(IMapping parentMapping, ILogger logger)
        {
            if (parentMapping is null)
            {
                return;
            }
            var IsDebug = logger?.IsEnabled(Serilog.Events.LogEventLevel.Debug) ?? false;

            for (var x = 0; x < parentMapping.ReferenceProperties.Count; ++x)
            {
                var ReferenceProperty1 = parentMapping.ReferenceProperties[x];
                for (var y = 0; y < ReferenceProperties.Count; ++y)
                {
                    var ReferenceProperty2 = ReferenceProperties[y];
                    if (ReferenceProperty1.Similar(ReferenceProperty2))
                    {
                        if (IsDebug)
                        {
                            logger?.Debug("Found duplicate reference and removing {Name:l} from mapping {Mapping:l}", ReferenceProperty2.Name, ObjectType.Name);
                        }
                        ReferenceProperties.Remove(ReferenceProperty2);
                        --y;
                    }
                }
            }
            for (var x = 0; x < parentMapping.MapProperties.Count; ++x)
            {
                var ReferenceProperty1 = parentMapping.MapProperties[x];
                for (var y = x + 1; y < MapProperties.Count; ++y)
                {
                    var ReferenceProperty2 = MapProperties[y];
                    if (ReferenceProperty1.Similar(ReferenceProperty2))
                    {
                        if (IsDebug)
                        {
                            logger?.Debug("Found duplicate map and removing {Name:l} from mapping {Mapping:l}", ReferenceProperty2.Name, ObjectType.Name);
                        }
                        MapProperties.Remove(ReferenceProperty2);
                        --y;
                    }
                }
            }
            for (var x = 0; x < parentMapping.ManyToManyProperties.Count; ++x)
            {
                var ReferenceProperty1 = parentMapping.ManyToManyProperties[x];
                for (var y = x + 1; y < ManyToManyProperties.Count; ++y)
                {
                    var ReferenceProperty2 = ManyToManyProperties[y];
                    if (ReferenceProperty1.Similar(ReferenceProperty2))
                    {
                        if (IsDebug)
                        {
                            logger?.Debug("Found duplicate many to many and removing {Name:l} from mapping {Mapping:l}", ReferenceProperty2.Name, ObjectType.Name);
                        }
                        ManyToManyProperties.Remove(ReferenceProperty2);
                        --y;
                    }
                }
            }

            for (var x = 0; x < parentMapping.ManyToOneProperties.Count; ++x)
            {
                var ReferenceProperty1 = parentMapping.ManyToOneProperties[x];
                for (var y = x + 1; y < ManyToOneProperties.Count; ++y)
                {
                    var ReferenceProperty2 = ManyToOneProperties[y];
                    if (ReferenceProperty1.Similar(ReferenceProperty2))
                    {
                        if (IsDebug)
                        {
                            logger?.Debug("Found duplicate many to one and removing {Name:l} from mapping {Mapping:l}", ReferenceProperty2.Name, ObjectType.Name);
                        }
                        ManyToOneProperties.Remove(ReferenceProperty2);
                        --y;
                    }
                }
            }
        }