Exemple #1
0
        public void ComponentAsId(MemberInfo idProperty, Action <IComponentAsIdMapper> mapper)
        {
            if (idProperty == null)
            {
                return;
            }
            if (!IsMemberSupportedByMappedContainer(idProperty))
            {
                throw new ArgumentOutOfRangeException("idProperty", "Can't use, as component id property, a property of another graph");
            }
            if (composedIdWasUsed)
            {
                throw new MappingException(string.Format("Ambiguous mapping of {0} id. A composed id was defined and you are trying to map the component {1}, of property '{2}', as id for {0}."
                                                         , Container.FullName, idProperty.GetPropertyOrFieldType().FullName, idProperty.Name));
            }
            if (simpleIdPropertyWasUsed)
            {
                throw new MappingException(string.Format("Ambiguous mapping of {0} id. An id property, with generator, was defined and you are trying to map the component {1}, of property '{2}', as id for {0}."
                                                         , Container.FullName, idProperty.GetPropertyOrFieldType().FullName, idProperty.Name));
            }
            var id = classMapping.Item as HbmCompositeId;

            if (id == null)
            {
                id = new HbmCompositeId();
                classMapping.Item = id;
            }
            mapper(new ComponentAsIdMapper(idProperty.GetPropertyOrFieldType(), idProperty, id, mapDoc));
            componentAsIdWasUsed = true;
        }
        private void BindComponent(System.Type reflectedClass, string path, HbmCompositeId idSchema)
        {
            if (idSchema.@class != null)
            {
                compositeId.ComponentClass = ClassForNameChecked(idSchema.@class, mappings, "component class not found: {0}");

                compositeId.IsEmbedded = false;
            }
            else if (reflectedClass != null)
            {
                compositeId.ComponentClass = reflectedClass;
                compositeId.IsEmbedded     = false;
            }
            else
            {
                // an "embedded" component (ids only)
                if (compositeId.Owner.HasPocoRepresentation)
                {
                    compositeId.ComponentClass = compositeId.Owner.MappedClass;
                    compositeId.IsEmbedded     = true;
                }
                else
                {
                    // if not - treat compositeid as a dynamic-component
                    compositeId.IsDynamic = true;
                }
            }

            foreach (object item in idSchema.Items ?? new object[0])
            {
                var keyManyToOneSchema = item as HbmKeyManyToOne;
                var keyPropertySchema  = item as HbmKeyProperty;

                if (keyManyToOneSchema != null)
                {
                    var manyToOne = new ManyToOne(compositeId.Table);

                    string propertyName = keyManyToOneSchema.name == null ? null : StringHelper.Qualify(path, keyManyToOneSchema.name);

                    BindManyToOne(keyManyToOneSchema, manyToOne, propertyName, false);

                    Property property = CreateProperty(manyToOne, keyManyToOneSchema.name, compositeId.ComponentClass,
                                                       keyManyToOneSchema);

                    compositeId.AddProperty(property);
                }
                else if (keyPropertySchema != null)
                {
                    var value = new SimpleValue(compositeId.Table);

                    string propertyName = keyPropertySchema.name == null ? null : StringHelper.Qualify(path, keyPropertySchema.name);

                    BindSimpleValue(keyPropertySchema, value, false, propertyName);

                    Property property = CreateProperty(value, keyPropertySchema.name, compositeId.ComponentClass, keyPropertySchema);

                    compositeId.AddProperty(property);
                }
            }
        }
 public ComponentAsIdMapper(System.Type componentType, MemberInfo declaringTypeMember, HbmCompositeId id, HbmMapping mapDoc)
 {
     this.id                = id;
     this.mapDoc            = mapDoc;
     id.@class              = componentType.GetShortClassName(mapDoc);
     accessorPropertyMapper = new AccessorPropertyMapper(declaringTypeMember.DeclaringType, declaringTypeMember.Name, x => id.access = x);
 }
        public void WhenPropertyUsedAsComposedIdAndPropertiesAndNaturalIdThenMapOnlyAsComposedId()
        {
            var mapper = new ModelMapper();

            mapper.Class <MyClass>(map =>
            {
                map.ComposedId(cm =>
                {
                    cm.Property(x => x.Code);
                    cm.ManyToOne(x => x.Relation);
                });
                map.NaturalId(nm =>
                {
                    nm.Property(x => x.Code);
                    nm.ManyToOne(x => x.Relation);
                });
                map.Property(x => x.Code);
                map.ManyToOne(x => x.Relation);
            }
                                   );
            HbmMapping     hbmMapping    = mapper.CompileMappingFor(new[] { typeof(MyClass) });
            HbmClass       hbmClass      = hbmMapping.RootClasses[0];
            HbmCompositeId hbmCompositId = hbmClass.CompositeId;

            hbmCompositId.Items.Should().Have.Count.EqualTo(2);
            hbmClass.naturalid.Should().Be.Null();
            hbmClass.Properties.Should().Be.Empty();
        }
