Exemple #1
0
        public void GetAllSourceMappingSystems()
        {
            using (TransactionScope scope = new TransactionScope())
            {
                if (MappingController.DeleteAllMappings())
                {
                    if (MappingController.DeleteAllMappingSystems())
                    {
                        MappingSystem mappingSystem = PopulateNewSourceMappingSystem();
                        mappingSystem.Id = MappingController.SaveMappingSystem(mappingSystem);
                        if (mappingSystem.Id != -1)
                        {
                            //retrieve all mapping Systems and the one we saved should return at least
                            List <MappingSystem> mappingSystems = MappingController.GetMappingSourceSystems();

                            //so the count should be >0
                            Assert.IsTrue(mappingSystems.Count > 0);
                            //check for our new id
                            Assert.IsTrue(mappingSystems.Find(delegate(MappingSystem currentItem)
                            {
                                return(currentItem.Id == mappingSystem.Id);
                            }) != null);
                        }
                    }
                }
            }
        }
Exemple #2
0
		private void InitSpecialMember(MetaDataMember mm)
		{
			// Can only have one auto gen member that is also an identity member,
			// except if that member is a computed column (since they are implicitly auto gen)
			if(mm.IsDbGenerated && mm.IsPrimaryKey && string.IsNullOrEmpty(mm.Expression))
			{
				if(this.dbGeneratedIdentity != null)
					throw Error.TwoMembersMarkedAsPrimaryKeyAndDBGenerated(mm.Member, this.dbGeneratedIdentity.Member);
				this.dbGeneratedIdentity = mm;
			}
			if(mm.IsPrimaryKey && !MappingSystem.IsSupportedIdentityType(mm.Type))
			{
				throw Error.IdentityClrTypeNotSupported(mm.DeclaringType, mm.Name, mm.Type);
			}
			if(mm.IsVersion)
			{
				if(this.version != null)
					throw Error.TwoMembersMarkedAsRowVersion(mm.Member, this.version.Member);
				this.version = mm;
			}
			if(mm.IsDiscriminator)
			{
				if(this.discriminator != null)
					throw Error.TwoMembersMarkedAsInheritanceDiscriminator(mm.Member, this.discriminator.Member);
				this.discriminator = mm;
			}
		}
Exemple #3
0
 private void InitSpecialMember(MetaDataMember mm)
 {
     if ((mm.IsDbGenerated && mm.IsPrimaryKey) && string.IsNullOrEmpty(mm.Expression))
     {
         if (this.dbGeneratedIdentity != null)
         {
             throw Mapping.Error.TwoMembersMarkedAsPrimaryKeyAndDBGenerated(mm.Member, this.dbGeneratedIdentity.Member);
         }
         this.dbGeneratedIdentity = mm;
     }
     if (mm.IsPrimaryKey && !MappingSystem.IsSupportedIdentityType(mm.Type))
     {
         throw Mapping.Error.IdentityClrTypeNotSupported(mm.DeclaringType, mm.Name, mm.Type);
     }
     if (mm.IsVersion)
     {
         if (this.version != null)
         {
             throw Mapping.Error.TwoMembersMarkedAsRowVersion(mm.Member, this.version.Member);
         }
         this.version = mm;
     }
     if (mm.IsDiscriminator)
     {
         if (this.discriminator != null)
         {
             throw Mapping.Error.TwoMembersMarkedAsInheritanceDiscriminator(mm.Member, this.discriminator.Member);
         }
         this.discriminator = mm;
     }
 }
Exemple #4
0
        /// <summary>
        /// Saves the mapping system.
        /// </summary>
        /// <param name="system">The system.</param>
        public static int SaveMappingSystem(MappingSystem system)
        {
            try
            {
                if (system.IsValid)
                {
                    // Save entity
                    system.Id = DataAccessProvider.Instance().SaveMappingSystem(system);
                }
                else
                {
                    // Entity is not valid
                    throw new InValidBusinessObjectException(system);
                }
            }
            catch (Exception ex)
            {
                if (
                    Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicy.HandleException(ex,
                                                                                                            "Business Logic"))
                {
                    throw;
                }
            }

            // Done
            return(system.Id);
        }
