Defines a relationship where the owner relates to only one other object.
Наследование: RelationshipDef, ISingleRelationshipDef
 public void TestRemove()
 {
     SingleRelationshipDef relDef = new SingleRelationshipDef("rel", typeof(MyRelatedBo), new RelKeyDef(), true, DeleteParentAction.Prevent);
     RelationshipDefColInheritor col = new RelationshipDefColInheritor();
     
     col.CallRemove(relDef);
     col.Add(relDef);
     Assert.AreEqual(1, col.Count);
     col.CallRemove(relDef);
     Assert.AreEqual(0, col.Count);
 }
Пример #2
0
 private static MockBO GetMockBO(out RelationshipDef mRelationshipDef, out RelKeyDef mRelKeyDef)
 {
     MockBO _mMockBO= new MockBO();
     IPropDefCol mPropDefCol = _mMockBO.PropDefCol;
     mRelKeyDef = new RelKeyDef();
     IPropDef propDef = mPropDefCol["MockBOProp1"];
     RelPropDef lRelPropDef = new RelPropDef(propDef, "MockBOID");
     mRelKeyDef.Add(lRelPropDef);
     mRelationshipDef = new SingleRelationshipDef("Relation1", typeof(MockBO), mRelKeyDef, false,
                                                  DeleteParentAction.Prevent);
     return _mMockBO;
 }
Пример #3
0
        private static RelationshipDefCol CreateRelationshipDefCol(PropDefCol lPropDefCol)
        {
            RelationshipDefCol relDefCol = new RelationshipDefCol();

            //Define Engine Relationships
            RelKeyDef relKeyDef = new RelKeyDef();
            IPropDef propDef = lPropDefCol["CarID"];

            RelPropDef lRelPropDef = new RelPropDef(propDef, "CarID");
            relKeyDef.Add(lRelPropDef);

            RelationshipDef relDef = new SingleRelationshipDef("Car", typeof (Car), relKeyDef, false, DeleteParentAction.Prevent);
            relDefCol.Add(relDef);

            return relDefCol;
        }
 public void TestAddDuplicationException()
 {
     //---------------Set up test pack-------------------
     SingleRelationshipDef relDef = new SingleRelationshipDef("rel", typeof(MyRelatedBo), new RelKeyDef(), true, DeleteParentAction.Prevent);
     RelationshipDefCol col = new RelationshipDefCol();
     col.Add(relDef);
     //---------------Execute Test ----------------------
     try
     {
         col.Add(relDef);
         Assert.Fail("Expected to throw an ArgumentException");
     }
         //---------------Test Result -----------------------
     catch (ArgumentException ex)
     {
         StringAssert.Contains("A relationship definition with the name 'rel' already exists", ex.Message);
     }
 }