Exemple #5
0
        public void WhenPropertyUsedAsComposedIdAndPropertiesAndNaturalIdThenMapOnlyAsComposedId()
        {
            var mapper = new ModelMapper();

            mapper.Class <MyClass>(map =>
            {
                map.ComposedId(cm =>
                {
                    cm.Property(x => x.Code);
                    cm.ManyToOne(x => x.Relation);
                });
                map.NaturalId(nm =>
                {
                    nm.Property(x => x.Code);
                    nm.ManyToOne(x => x.Relation);
                });
                map.Property(x => x.Code);
                map.ManyToOne(x => x.Relation);
            }
                                   );
            HbmMapping     hbmMapping    = mapper.CompileMappingFor(new[] { typeof(MyClass) });
            HbmClass       hbmClass      = hbmMapping.RootClasses[0];
            HbmCompositeId hbmCompositId = hbmClass.CompositeId;

            Assert.That(hbmCompositId.Items, Has.Length.EqualTo(2));
            Assert.That(hbmClass.naturalid, Is.Null);
            Assert.That(hbmClass.Properties, Is.Empty);
        }
Exemple #6
0
        public void WhenCreatedThenSetTheComponentClass()
        {
            var mapdoc    = new HbmMapping();
            var component = new HbmCompositeId();

            new ComponentAsIdMapper(typeof(PersonId), For <Person> .Property(x => x.Id), component, mapdoc);

            [email protected]().Contain("PersonId");
        }
        public void WhenCreatedThenSetTheComponentClass()
        {
            var mapdoc    = new HbmMapping();
            var component = new HbmCompositeId();

            new ComponentAsIdMapper(typeof(PersonId), For <Person> .Property(x => x.Id), component, mapdoc);

            Assert.That(component.@class, Is.StringContaining("PersonId"));
        }
        public void CallPropertyMapper()
        {
            var mapdoc      = new HbmMapping();
            var compositeId = new HbmCompositeId();
            var mapper      = new ComposedIdMapper(typeof(Person), compositeId, mapdoc);
            var called      = false;

            mapper.Property(For <Person> .Property(ts => ts.Email), x => called = true);

            called.Should().Be.True();
        }
Exemple #9
0
        public void CallMapManyToOneMapper()
        {
            var mapdoc      = new HbmMapping();
            var compositeId = new HbmCompositeId();
            var mapper      = new ComponentAsIdMapper(typeof(PersonId), For <Person> .Property(x => x.Id), compositeId, mapdoc);
            var called      = false;

            mapper.ManyToOne(For <PersonId> .Property(ts => ts.User), x => called = true);

            called.Should().Be.True();
        }