Exemple #5
0
        private MappingSystem PopulateNewSourceMappingSystem()
        {
            MappingSystem mappingSystem = new MappingSystem();

            mappingSystem.IsDestination = false;
            mappingSystem.IsSource      = true;
            mappingSystem.Name          = "RHG";
            return(mappingSystem);
        }
Exemple #6
0
 public void SaveDestinationMappingSystemConstraint()
 {
     using (TransactionScope scope = new TransactionScope())
     {
         MappingController.DeleteAllMappings();
         MappingController.DeleteAllMappingSystems();
         MappingSystem system = PopulateNewDestinationMappingSystem();
         if (MappingController.SaveMappingSystem(system) != -1)
         {
             MappingController.SaveMappingSystem(system);
         }
     }
 }
Exemple #7
0
        [ResourceConsumption(ResourceScope.Assembly | ResourceScope.Machine)]      // InitInheritedType method call.
        public MappedRootType(MappedMetaModel model, MappedTable table, TypeMapping typeMapping, Type type)
            : base(model, table, typeMapping, type, null)
        {
            if (typeMapping == null)
            {
                throw Error.ArgumentNull("typeMapping");
            }

            if (typeMapping.InheritanceCode != null || typeMapping.DerivedTypes.Count > 0)
            {
                if (this.Discriminator == null)
                {
                    throw Error.NoDiscriminatorFound(type.Name);
                }
                this.hasInheritance = true;
                if (!MappingSystem.IsSupportedDiscriminatorType(this.Discriminator.Type))
                {
                    throw Error.DiscriminatorClrTypeNotSupported(this.Discriminator.DeclaringType.Name, this.Discriminator.Name, this.Discriminator.Type);
                }
                this.derivedTypes     = new Dictionary <Type, MetaType>();
                this.inheritanceCodes = new Dictionary <object, MetaType>();
                this.InitInheritedType(typeMapping, this);
            }

            if (this.inheritanceDefault == null && (this.InheritanceCode != null || this.inheritanceCodes != null && this.inheritanceCodes.Count > 0))
            {
                throw Error.InheritanceHierarchyDoesNotDefineDefault(type);
            }

            if (this.derivedTypes != null)
            {
                this.inheritanceTypes = this.derivedTypes.Values.ToList().AsReadOnly();
            }
            else
            {
                this.inheritanceTypes = new MetaType[] { this }.ToList().AsReadOnly();
            }

            this.Validate();
        }
