/// <summary>
        /// Gets the name of the foreign key.
        /// </summary>
        /// <param name="manyToOneMapping">The many to one mapping.</param>
        /// <param name="componentMapping">The component mapping.</param>
        /// <param name="entityType">Type of the entity.</param>
        /// <returns>A <see cref="System.String"/></returns>
        public static string GetForeignKeyName(this ManyToOneMapping manyToOneMapping, IComponentMapping componentMapping, Type entityType)
        {
            var manyToOnePropertyType = manyToOneMapping.Member.PropertyType;
            var propertyTypeName      = manyToOnePropertyType.Name;
            //if ( typeof( ILookup ).IsAssignableFrom ( manyToOnePropertyType ) )
            //{
            //    propertyTypeName += "Lkp";
            //}
            var namingStrategy = componentMapping.GetNamingStrategy();
            var columnName     = namingStrategy.GetColumnName(
                componentMapping.Member.DeclaringType,
                componentMapping.Member.PropertyType,
                componentMapping.Name,
                manyToOneMapping.Member.DeclaringType,
                manyToOneMapping.Member.PropertyType,
                manyToOneMapping.Name,
                true);

            var referenceName = string.Format("{0}_{1}", entityType.Name, propertyTypeName);

            const string ForeignKeyNameSuffix = "_FK";

            if (columnName != propertyTypeName.Replace("Lkp", string.Empty))
            {
                referenceName = string.Format("{0}_{1}_{2}", entityType.Name, propertyTypeName, columnName);
            }

            var foreignKeyName = string.Format("{0}{1}", referenceName, ForeignKeyNameSuffix);

            return(foreignKeyName);
        }
Beispiel #2
0
        ManyToOneMapping IManyToOneMappingProvider.GetManyToOneMapping()
        {
            var mapping = new ManyToOneMapping(attributes.Clone())
            {
                ContainingEntityType = entity,
                Member = member
            };

            mapping.Set(x => x.Name, Layer.Defaults, member.Name);
            mapping.Set(x => x.Class, Layer.Defaults, new TypeReference(typeof(TOther)));

            if (columns.Count == 0 && !mapping.IsSpecified("Formula"))
            {
                mapping.AddColumn(Layer.Defaults, CreateColumn(member.Name + "_id"));
            }

            foreach (var column in columns)
            {
                var columnMapping = CreateColumn(column);

                mapping.AddColumn(Layer.UserSupplied, columnMapping);
            }

            return(mapping);
        }
        ManyToOneMapping IManyToOneMappingProvider.GetManyToOneMapping()
        {
            var mapping = new ManyToOneMapping(attributes.CloneInner());

            mapping.ContainingEntityType = entity;
            mapping.Member = property;

            if (!mapping.IsSpecified("Name"))
            {
                mapping.Name = property.Name;
            }

            if (!mapping.IsSpecified("Class"))
            {
                mapping.SetDefaultValue(x => x.Class, new TypeReference(typeof(TOther)));
            }

            if (columns.Count == 0)
            {
                mapping.AddDefaultColumn(CreateColumn(property.Name + "_id"));
            }

            foreach (var column in columns)
            {
                var columnMapping = CreateColumn(column);

                mapping.AddColumn(columnMapping);
            }

            return(mapping);
        }
        protected ManyToOneMapping reference_with_column(string column)
        {
            var reference = new ManyToOneMapping();

            reference.AddColumn(Layer.Defaults, new ColumnMapping("propertyColumn"));
            return(reference);
        }
        public override void Visit(ManyToOneMapping manyToOneMapping)
        {
            var writer       = serviceLocator.GetWriter <ManyToOneMapping>();
            var manyToOneXml = writer.Write(manyToOneMapping);

            document.ImportAndAppendChild(manyToOneXml);
        }
Beispiel #6
0
 /// <summary>
 /// Sets the Foreign Key.
 /// </summary>
 /// <param name="manyToOneMapping">The many to one mapping.</param>
 /// <param name="foreignKeyName">Name of the foreign key.</param>
 public static void ForeignKey(this ManyToOneMapping manyToOneMapping, string foreignKeyName)
 {
     if (!manyToOneMapping.IsSpecified("ForeignKey"))
     {
         manyToOneMapping.ForeignKey = foreignKeyName;
     }
 }
Beispiel #7
0
 public static void ForeignKey(this ManyToOneMapping manyToOneMapping, string foreignKeyName)
 {
     if (!manyToOneMapping.IsSpecified("ForeignKey"))
     {
         manyToOneMapping.Set <string>(c => c.ForeignKey, 1, foreignKeyName);
     }
 }