Пример #5
0
        private static RelationshipDefCol CreateRelationshipDefCol(IPropDefCol lPropDefCol)
        {
            RelationshipDefCol relDefCol = new RelationshipDefCol();

            //Define Owner Relationships
            RelKeyDef relKeyDef = new RelKeyDef();
            IPropDef propDef = lPropDefCol["OwnerId"];

            RelPropDef lRelPropDef = new RelPropDef(propDef, "ContactPersonID");
            relKeyDef.Add(lRelPropDef);

            RelationshipDef relDef = new SingleRelationshipDef("Owner", typeof(ContactPerson), relKeyDef, false, DeleteParentAction.Prevent);

            relDefCol.Add(relDef);

            //Define Driver Relationships
            relKeyDef = new RelKeyDef();
            propDef = lPropDefCol["DriverFK1"];

            lRelPropDef = new RelPropDef(propDef, "PK1Prop1");
            relKeyDef.Add(lRelPropDef);

            propDef = lPropDefCol["DriverFK2"];

            lRelPropDef = new RelPropDef(propDef, "PK1Prop2");
            relKeyDef.Add(lRelPropDef);

            relDef = new SingleRelationshipDef("Driver", typeof(ContactPersonCompositeKey), relKeyDef, true, DeleteParentAction.Prevent);

            relDefCol.Add(relDef);

            //Define Engine Relationships
            relKeyDef = new RelKeyDef();
            propDef = lPropDefCol["CarID"];

            lRelPropDef = new RelPropDef(propDef, "CarID");
            relKeyDef.Add(lRelPropDef);

            relDef = new SingleRelationshipDef("Engine", typeof(Engine), relKeyDef, false, DeleteParentAction.DereferenceRelated)
                         {OwningBOHasForeignKey = false};
            relDefCol.Add(relDef);
            return relDefCol;
        }
        public void init()
        {
            BORegistry.DataAccessor = new DataAccessorInMemory();
            _fakeBO = new MockBO();
            _propDefCol = _fakeBO.PropDefCol;

            _RelKeyDef = new RelKeyDef();
            IPropDef propDef = _propDefCol["MockBOID"];

            RelPropDef relPropDef = new RelPropDef(propDef, "MockBOProp1");
            _RelKeyDef.Add(relPropDef);

            _multipleRelationshipDef = new MultipleRelationshipDef("Relation1", typeof(MockBO),
                                                       _RelKeyDef, false, "",
                                                       DeleteParentAction.DeleteRelated);


            _singleRelationshipDef = new SingleRelationshipDef("Single", typeof(MockBO),
                                                       _RelKeyDef, false,
                                                       DeleteParentAction.DeleteRelated);
        }
        public void init()
        {
            _mockBo = new MockBO();
            _propDefCol = _mockBo.PropDefCol;

            _RelKeyDef = new RelKeyDef();
            IPropDef propDef = _propDefCol["MockBOID"];

            RelPropDef relPropDef = new RelPropDef(propDef, "MockBOProp1");
            _RelKeyDef.Add(relPropDef);

            _multipleRelationshipDef = new MultipleRelationshipDef("Relation1", typeof(MockBO),
                                                       _RelKeyDef, false, "",
                                                       DeleteParentAction.DeleteRelated);


            _singleRelationshipDef = new SingleRelationshipDef("Single", typeof(MockBO),
                                                       _RelKeyDef, false,
                                                       DeleteParentAction.DeleteRelated);
            DatabaseConnection.CurrentConnection.ConnectionString = MyDBConnection.GetConnectionString();
        }
        /// <summary>
        /// Maps the <see cref="ReflectionWrappers.PropertyWrapper"/> to a <see cref="IRelationshipDef"/>.
        /// </summary>
        /// <returns></returns>
        public IRelationshipDef MapOneToOne()
        {
            if (!MustBeMapped()) return null;
            CheckReverseRelationshipValid();
            
            var relatedClassType = PropertyWrapper.RelatedClassType.UnderlyingType;
            DeleteParentAction deleteAction = GetDeleteAction();

            var relDef = new SingleRelationshipDef(this.PropertyWrapper.Name, relatedClassType
                                            , new RelKeyDef(), true, deleteAction)
                      {
                          OwningBOHasForeignKey = this.OwningBoHasForeignKey,
                          ReverseRelationshipName = this.ReverseRelationshipName
                      };
            SetRelationshipType(relDef);
            relDef.SetAsOneToOne();
            IRelPropDef relPropDef = this.CreateRelPropDef();
            relDef.RelKeyDef.Add(relPropDef);
            return relDef;
        }
        private void SetRelationshipType(SingleRelationshipDef relDef)
        {
            var onToManyAtt = this.PropertyWrapper.GetAttribute<AutoMapOneToOneAttribute>();

            if (onToManyAtt != null) relDef.RelationshipType = onToManyAtt.RelationshipType;
        }
 private static IRelationshipDef CreateReverseRelDef(IRelationshipDef rel, IClassDef classDef)
 {
     IRelationshipDef newReverseRelDef;
     if (rel.IsManyToOne)
     {
         newReverseRelDef = new MultipleRelationshipDef(rel.ReverseRelationshipName
                                                        , classDef.ClassType, new RelKeyDef(), true, ""
                                                        , DeleteParentAction.Prevent);
     }else
     {
         newReverseRelDef = new SingleRelationshipDef(rel.ReverseRelationshipName
                                                      , classDef.ClassType, new RelKeyDef(), true
                                                      , DeleteParentAction.DoNothing);
     }
     newReverseRelDef.ReverseRelationshipName = rel.RelationshipName;
     return newReverseRelDef;
 }