Exemple #8
0
        internal AttributedRootType(AttributedMetaModel model, AttributedMetaTable table, Type type)
            : base(model, table, type, null)
        {
            // check for inheritance and create all other types
            InheritanceMappingAttribute[] inheritanceInfo = (InheritanceMappingAttribute[])type.GetCustomAttributes(typeof(InheritanceMappingAttribute), true);
            if (inheritanceInfo.Length > 0)
            {
                if (this.Discriminator == null)
                {
                    throw Error.NoDiscriminatorFound(type);
                }
                if (!MappingSystem.IsSupportedDiscriminatorType(this.Discriminator.Type))
                {
                    throw Error.DiscriminatorClrTypeNotSupported(this.Discriminator.DeclaringType.Name, this.Discriminator.Name, this.Discriminator.Type);
                }
                this.types = new Dictionary <Type, MetaType>();
                this.types.Add(type, this);                 // add self
                this.codeMap = new Dictionary <object, MetaType>();

                // initialize inheritance types
                foreach (InheritanceMappingAttribute attr in inheritanceInfo)
                {
                    if (!type.IsAssignableFrom(attr.Type))
                    {
                        throw Error.InheritanceTypeDoesNotDeriveFromRoot(attr.Type, type);
                    }
                    if (attr.Type.IsAbstract)
                    {
                        throw Error.AbstractClassAssignInheritanceDiscriminator(attr.Type);
                    }
                    AttributedMetaType mt = this.CreateInheritedType(type, attr.Type);
                    if (attr.Code == null)
                    {
                        throw Error.InheritanceCodeMayNotBeNull();
                    }
                    if (mt.inheritanceCode != null)
                    {
                        throw Error.InheritanceTypeHasMultipleDiscriminators(attr.Type);
                    }
                    object codeValue = DBConvert.ChangeType(attr.Code, this.Discriminator.Type);
                    foreach (object d in codeMap.Keys)
                    {
                        // if the keys are equal, or if they are both strings containing only spaces
                        // they are considered equal
                        if ((codeValue.GetType() == typeof(string) && ((string)codeValue).Trim().Length == 0 &&
                             d.GetType() == typeof(string) && ((string)d).Trim().Length == 0) ||
                            object.Equals(d, codeValue))
                        {
                            throw Error.InheritanceCodeUsedForMultipleTypes(codeValue);
                        }
                    }
                    mt.inheritanceCode = codeValue;
                    this.codeMap.Add(codeValue, mt);
                    if (attr.IsDefault)
                    {
                        if (this.inheritanceDefault != null)
                        {
                            throw Error.InheritanceTypeHasMultipleDefaults(type);
                        }
                        this.inheritanceDefault = mt;
                    }
                }

                if (this.inheritanceDefault == null)
                {
                    throw Error.InheritanceHierarchyDoesNotDefineDefault(type);
                }
            }

            if (this.types != null)
            {
                this.inheritanceTypes = this.types.Values.ToList().AsReadOnly();
            }
            else
            {
                this.inheritanceTypes = new MetaType[] { this }.ToList().AsReadOnly();
            }
            this.Validate();
        }
Exemple #9
0
        // Methods
        internal AttributedRootType(AttributedMetaModel model, AttributedMetaTable table, Type type)
            : base(model, table, type, null)
        {
            this.model = model;
            var customAttributes = model.AttributeProvider.GetInheritanceMappingAttribute(type);

            //(InheritanceMappingAttribute[])type.GetCustomAttributes(typeof(InheritanceMappingAttribute), true);
            if (customAttributes != null && customAttributes.Length > 0)
            {
                if (this.Discriminator == null)
                {
                    throw Mapping.Error.NoDiscriminatorFound(type);
                }
                if (!MappingSystem.IsSupportedDiscriminatorType(this.Discriminator.Type))
                {
                    throw Mapping.Error.DiscriminatorClrTypeNotSupported(this.Discriminator.DeclaringType.Name, this.Discriminator.Name, this.Discriminator.Type);
                }
                this.types = new Dictionary <Type, MetaType>();
                this.types.Add(type, this);
                this.codeMap = new Dictionary <object, MetaType>();
                foreach (InheritanceMappingAttribute attribute in customAttributes)
                {
                    if (!type.IsAssignableFrom(attribute.Type))
                    {
                        throw Mapping.Error.InheritanceTypeDoesNotDeriveFromRoot(attribute.Type, type);
                    }
                    if (attribute.Type.IsAbstract)
                    {
                        throw Mapping.Error.AbstractClassAssignInheritanceDiscriminator(attribute.Type);
                    }
                    AttributedMetaType type2 = this.CreateInheritedType(type, attribute.Type);
                    if (attribute.Code == null)
                    {
                        throw Mapping.Error.InheritanceCodeMayNotBeNull();
                    }
                    if (type2.inheritanceCode != null)
                    {
                        throw Mapping.Error.InheritanceTypeHasMultipleDiscriminators(attribute.Type);
                    }
                    object objB = DBConvert.ChangeType(attribute.Code, this.Discriminator.Type);
                    foreach (object obj3 in this.codeMap.Keys)
                    {
                        if ((((objB.GetType() == typeof(string)) && (((string)objB).Trim().Length == 0)) && ((obj3.GetType() == typeof(string)) && (((string)obj3).Trim().Length == 0))) || object.Equals(obj3, objB))
                        {
                            throw Mapping.Error.InheritanceCodeUsedForMultipleTypes(objB);
                        }
                    }
                    type2.inheritanceCode = objB;
                    this.codeMap.Add(objB, type2);
                    if (attribute.IsDefault)
                    {
                        if (this.inheritanceDefault != null)
                        {
                            throw Mapping.Error.InheritanceTypeHasMultipleDefaults(type);
                        }
                        this.inheritanceDefault = type2;
                    }
                }
                if (this.inheritanceDefault == null)
                {
                    throw Mapping.Error.InheritanceHierarchyDoesNotDefineDefault(type);
                }
            }
            if (this.types != null)
            {
                this.inheritanceTypes = this.types.Values.ToList <MetaType>().AsReadOnly();
            }
            else
            {
                this.inheritanceTypes = new MetaType[] { this }.ToList <MetaType>().AsReadOnly();
            }
            this.Validate();
        }
