Esempio n. 1
0
        public void PostBelongsToBlog()
        {
            InitKernel();
            IRelationshipInferenceService relService = ObtainService();

            TableDefinition blogTable;
            TableDefinition postTable;

            BuildBlogPostsStructure(out blogTable, out postTable);

            BuildContext context = new BuildContext();

            ActiveRecordDescriptor arDesc = new ActiveRecordDescriptor();

            ActiveRecordPropertyDescriptor[] descs = relService.InferRelations(arDesc, postTable, context);

            Assert.IsNotNull(descs);
            Assert.AreEqual(1, descs.Length);

            ActiveRecordBelongsToDescriptor desc1 = descs[0] as ActiveRecordBelongsToDescriptor;

            Assert.IsNotNull(desc1);
            Assert.IsNotNull(desc1.TargetType);
            Assert.IsNull(desc1.PropertyType);

            Assert.AreEqual("Blog", desc1.PropertyName);
            Assert.AreEqual("blog_id", desc1.ColumnName);

            ActiveRecordDescriptor targetARDescriptor = context.GetNextPendent();

            Assert.AreSame(blogTable, targetARDescriptor.Table);
        }
Esempio n. 2
0
        public void SelfReference()
        {
            InitKernel();
            IRelationshipInferenceService relService = ObtainService();

            DatabaseDefinition dbdef = new DatabaseDefinition("alias");

            TableDefinition categoryTable = new TableDefinition("categories", dbdef);

            categoryTable.AddColumn(new ColumnDefinition("id", true, false, true, false, OleDbType.Integer));
            categoryTable.AddColumn(new ColumnDefinition("name", false, false, false, false, OleDbType.VarChar));
            categoryTable.AddColumn(new ColumnDefinition("parent_id", false, true, false, false, OleDbType.Integer, categoryTable));

            categoryTable.AddManyRelation(categoryTable);

            BuildContext context = new BuildContext();

            ActiveRecordDescriptor arDesc = new ActiveRecordDescriptor();

            ActiveRecordPropertyDescriptor[] descs = relService.InferRelations(arDesc, categoryTable, context);

            Assert.IsFalse(context.HasPendents);

            Assert.IsNotNull(descs);
            Assert.AreEqual(2, descs.Length);

            ActiveRecordHasManyDescriptor desc1 = descs[0] as ActiveRecordHasManyDescriptor;

            Assert.IsNotNull(desc1);
            Assert.IsNotNull(desc1.TargetType);
            Assert.IsNotNull(desc1.PropertyType);

            Assert.AreEqual("Categories", desc1.PropertyName);
            Assert.AreEqual("parent_id", desc1.ColumnName);
            Assert.AreEqual(typeof(IList), desc1.PropertyType);

            ActiveRecordBelongsToDescriptor desc2 = descs[1] as ActiveRecordBelongsToDescriptor;

            Assert.IsNotNull(desc2);
            Assert.IsNotNull(desc2.TargetType);
            Assert.IsNull(desc2.PropertyType);
            Assert.AreEqual("Category", desc2.PropertyName);
            Assert.AreEqual("parent_id", desc2.ColumnName);
        }
        private void CreateBelongsToRelations(ActiveRecordDescriptor desc, TableDefinition tableDef,
                                              IList list, BuildContext context)
        {
            foreach (ColumnDefinition col in tableDef.Columns)
            {
                if (col.RelatedTable != null)
                {
                    bool   pendentNecessary = false;
                    String propertyName     = _namingService.CreateClassName(col.RelatedTable.Name);

                    ActiveRecordDescriptor targetType = null;

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

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

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

                    ActiveRecordBelongsToDescriptor belongsTo =
                        new ActiveRecordBelongsToDescriptor(col.Name,
                                                            propertyName, targetType);

                    list.Add(belongsTo);

                    if (pendentNecessary)
                    {
                        context.AddPendentDescriptor(belongsTo, col.RelatedTable.RelatedDescriptor);
                    }
                }
            }
        }