Пример #11
0
 public void TestGetRelatedObject()
 {
     RelationshipDef mRelationshipDef;
     RelKeyDef mRelKeyDef;
     MockBO _mMockBO= GetMockBO(out mRelationshipDef, out mRelKeyDef);
     RelationshipDef lRelationshipDef = new SingleRelationshipDef("Relation1", typeof (MockBO), mRelKeyDef, true,
                                                                  DeleteParentAction.Prevent);
     ISingleRelationship rel =
         (ISingleRelationship) lRelationshipDef.CreateRelationship(_mMockBO, _mMockBO.PropCol);
     Assert.AreEqual(lRelationshipDef.RelationshipName, rel.RelationshipName);
     Assert.IsTrue(_mMockBO.GetPropertyValue("MockBOProp1") == null);
     Assert.IsFalse(rel.HasRelatedObject(), "Should be false since props are not defaulted in Mock bo");
     //Set a related object
     _mMockBO.SetPropertyValue("MockBOProp1", _mMockBO.GetPropertyValue("MockBOID"));
     //Save the object, so that the relationship can retrieve the object from the database
     _mMockBO.Save();
     Assert.IsTrue(rel.HasRelatedObject(), "Should have a related object since the relating props have values");
     MockBO ltempBO = (MockBO) rel.GetRelatedObject();
     Assert.IsNotNull(ltempBO, "The related object should exist");
     //Clear the related object
     _mMockBO.SetPropertyValue("MockBOProp1", null);
     Assert.IsFalse(rel.HasRelatedObject(),
                    "Should not have a related object since the relating props have been set to null");
     ltempBO = (MockBO) rel.GetRelatedObject();
     Assert.IsNull(ltempBO, "The related object should now be null");
     //Set a related object again
     _mMockBO.SetPropertyValue("MockBOProp1", _mMockBO.GetPropertyValue("MockBOID"));
     Assert.IsTrue(rel.HasRelatedObject(),
                   "Should have a related object since the relating props have values again");
     ltempBO = (MockBO) rel.GetRelatedObject();
     Assert.IsNotNull(ltempBO, "The related object should exist again");
     _mMockBO.MarkForDelete();
     _mMockBO.Save();
 }
Пример #12
0
        public void TestOwningBOHasForeignKey_Single()
        {
            //---------------Set up test pack-------------------
            SingleRelationshipDef relDef = new SingleRelationshipDef
                ("rel", typeof (MyRelatedBo), new RelKeyDef(), true, DeleteParentAction.Prevent);

            //---------------Execute Test ----------------------
            relDef.OwningBOHasForeignKey = false;
            //---------------Test Result -----------------------
            Assert.IsFalse(relDef.OwningBOHasForeignKey);
            //---------------Tear Down -------------------------          
        }
