private ActiveRecordPropertyRelationDescriptor CreateHasManyRelation(RelationshipInfo info)
        {
            // Validates first

            if (info.ChildCol == null)
            {
                throw new ArgumentException("No column specified");
            }

            String colName  = info.ChildCol.Name;
            String propName = _namingService.CreateRelationName(info.TargetDescriptor.ClassName);

            return(new ActiveRecordHasManyDescriptor(colName, propName, info.TargetDescriptor));
        }
        private void CreateHasManyRelations(ActiveRecordDescriptor desc, TableDefinition tableDef,
                                            IList list, BuildContext context)
        {
            foreach (TableDefinition fkTable in tableDef.TablesReferencedByHasRelation)
            {
                String propertyName = _namingService.CreateRelationName(fkTable.Name);
                ActiveRecordDescriptor targetType = null;
                String           colName          = null;
                bool             pendentNecessary = false;
                ColumnDefinition selectedCol      = null;

                foreach (ColumnDefinition col in fkTable.Columns)
                {
                    if (col.RelatedTable == tableDef)
                    {
                        colName = col.Name;

                        if (col.RelatedTable.RelatedDescriptor == null && col.RelatedTable != fkTable)
                        {
                            col.RelatedTable.RelatedDescriptor = new ActiveRecordDescriptor(fkTable);

                            pendentNecessary = true;
                        }
                        else if (col.RelatedTable == tableDef)
                        {
                            targetType = desc;
                        }

                        if (targetType == null)
                        {
                            targetType = col.RelatedTable.RelatedDescriptor;
                        }

                        selectedCol = col;

                        break;
                    }
                }

                // Just to protect ourselves from awkward conditions
                if (colName == null)
                {
                    continue;
                }

                ActiveRecordHasManyDescriptor hasMany =
                    new ActiveRecordHasManyDescriptor(colName, propertyName, targetType);

                if (pendentNecessary)
                {
                    context.AddPendentDescriptor(hasMany, selectedCol.RelatedTable.RelatedDescriptor);
                }

                list.Add(hasMany);
            }
        }
Example #3
0
        public void CreateRelationName()
        {
            INamingService service = (INamingService)Kernel[typeof(INamingService)];

            Assert.AreEqual("Posts", service.CreateRelationName("Post"));
            Assert.AreEqual("Children", service.CreateRelationName("child"));
            Assert.AreEqual("Posts", service.CreateRelationName("posts"));
            Assert.AreEqual("Posts", service.CreateRelationName("post"));
            Assert.AreEqual("Orders", service.CreateRelationName("order"));
            Assert.AreEqual("Facilities", service.CreateRelationName("facility"));
        }