Exemple #10
0
        public void CallMapManyToOneMapper()
        {
            var mapdoc      = new HbmMapping();
            var compositeId = new HbmCompositeId();
            var mapper      = new ComposedIdMapper(typeof(Person), compositeId, mapdoc);
            var called      = false;

            mapper.ManyToOne(For <Person> .Property(ts => ts.User), x => called = true);

            Assert.That(called, Is.True);
        }
        public void CallPropertyMapper()
        {
            var mapdoc      = new HbmMapping();
            var compositeId = new HbmCompositeId();
            var mapper      = new ComponentAsIdMapper(typeof(PersonId), For <Person> .Property(x => x.Id), compositeId, mapdoc);
            var called      = false;

            mapper.Property(For <PersonId> .Property(ts => ts.Email), x => called = true);

            Assert.That(called, Is.True);
        }
        public void BindCompositeId(HbmCompositeId idSchema, PersistentClass rootClass)
        {
            if (idSchema == null)
            {
                return;
            }

            compositeId          = new Component(rootClass);
            rootClass.Identifier = compositeId;

            if (idSchema.name == null)
            {
                BindComponent(null, "id", idSchema);
                rootClass.HasEmbeddedIdentifier = compositeId.IsEmbedded;
            }
            else
            {
                System.Type reflectedClass = GetPropertyType(rootClass.MappedClass,
                                                             idSchema.name, idSchema);

                BindComponent(reflectedClass, idSchema.name, idSchema);

                Mapping.Property prop = new Mapping.Property(compositeId);
                BindProperty(prop, idSchema);
                rootClass.IdentifierProperty = prop;
            }

            compositeId.Table.SetIdentifierValue(compositeId);
            compositeId.NullValue = GetXmlEnumAttribute(idSchema.unsavedvalue);

            System.Type compIdClass = compositeId.ComponentClass;

            //<Simon date='26.04.2010'>
            if (compositeId.IsDynamic)
            {
                return;
            }
            //</Simon>
            if (!ReflectHelper.OverridesEquals(compIdClass))
            {
                throw new MappingException(
                          "composite-id class must override Equals(): " + compIdClass.FullName
                          );
            }

            if (!ReflectHelper.OverridesGetHashCode(compIdClass))
            {
                throw new MappingException(
                          "composite-id class must override GetHashCode(): " + compIdClass.FullName
                          );
            }
            // Serializability check not ported
        }
        public void CanMapProperty()
        {
            var mapdoc      = new HbmMapping();
            var compositeId = new HbmCompositeId();
            var mapper      = new ComponentAsIdMapper(typeof(PersonId), For <Person> .Property(x => x.Id), compositeId, mapdoc);

            mapper.Property(For <PersonId> .Property(ts => ts.Email), x => { });

            Assert.That(compositeId.Items, Has.Length.EqualTo(1));
            Assert.That(compositeId.Items.First(), Is.TypeOf <HbmKeyProperty>());
            Assert.That(compositeId.Items.OfType <HbmKeyProperty>().First().Name, Is.EqualTo("Email"));
        }
Exemple #14
0
        public void CanMapManyToOne()
        {
            var mapdoc      = new HbmMapping();
            var compositeId = new HbmCompositeId();
            var mapper      = new ComponentAsIdMapper(typeof(PersonId), For <Person> .Property(x => x.Id), compositeId, mapdoc);

            mapper.ManyToOne(For <PersonId> .Property(ts => ts.User), x => { });

            compositeId.Items.Should().Have.Count.EqualTo(1);
            compositeId.Items.First().Should().Be.OfType <HbmKeyManyToOne>();
            compositeId.Items.OfType <HbmKeyManyToOne>().First().Name.Should().Be.EqualTo("User");
        }
        public void CanMapProperty()
        {
            var mapdoc      = new HbmMapping();
            var compositeId = new HbmCompositeId();
            var mapper      = new ComposedIdMapper(typeof(Person), compositeId, mapdoc);

            mapper.Property(For <Person> .Property(ts => ts.Email), x => { });

            compositeId.Items.Should().Have.Count.EqualTo(1);
            compositeId.Items.First().Should().Be.OfType <HbmKeyProperty>();
            compositeId.Items.OfType <HbmKeyProperty>().First().Name.Should().Be.EqualTo("Email");
        }