Beispiel #8
0
        public override void ProcessManyToOne(ManyToOneMapping mapping)
        {
            var conventions = finder.Find <IReferenceConvention>();

            Apply <IManyToOneInspector, IManyToOneInstance>(conventions,
                                                            new ManyToOneInstance(mapping));
        }
Beispiel #9
0
        public override void establish_context()
        {
            manyToOneARankedFirstToHolder = new ManyToOneMapping();
            manyToOneARankedFirstToHolder.Set(x => x.Class, Layer.Defaults, new TypeReference(typeof(Holder)));
            manyToOneARankedFirstToHolder.Set(x => x.Name, Layer.Defaults, "ARankedFirstProperty");
            manyToOneARankedFirstToHolder.ContainingEntityType = typeof(ARankedFirst);
            manyToOneARankedFirstToHolder.Member = new PropertyMember(typeof(ARankedFirst).GetProperty("ARankedFirstProperty"));

            manyToOneBRankedSecondToHolder = new ManyToOneMapping();
            manyToOneBRankedSecondToHolder.Set(x => x.Class, Layer.Defaults, new TypeReference(typeof(Holder)));
            manyToOneBRankedSecondToHolder.Set(x => x.Name, Layer.Defaults, "BRankedSecondProperty");
            manyToOneBRankedSecondToHolder.ContainingEntityType = typeof(BRankedSecond);
            manyToOneBRankedSecondToHolder.Member = new PropertyMember(typeof(BRankedSecond).GetProperty("BRankedSecondProperty"));

            var relationship = new OneToManyMapping();

            relationship.Set(x => x.Class, Layer.Defaults, new TypeReference(typeof(BRankedSecond)));
            relationship.ContainingEntityType = typeof(Holder);

            collectionMappingToBRankedSecond = CollectionMapping.Bag();
            collectionMappingToBRankedSecond.Set(x => x.ChildType, Layer.Defaults, typeof(BRankedSecond));
            collectionMappingToBRankedSecond.Set(x => x.Name, Layer.Defaults, "ColectionOfBRankedSeconds");
            collectionMappingToBRankedSecond.Set(x => x.Relationship, Layer.Defaults, relationship);
            collectionMappingToBRankedSecond.ContainingEntityType = typeof(Holder);

            visitor = new RelationshipPairingVisitor(A.Fake <PairBiDirectionalManyToManySidesDelegate>());
        }
        public override void Visit(ManyToOneMapping mapping)
        {
            var writer = serviceLocator.GetWriter<ManyToOneMapping>();
            var xml = writer.Write(mapping);

            document.ImportAndAppendChild(xml);
        }
        public void ShouldWriteProxyLazyAttribute()
        {
            var mapping = new ManyToOneMapping();

            mapping.Set(x => x.Lazy, Layer.Defaults, Laziness.Proxy.ToString());

            writer.VerifyXml(mapping)
            .HasAttribute("lazy", "proxy");
        }
        public void CanAddReference()
        {
            var reference = new ManyToOneMapping();

            reference.Set(x => x.Name, Layer.Defaults, "parent");
            mapping.AddReference(reference);

            mapping.References.ShouldContain(reference);
        }
Beispiel #13
0
        public void ShouldWriteColumns()
        {
            var mapping = new ManyToOneMapping();

            mapping.AddColumn(new ColumnMapping());

            writer.VerifyXml(mapping)
            .Element("column").Exists();
        }
Beispiel #14
0
        public void ShouldWriteProxyLazyAttribute()
        {
            var mapping = new ManyToOneMapping();

            mapping.Lazy = true;

            writer.VerifyXml(mapping)
            .HasAttribute("lazy", "proxy");
        }
Beispiel #15
0
        protected ManyToOneMapping reference_with_column(string column)
        {
            var reference = new ManyToOneMapping();

            reference.AddDefaultColumn(new ColumnMapping {
                Name = "propertyColumn"
            });
            return(reference);
        }
        public void CanAddReference()
        {
            var reference = new ManyToOneMapping {
                Name = "parent"
            };

            _classMapping.AddReference(reference);

            _classMapping.References.ShouldContain(reference);
        }
        public ColumnMapping Apply(ManyToOneMapping manyToOneMap, Lazy <Dictionary <Type, ClassMapping> > lazyTypeMap)
        {
            var codeListAttr = manyToOneMap.Member.PropertyType.GetCustomAttribute <CodeListConfigurationAttribute>(false);
            var length       = codeListAttr?.CodeLength ?? 20;
            var keyName      = GetKeyName(manyToOneMap.Member, manyToOneMap.Class.GetUnderlyingSystemType());
            var col          = manyToOneMap.Columns.First();

            col.Set(o => o.Name, Layer.UserSupplied, keyName);
            col.Set(o => o.Length, Layer.UserSupplied, length);
            return(col);
        }
