Пример #1
0
        public SagaModelMapper(IEnumerable <Type> typesToScan)
        {
            Mapper = new ConventionModelMapper();

            _sagaEntities =
                typesToScan.Where(t => typeof(IContainSagaData).IsAssignableFrom(t) && !t.IsInterface);

            _entityTypes = GetTypesThatShouldBeAutoMapped(_sagaEntities, typesToScan);

            Mapper.IsTablePerClass((type, b) => false);
            Mapper.IsTablePerConcreteClass((type, b) => _sagaEntities.Contains(type));
            Mapper.IsTablePerClassHierarchy((type, b) => false);
            Mapper.IsEntity((type, mapped) => _entityTypes.Contains(type));
            Mapper.IsArray((info, b) => false);
            Mapper.IsBag((info, b) =>
            {
                var memberType = info.GetPropertyOrFieldType();
                return(typeof(IEnumerable).IsAssignableFrom(memberType) &&
                       !(memberType == typeof(string) || memberType == typeof(byte[]) || memberType.IsArray));
            });
            Mapper.IsPersistentProperty((info, b) => !HasAttribute <RowVersionAttribute>(info));

            Mapper.BeforeMapClass         += ApplyClassConvention;
            Mapper.BeforeMapUnionSubclass += ApplySubClassConvention;
            Mapper.BeforeMapProperty      += ApplyPropertyConvention;
            Mapper.BeforeMapBag           += ApplyBagConvention;
            Mapper.BeforeMapManyToOne     += ApplyManyToOneConvention;
        }
Пример #2
0
        public void Test_AbstractIntermediateSubclasses()
        {
            var mapper = new ConventionModelMapper();

            mapper.IsTablePerClass((type, declared) => false);
            mapper.IsTablePerClassHierarchy((type, declared) => true);
            var mappings = mapper.CompileMappingFor(new[] { typeof(Animal), typeof(Mammal), typeof(Dog) });

            Assert.AreEqual(1, mappings.RootClasses.Length, "Mapping should only have Animal as root class");
            Assert.AreEqual(2, mappings.SubClasses.Length, "Subclasses not mapped as expected");
            var animalMapping = mappings.RootClasses.SingleOrDefault(s => s.Name == nameof(Animal));

            Assert.IsNotNull(animalMapping, "Unable to find mapping for animal class");
            Assert.AreEqual(nameof(Animal.Id), animalMapping.Id.name, "Identifier not mapped as expected");
            CollectionAssert.AreEquivalent(new [] { nameof(Animal.Description), nameof(Animal.Sequence) }, animalMapping.Properties.Select(p => p.Name));
            var mammalMapping = mappings.SubClasses.SingleOrDefault(s => s.Name == nameof(Mammal));

            Assert.IsNotNull(mammalMapping, "Unable to find mapping for Mammal class");
            Assert.AreEqual(nameof(Animal), mammalMapping.extends, "Mammal mapping does not extend Animal as expected");
            CollectionAssert.AreEquivalent(new[] { nameof(Mammal.Pregnant), nameof(Mammal.BirthDate) }, mammalMapping.Properties.Select(p => p.Name));
            var dogMapping = mappings.SubClasses.SingleOrDefault(s => s.Name == nameof(Dog));

            Assert.IsNotNull(dogMapping, "Unable to find mapping for Dog class");
            Assert.AreEqual(nameof(Mammal), dogMapping.extends, "Dog mapping does not extend Mammal as expected");
            CollectionAssert.IsEmpty(dogMapping.Properties);
        }