Exemple #16
0
        public void CanMapManyToOne()
        {
            var mapdoc      = new HbmMapping();
            var compositeId = new HbmCompositeId();
            var mapper      = new ComposedIdMapper(typeof(Person), compositeId, mapdoc);

            mapper.ManyToOne(For <Person> .Property(ts => ts.User), x => { });

            Assert.That(compositeId.Items, Has.Length.EqualTo(1));
            Assert.That(compositeId.Items.First(), Is.TypeOf <HbmKeyManyToOne>());
            Assert.That(compositeId.Items.OfType <HbmKeyManyToOne>().First().Name, Is.EqualTo("User"));
        }
        private void BindProperty(Property property, HbmCompositeId idSchema)
        {
            property.Name = idSchema.name;

            if (property.Value.Type == null)
            {
                throw new MappingException("could not determine a property type for: " + property.Name);
            }

            property.PropertyAccessorName = idSchema.access ?? mappings.DefaultAccess;
            property.Cascade            = mappings.DefaultCascade;
            property.IsUpdateable       = true;
            property.IsInsertable       = true;
            property.IsOptimisticLocked = true;
            property.Generation         = PropertyGeneration.Never;
            property.MetaAttributes     = new Dictionary <string, MetaAttribute>();

            property.LogMapped(log);
        }
        private System.Type GetPropertyType(System.Type containingType, string propertyName,
                                            HbmCompositeId idSchema)
        {
            if (idSchema.@class != null)
            {
                return(ClassForNameChecked(idSchema.@class, mappings, "could not find class: {0}"));
            }

            else if (containingType == null)
            {
                return(null);
            }

            else
            {
                string access = idSchema.access ?? mappings.DefaultAccess;
                return(ReflectHelper.ReflectedPropertyClass(containingType, propertyName, access));
            }
        }
Exemple #19
0
        public void ComposedId(Action <IComposedIdMapper> idPropertiesMapping)
        {
            if (componentAsIdWasUsed)
            {
                throw new MappingException(string.Format("Ambiguous mapping of {0} id. A Component as id was used and you are trying to map an id composed by various properties of {0}.", Container.FullName));
            }
            if (simpleIdPropertyWasUsed)
            {
                throw new MappingException(string.Format("Ambiguous mapping of {0} id. An id property, with generator, was defined and you are trying to map an id composed by various properties of {0}.", Container.FullName));
            }

            var id = classMapping.Item as HbmCompositeId;

            if (id == null)
            {
                id = new HbmCompositeId();
                classMapping.Item = id;
            }
            idPropertiesMapping(new ComposedIdMapper(Container, id, mapDoc));
            composedIdWasUsed = true;
        }
        public void BindCompositeId(HbmCompositeId idSchema, PersistentClass rootClass)
        {
            if (idSchema == null)
            {
                return;
            }

            compositeId       = new Component(rootClass);
            compositeId.IsKey = true;

            rootClass.Identifier = compositeId;

            if (idSchema.name == null)
            {
                BindComponent(null, "id", idSchema);
                rootClass.HasEmbeddedIdentifier = compositeId.IsEmbedded;
            }
            else
            {
                System.Type reflectedClass = GetPropertyType(rootClass.MappedClass, idSchema.name, idSchema);

                BindComponent(reflectedClass, idSchema.name, idSchema);

                var prop = new Property(compositeId);
                BindProperty(prop, idSchema);
                rootClass.IdentifierProperty = prop;
            }

            compositeId.Table.SetIdentifierValue(compositeId);
            compositeId.NullValue = idSchema.unsavedvalue.ToNullValue();

            if (!compositeId.IsDynamic)
            {
                CheckEqualsAndGetHashCodeOverride();
            }
            // Serializability check not ported
        }
Exemple #21
0
 public ComposedIdMapper(System.Type container, HbmCompositeId id, HbmMapping mapDoc)
 {
     this.container = container;
     this.id        = id;
     this.mapDoc    = mapDoc;
 }