Beispiel #18
0
        /// <summary>
        /// Sets the Index.
        /// </summary>
        /// <param name="manyToOneMapping">The many to one mapping.</param>
        /// <param name="indexName">Name of the index.</param>
        public static void Index(this ManyToOneMapping manyToOneMapping, string indexName)
        {
            if (manyToOneMapping.Columns.First().IsSpecified("Index"))
            {
                return;
            }

            foreach (var column in manyToOneMapping.Columns)
            {
                column.Index = indexName;
            }
        }
        /// <summary>
        /// Sets the Foreign Key.
        /// </summary>
        /// <param name="manyToOneMapping">The many to one mapping.</param>
        /// <param name="foreignKeyName">Name of the foreign key.</param>
        public static void ForeignKey(this ManyToOneMapping manyToOneMapping, string foreignKeyName)
        {
            if (!manyToOneMapping.IsSpecified("ForeignKey"))
            {
                manyToOneMapping.Set(x => x.ForeignKey, Layer.Defaults, foreignKeyName);

                //var columnPart = new ManyToOnePart(column);
                //columnPart. (indexName);

                // manyToOneMapping.ForeignKey = foreignKeyName;
            }
        }
        public override void ProcessManyToOne(ManyToOneMapping thisSide)
        {
            if (thisSide.OtherSide == null)
            {
                return;
            }

            // other side is always going to be a collection for a many-to-one mapping
            var otherSide = (CollectionMapping)thisSide.OtherSide;

            otherSide.Key.MakeColumnsEmpty(Layer.Defaults);
            thisSide.Columns.Each(x => otherSide.Key.AddColumn(Layer.Defaults, x.Clone()));
        }
Beispiel #21
0
        private ManyToOneMapping CreateMapping(Member property)
        {
            var mapping = new ManyToOneMapping {
                Member = property
            };

            mapping.SetDefaultValue(x => x.Name, property.Name);
            mapping.SetDefaultValue(x => x.Class, new TypeReference(property.PropertyType));
            mapping.AddDefaultColumn(new ColumnMapping {
                Name = property.Name + "_id"
            });

            return(mapping);
        }
Beispiel #22
0
        protected virtual ManyToOneBuilder <TOther> References <TOther>(Member property, string columnName)
        {
            var manyToOneMapping = new ManyToOneMapping();
            var part             = new ManyToOneBuilder <TOther>(manyToOneMapping, typeof(T), property);

            if (columnName != null)
            {
                part.Column(columnName);
            }

            mapping.AddReference(manyToOneMapping);

            return(part);
        }
        protected virtual ManyToOneBuilder <TOther> References <TOther>(Member property, string columnName)
        {
            var manyToOneMapping = new ManyToOneMapping();
            var part             = new ManyToOneBuilder <TOther>(manyToOneMapping, EntityType, property);

            if (columnName != null)
            {
                part.Column(columnName);
            }

            references.Add(new PassThroughMappingProvider(manyToOneMapping));

            return(part);
        }