Пример #3
0
        public void Test_Longchain3_ConventionMappingMappings_PerClass()
        {
            // Mapped -> Unmapped -> Mapped -> Root
            var mapper = new ConventionModelMapper();

            mapper.IsTablePerClass((type, declared) => true);
            mapper.IsTablePerClassHierarchy((type, declared) => false);
            var mappings = mapper.CompileMappingFor(new[] { typeof(Longchain3.MappedRoot), typeof(Longchain3.MappedExtension), typeof(Longchain3.TopLevel) });

            Assert.AreEqual(1, mappings.RootClasses.Length, "Mapping should only have MappedRoot as root class");
            Assert.AreEqual(2, mappings.JoinedSubclasses.Length, "Subclasses not mapped as expected");
            var rootMapping = mappings.RootClasses.SingleOrDefault(s => s.Name == nameof(Longchain3.MappedRoot));

            Assert.IsNotNull(rootMapping, "Unable to find mapping for MappedRoot class");
            Assert.AreEqual(nameof(Longchain3.MappedRoot.Id), rootMapping.Id.name, "Identifier not mapped as expected");
            CollectionAssert.AreEquivalent(new[] { nameof(Longchain3.MappedRoot.BaseField) }, rootMapping.Properties.Select(p => p.Name));
            var mappedExtensionMapping = mappings.JoinedSubclasses.SingleOrDefault(s => s.Name == nameof(Longchain3.MappedExtension));

            Assert.IsNotNull(mappedExtensionMapping, "Unable to find mapping for MappedExtension class");
            Assert.AreEqual(nameof(Longchain3.MappedRoot), mappedExtensionMapping.extends, "MappedExtension extension not as expected");
            CollectionAssert.AreEquivalent(new[] { nameof(Longchain3.FirstUnmappedExtension.FirstUnmappedExtensionField), nameof(Longchain3.MappedExtension.MappedExtensionField) }, mappedExtensionMapping.Properties.Select(p => p.Name));
            var topLevelMapping = mappings.JoinedSubclasses.SingleOrDefault(s => s.Name == nameof(Longchain3.TopLevel));

            Assert.IsNotNull(topLevelMapping, "Unable to find mapping for TopLevel class");
            Assert.AreEqual(nameof(Longchain3.MappedExtension), topLevelMapping.extends, "TopLevel extension not as expected");
            CollectionAssert.AreEquivalent(new[] { nameof(Longchain3.SecondUnmappedExtension.SecondUnmappedExtensionField), nameof(Longchain3.TopLevel.TopLevelExtensionField) }, topLevelMapping.Properties.Select(p => p.Name));
        }
        SagaModelMapper(SagaMetadataCollection allMetadata, IEnumerable <Type> typesToScan, Func <Type, string> tableNamingConvention = null)
        {
            this.tableNamingConvention = tableNamingConvention ?? DefaultTableNameConvention;
            mapper = new ConventionModelMapper();

            this.typesToScan = typesToScan.ToList();

            sagaMetaModel = allMetadata;

            sagaEntities =
                this.typesToScan.Where(t => typeof(IContainSagaData).IsAssignableFrom(t) && !t.IsInterface).ToList();

            PopulateTypesThatShouldBeAutoMapped();

            mapper.IsTablePerClass((type, b) => false);
            mapper.IsTablePerConcreteClass((type, b) => sagaEntities.Contains(type));
            mapper.IsTablePerClassHierarchy((type, b) => false);
            mapper.IsEntity((type, mapped) => entityTypes.Contains(type));
            mapper.IsArray((info, b) => false);
            mapper.IsBag((info, b) =>
            {
                var memberType = info.GetPropertyOrFieldType();
                return(typeof(IEnumerable).IsAssignableFrom(memberType) &&
                       !(memberType == typeof(string) || memberType == typeof(byte[]) || memberType.IsArray));
            });
            mapper.IsPersistentProperty((info, b) => !HasAttribute <RowVersionAttribute>(info));
            mapper.BeforeMapClass         += ApplyClassConvention;
            mapper.BeforeMapUnionSubclass += ApplySubClassConvention;
            mapper.BeforeMapProperty      += ApplyPropertyConvention;
            mapper.BeforeMapBag           += ApplyBagConvention;
            mapper.BeforeMapManyToOne     += ApplyManyToOneConvention;
        }
Пример #5
0
        protected override HbmMapping GetMappings()
        {
            var mapper = new ConventionModelMapper();

            mapper.IsTablePerClass((type, declared) => false);
            mapper.IsTablePerClassHierarchy((type, declared) => true);
            var mappings = mapper.CompileMappingFor(new[] { typeof(Animal), typeof(Reptile), typeof(Mammal), typeof(Lizard), typeof(Dog), typeof(Cat) });

            return(mappings);
        }