Пример #13
0
        public static IClassDef LoadDefaultClassDef_SingleRel_NoReverseRelationship()
        {
            XmlClassLoader itsLoader = CreateXmlClassLoader();
            IClassDef itsClassDef =
                itsLoader.LoadClass(
                    @"
				<class name=""OrganisationTestBO"" assembly=""Habanero.Test.BO"" table=""organisation"">
					<property  name=""OrganisationID"" type=""Guid"" />
                    <property name=""Name"" />
					<primaryKey>
						<prop name=""OrganisationID"" />
					</primaryKey>
			    </class>
			");
            RelPropDef relPropDef = new RelPropDef(itsClassDef.PropDefcol["OrganisationID"], "OrganisationID");
            RelKeyDef relKeyDef = new RelKeyDef();
            relKeyDef.Add(relPropDef);
            IRelationshipDef relationshipDef = new SingleRelationshipDef("ContactPerson", "Habanero.Test.BO",
                    "ContactPersonTestBO", relKeyDef, true, DeleteParentAction.DeleteRelated, InsertParentAction.InsertRelationship, RelationshipType.Aggregation);
            relationshipDef.OwningBOHasForeignKey = false;
            itsClassDef.RelationshipDefCol.Add(relationshipDef);
            ClassDef.ClassDefs.Add(itsClassDef);
            return itsClassDef;
        }
Пример #14
0
        public void TestFieldDefaultLabelFromRelatedClassDef()
        {
            ClassDef.ClassDefs.Clear();
            ClassDef classDef = CreateTestClassDef("");
            ClassDef classDef2 = CreateTestClassDef("2");
            ClassDef.ClassDefs.Add(classDef2);
            RelKeyDef relKeyDef = new RelKeyDef();
            RelPropDef relPropDef = new RelPropDef(classDef.PropDefcol["TestProperty"], "TestProperty2");
            relKeyDef.Add(relPropDef);
            SingleRelationshipDef def = new SingleRelationshipDef("TestRel", classDef2.AssemblyName, classDef2.ClassName, relKeyDef, false, DeleteParentAction.Prevent);
            classDef.RelationshipDefCol.Add(def);

            UIFormField uiFormField = new UIFormField(null, "TestRel.TestProperty2", typeof(TextBox), null, null, true, null, null, LayoutStyle.Label);
            Assert.AreEqual("Tested Property2:", uiFormField.GetLabel(classDef));
        }
        /// <summary>
        /// Maps the <see cref="PropertyInfo"/> to a Many to One relationship
        /// </summary>
        /// <returns></returns>
        public IRelationshipDef MapManyToOne()
        {
            if (this.PropertyWrapper.DeclaringType == (Type)null) return null;
            if (!MustBeMapped()) return null;

            var relatedObjectType = this.PropertyWrapper.PropertyType;
            if (this.PropertyWrapper.HasAttribute<AutoMapManyToOneAttribute>())
            {
                var manyToOneAttribute = this.PropertyWrapper.GetAttribute<AutoMapManyToOneAttribute>();
                var specifiedType = manyToOneAttribute.RelatedObjectClassType;
                if (specifiedType != null) relatedObjectType = specifiedType.ToTypeWrapper();
            }
            if (!relatedObjectType.IsBusinessObject)
            {
                throw new InvalidDefinitionException(string.Format(
                    "The specified RelatedObjectClassType '{0}' on '{1}.{2}' must be a Business Object",
                    relatedObjectType, this.PropertyWrapper.DeclaringClassName, this.PropertyWrapper.Name));
            }
            if (!relatedObjectType.IsOfType(this.PropertyWrapper.UnderlyingPropertyType))
            {
                throw new InvalidDefinitionException(string.Format(
                    "The specified RelatedObjectClassType '{0}' on '{1}.{2}' must be assignment compatible with the actual property type '{3}'",
                    relatedObjectType, this.PropertyWrapper.DeclaringClassName,
                    this.PropertyWrapper.Name, this.PropertyWrapper.UnderlyingPropertyType));
            }

            var relDef = new SingleRelationshipDef(this.PropertyWrapper.Name, relatedObjectType.UnderlyingType,
                new RelKeyDef(), true, DeleteParentAction.DoNothing);

            SetRelationshipType(relDef);
            if(this.PropertyWrapper.HasCompulsoryAttribute) relDef.SetAsCompulsory();
            relDef.OwningBOHasForeignKey = true;
            SetReverseRelationshipName(relDef);
            var ownerPropName = GetOwningPropName();
            var relatedPropName = GetRelatedPropName(relatedObjectType);
            IRelPropDef relPropDef = new RelPropDef(ownerPropName, relatedPropName);
            relDef.RelKeyDef.Add(relPropDef);
            return relDef;
        }
Пример #16
0
        public void TestFieldDefaultLabelFromRelatedClassDef()
        {
            ClassDef.ClassDefs.Clear();
            ClassDef classDef = CreateTestClassDef("");
            ClassDef classDef2 = CreateTestClassDef("2");
            ClassDef.ClassDefs.Add(classDef2);
            RelKeyDef relKeyDef = new RelKeyDef();
            RelPropDef relPropDef = new RelPropDef(classDef.PropDefcol["TestProperty"], "TestProperty2");
            relKeyDef.Add(relPropDef);
            SingleRelationshipDef def = new SingleRelationshipDef("TestRel", classDef2.AssemblyName, classDef2.ClassName, relKeyDef, false, DeleteParentAction.Prevent);
            classDef.RelationshipDefCol.Add(def);

            UIGridColumn uiGridColumn;
            uiGridColumn = new UIGridColumn(null, "TestRel.TestProperty2", typeof(DataGridViewTextBoxColumn), false, 100, PropAlignment.left, null);
#pragma warning disable 612,618
            Assert.AreEqual("Tested Property2", uiGridColumn.GetHeading(classDef));
#pragma warning restore 612,618
        }
Пример #17
0
        public void TestCreateRelationshipHoldRelRef()
        {
            RelationshipDef mRelationshipDef;
            RelKeyDef mRelKeyDef;
            MockBO _mMockBO= GetMockBO(out mRelationshipDef, out mRelKeyDef);
            RelationshipDef lRelationshipDef = new SingleRelationshipDef("Relation1", typeof (MockBO), mRelKeyDef, true,
                                                                         DeleteParentAction.Prevent);
            ISingleRelationship rel =
                (ISingleRelationship) lRelationshipDef.CreateRelationship(_mMockBO, _mMockBO.PropCol);
            Assert.AreEqual(lRelationshipDef.RelationshipName, rel.RelationshipName);
            Assert.IsTrue(_mMockBO.GetPropertyValue("MockBOProp1") == null);
            Assert.IsFalse(rel.HasRelatedObject(), "Should be false since props are not defaulted in Mock bo");
            _mMockBO.SetPropertyValue("MockBOProp1", _mMockBO.GetPropertyValue("MockBOID"));
            _mMockBO.Save();
            Assert.IsTrue(rel.HasRelatedObject(), "Should be true since prop MockBOProp1 has been set");

            Assert.AreEqual(_mMockBO.GetPropertyValue("MockBOProp1"), _mMockBO.GetPropertyValue("MockBOID"));
            MockBO ltempBO = (MockBO) rel.GetRelatedObject();
            Assert.IsFalse(ltempBO == null);
            Assert.AreEqual(_mMockBO.GetPropertyValue("MockBOID"), ltempBO.GetPropertyValue("MockBOID"),
                            "The object returned should be the one with the ID = MockBOID");
            Assert.AreEqual(_mMockBO.GetPropertyValueString("MockBOProp1"), ltempBO.GetPropertyValueString("MockBOID"),
                            "The object returned should be the one with the ID = MockBOID");
            Assert.AreEqual(_mMockBO.GetPropertyValue("MockBOProp1"), ltempBO.GetPropertyValue("MockBOID"),
                            "The object returned should be the one with the ID = MockBOID");

            Assert.IsTrue(ReferenceEquals(ltempBO, rel.GetRelatedObject()));
            FixtureEnvironment.ClearBusinessObjectManager();
            Assert.IsTrue(ReferenceEquals(ltempBO, rel.GetRelatedObject()));
            _mMockBO.MarkForDelete();
            _mMockBO.Save();
        }
Пример #18
0
        public void TestGetRelationship()
        {
            ClassDef parentClassDef = new ClassDef(typeof(String), null, null, null, new RelationshipDefCol());
            Assert.IsNull(parentClassDef.GetRelationship("wrongrel"));

            ClassDef childClassDef = new ClassDef(typeof(String), null, null, null, new RelationshipDefCol());
            childClassDef.SuperClassDef = new SuperClassDef(parentClassDef, ORMapping.ClassTableInheritance);
            Assert.IsNull(parentClassDef.GetRelationship("wrongrel"));

            PropDef propDef = new PropDef("prop", typeof(String), PropReadWriteRule.ReadWrite, null);
            RelPropDef relPropDef = new RelPropDef(propDef, "relProp");
            RelKeyDef relKeyDef = new RelKeyDef();
            relKeyDef.Add(relPropDef);
            RelationshipDef relDef = new SingleRelationshipDef("rel", typeof(MyRelatedBo), relKeyDef, true, DeleteParentAction.Prevent);
            childClassDef.RelationshipDefCol.Add(relDef);
            Assert.AreEqual(relDef, childClassDef.GetRelationship("rel"));

            childClassDef.RelationshipDefCol = new RelationshipDefCol();
            parentClassDef.RelationshipDefCol.Add(relDef);
            Assert.AreEqual(relDef, childClassDef.GetRelationship("rel"));
        }