Beispiel #24
0
        /// <summary>
        /// Sets the column.
        /// </summary>
        /// <param name="manyToOneMapping">The many to one mapping.</param>
        /// <param name="columnName">Name of the column.</param>
        public static void Column(this ManyToOneMapping manyToOneMapping, string columnName)
        {
            if (manyToOneMapping.Columns.UserDefined.Count() > 0)
            {
                return;
            }

            var originalColumn = manyToOneMapping.Columns.FirstOrDefault();
            var column         = originalColumn == null ? new ColumnMapping() : originalColumn.Clone();

            column.Name = columnName;

            manyToOneMapping.ClearColumns();
            manyToOneMapping.AddColumn(column);
        }
        void SetDefaultAccess(Member member, ManyToOneMapping mapping)
        {
            var resolvedAccess = MemberAccessResolver.Resolve(member);

            if (resolvedAccess != Access.Property && resolvedAccess != Access.Unset)
            {
                // if it's a property or unset then we'll just let NH deal with it, otherwise
                // set the access to be whatever we determined it might be
                mapping.Set(x => x.Access, Layer.Defaults, resolvedAccess.ToString());
            }

            if (member.IsProperty && !member.CanWrite)
            {
                mapping.Set(x => x.Access, Layer.Defaults, cfg.GetAccessStrategyForReadOnlyProperty(member).ToString());
            }
        }
        private ManyToOneMapping CreateMapping(Member member)
        {
            var mapping = new ManyToOneMapping {
                Member = member
            };

            mapping.Set(x => x.Name, Layer.Defaults, member.Name);
            mapping.Set(x => x.Class, Layer.Defaults, new TypeReference(member.PropertyType));
            var columnMapping = new ColumnMapping();

            columnMapping.Set(x => x.Name, Layer.Defaults, member.Name + "_id");
            mapping.AddColumn(Layer.Defaults, columnMapping);

            SetDefaultAccess(member, mapping);

            return(mapping);
        }
        public override void ProcessManyToOne(ManyToOneMapping thisSide)
        {
            if (thisSide.OtherSide == null)
            {
                return;
            }

            // other side is always going to be a collection for a many-to-one mapping
            var otherSide = (CollectionMapping)thisSide.OtherSide;

            if (thisSide.ContainingEntityType == otherSide.ContainingEntityType)
            {
                // special case for self-referential relationships
                otherSide.Key.MakeColumnsEmpty(Layer.Defaults);
                thisSide.Columns.Each(x => otherSide.Key.AddColumn(Layer.Defaults, x.Clone()));
            }
        }
Beispiel #28
0
        protected virtual NaturalIdPart <T> Reference(Member member, string columnName)
        {
            var key = new ManyToOneMapping
            {
                Name  = member.Name,
                Class = new TypeReference(member.PropertyType),
                ContainingEntityType = typeof(T)
            };

            key.AddColumn(new ColumnMapping {
                Name = columnName
            });

            manyToOnes.Add(key);

            return(this);
        }
Beispiel #29
0
        protected virtual NaturalIdPart <T> Reference(Member member, string columnName)
        {
            var key = new ManyToOneMapping
            {
                ContainingEntityType = typeof(T)
            };

            key.Set(x => x.Name, Layer.Defaults, member.Name);
            key.Set(x => x.Class, Layer.Defaults, new TypeReference(member.PropertyType));
            var columnMapping = new ColumnMapping();

            columnMapping.Set(x => x.Name, Layer.Defaults, columnName);
            key.AddColumn(Layer.Defaults, columnMapping);

            manyToOnes.Add(key);

            return(this);
        }
        /// <summary>
        /// Sets the column.
        /// </summary>
        /// <param name="manyToOneMapping">The many to one mapping.</param>
        /// <param name="columnName">Name of the column.</param>
        public static void Column(this ManyToOneMapping manyToOneMapping, string columnName)
        {
            if (manyToOneMapping.Columns.Any())
            {
                return;
            }

            var originalColumn = manyToOneMapping.Columns.FirstOrDefault();
            var column         = originalColumn == null ? new ColumnMapping() : originalColumn.Clone();
            var columnPart     = new ColumnPart(column);

            columnPart.Name(columnName);

            manyToOneMapping.MakeColumnsEmpty(Layer.Defaults);
            manyToOneMapping.AddColumn(Layer.Defaults, column);

            //  manyToOneMapping.AddColumn ( column );
        }
Beispiel #31
0
        private ManyToOneMapping CreateMapping(Member member)
        {
            var mapping = new ManyToOneMapping {
                Member = member
            };

            mapping.SetDefaultValue(x => x.Name, member.Name);
            mapping.SetDefaultValue(x => x.Class, new TypeReference(member.PropertyType));
            mapping.AddDefaultColumn(new ColumnMapping {
                Name = member.Name + "_id"
            });

            if (member.IsProperty && !member.CanWrite)
            {
                mapping.Access = cfg.GetAccessStrategyForReadOnlyProperty(member).ToString();
            }

            return(mapping);
        }
 public override void ProcessManyToOne(ManyToOneMapping manyToOneMapping)
 {
     Process(manyToOneMapping, manyToOneMapping.PropertyInfo);
 }
 public void AddReference(ManyToOneMapping manyToOne)
 {
     mergedComponent.AddReference(manyToOne);
 }
 public void AddReference(ManyToOneMapping manyToOne)
 {
     mappedMembers.AddReference(manyToOne);
 }
 public object Write(ManyToOneMapping mappingModel)
 {
     return _manyToOneWriter.Write(mappingModel);
 }