Exemple #10
0
        internal AttributedRootType(AttributedMetaModel model, AttributedMetaTable table, Type type)
            : base(model, table, type, null)
        {
            // check for inheritance and create all other types
            var inheritanceAttributes = GetInheritanceMappingAttributes(type, model);

            if (inheritanceAttributes.Count > 0)
            {
                if (Discriminator == null)
                {
                    throw Error.NoDiscriminatorFound(type);
                }
                if (!MappingSystem.IsSupportedDiscriminatorType(Discriminator.Type))
                {
                    throw Error.DiscriminatorClrTypeNotSupported(
                              Discriminator.DeclaringType.Name,
                              Discriminator.Name,
                              Discriminator.Type);
                }

                types = new Dictionary <Type, MetaType>();
                types.Add(type, this);                 // add self
                var codeMap = new Dictionary <object, MetaType>();

                // initialize inheritance types
                foreach (var inheritanceAttribute in inheritanceAttributes)
                {
                    if (!type.IsAssignableFrom(inheritanceAttribute.Type))
                    {
                        throw Error.InheritanceTypeDoesNotDeriveFromRoot(inheritanceAttribute.Type, type);
                    }
                    if (inheritanceAttribute.Type.IsAbstract)
                    {
                        throw Error.AbstractClassAssignInheritanceDiscriminator(inheritanceAttribute.Type);
                    }

                    var inheritedType = CreateInheritedType(type, inheritanceAttribute.Type);
                    if (inheritanceAttribute.Code == null)
                    {
                        throw Error.InheritanceCodeMayNotBeNull();
                    }
                    if (inheritedType.inheritanceCode != null)
                    {
                        throw Error.InheritanceTypeHasMultipleDiscriminators(inheritanceAttribute.Type);
                    }

                    var codeValue = DBConvert.ChangeType(inheritanceAttribute.Code, Discriminator.Type);
                    foreach (var codeMapKey in codeMap.Keys)
                    {
                        // if the keys are equal, or if they are both strings containing only spaces
                        // they are considered equal
                        if ((codeValue is string &&
                             ((string)codeValue).Trim().Length == 0 &&
                             codeMapKey is string &&
                             ((string)codeMapKey).Trim().Length == 0) ||
                            Equals(codeMapKey, codeValue))
                        {
                            throw Error.InheritanceCodeUsedForMultipleTypes(codeValue);
                        }
                    }
                    inheritedType.inheritanceCode = codeValue;
                    codeMap.Add(codeValue, inheritedType);

                    if (inheritanceAttribute.IsDefault)
                    {
                        if (inheritanceDefault != null)
                        {
                            throw Error.InheritanceTypeHasMultipleDefaults(type);
                        }
                        inheritanceDefault = inheritedType;
                    }
                }

                if (inheritanceDefault == null)
                {
                    throw Error.InheritanceHierarchyDoesNotDefineDefault(type);
                }
            }

            inheritanceTypes = types == null ?
                               new MetaType[]
            {
                this
            }
            .ToList()
            .AsReadOnly() :
            types.Values.ToList().AsReadOnly();

            Validate();
        }