示例#1
0
        /// <summary>
        /// Saves the mapping property association.
        /// </summary>
        /// <param name="association">The association.</param>
        /// <returns></returns>
        public static int SaveMappingPropertyAssociation(MappingPropertyAssociation association)
        {
            try
            {
                if (association.IsValid)
                {
                    association.Id = DataAccessProvider.Instance().SaveMappingPropertyAssociation(association);
                }
                else
                {
                    // Entity is not valid
                    throw new InValidBusinessObjectException(association);
                }
            }
            catch (Exception ex)
            {
                if (
                    Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicy.HandleException(ex,
                                                                                                            "Business Logic"))
                {
                    throw;
                }
            }

            // Done
            return(association.Id);
        }
示例#2
0
        private MappingPropertyAssociation PopulateNewMappingPropertyAssocation()
        {
            MappingPropertyAssociation propertyAssociation = new MappingPropertyAssociation();

            //setup the class association which defines the source and destination types the source and destination properties will belong to
            propertyAssociation.MappingClassAssociation    = PopulateNewMappingClassAssocation();
            propertyAssociation.MappingClassAssociation.Id = MappingController.SaveMappingClassAssociation(propertyAssociation.MappingClassAssociation);
            propertyAssociation.MappingClassAssociationId  = propertyAssociation.MappingClassAssociation.Id;

            propertyAssociation.DestinationProperty      = "RouteCode";
            propertyAssociation.SourceProperty           = "RouteCode";
            propertyAssociation.LookupTableName          = "Discovery_Route";
            propertyAssociation.LookUpTableDisplayColumn = "Description";

            return(propertyAssociation);
        }
示例#3
0
        /// <summary>
        /// Gets the mapping property association.
        /// </summary>
        /// <param name="mappingClassAssociationId">The mapping class association id.</param>
        /// <returns></returns>
        public static MappingPropertyAssociation GetMappingPropertyAssociation(int mappingClassAssociationId)
        {
            MappingPropertyAssociation mappingPropertyAssociation = new MappingPropertyAssociation();

            try
            {
                mappingPropertyAssociation =
                    CBO <MappingPropertyAssociation> .FillObject(
                        DataAccessProvider.Instance().GetMappingPropertyAssociation(mappingClassAssociationId));
            }
            catch (Exception ex)
            {
                if (
                    Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicy.HandleException(ex,
                                                                                                            "Business Logic"))
                {
                    throw;
                }
            }
            return(mappingPropertyAssociation);
        }
示例#4
0
        /// <summary>
        /// A method 'Map' defined in 'Mapper' class with 6 arguments passing in
        /// </summary>
        /// <param name="sourceObject">The source object.</param>
        /// <param name="destinationObject">The destination object.</param>
        /// <param name="sourceSystem">The source system.</param>
        /// <param name="destinationSystem">The destination system.</param>
        /// <param name="advancedMappingMethod">The advanced mapping method.</param>
        /// <param name="propertiesToIgnore">The properties to ignore.</param>
        public static void Map(
            PersistableBusinessObject sourceObject,
            PersistableBusinessObject destinationObject,
            string sourceSystem,
            string destinationSystem,
            AdvancedMapping advancedMappingMethod,
            string[] propertiesToIgnore)
        {
            if (destinationObject != null)
            {
                Type sourceObjectType      = sourceObject.GetType();
                Type destinationObjectType = destinationObject.GetType();

                // Source property value
                object sourcePropertyValue = null;

                // Source property info
                PropertyInfo sourcePropertyInfo = null;

                // Get a mapping class association if we have one
                MappingClassAssociation mappingClassAssociation = MappingController.GetMappingClassAssociationByTypes(
                    sourceObjectType,
                    destinationObjectType);

                // Get the properties for the above class association
                List <MappingPropertyAssociation> mappingPropertyAssociations = null;
                // If we have a mapping association see if there are any properties
                if (null != mappingClassAssociation)
                {
                    mappingPropertyAssociations = MappingController.GetMappingPropertyAssociationsByClassAssociationId(
                        mappingClassAssociation.Id);
                }

                // Loop through the properties of the destination object
                foreach (PropertyInfo destinationPropertyInfo in destinationObjectType.GetProperties())
                {
                    // Only continue if we can write the destination property
                    if (destinationPropertyInfo.CanWrite)
                    {
                        // See if we have properties to ignore
                        if (null != propertiesToIgnore)
                        {
                            // See if we can find the property
                            if (null != Array.Find <string>(propertiesToIgnore,
                                                            delegate(string propertyToIgnore)
                            {
                                // Do we ignore this property
                                return(destinationPropertyInfo.Name == propertyToIgnore);
                            }))
                            {
                                // Ignore this property
                                continue;
                            }
                        }

                        // The mapping if one exists
                        BusinessObjects.Mapping valueMapping = null;

                        // The property association we've found, if any
                        MappingPropertyAssociation mappingPropertyAssociation = null;

                        // Is it a mappable property?
                        bool mappable = (mappingPropertyAssociations != null &&
                                         null != (mappingPropertyAssociation = mappingPropertyAssociations.Find(
                                                      delegate(MappingPropertyAssociation propertyAssociation)
                        {
                            return(propertyAssociation.DestinationProperty == destinationPropertyInfo.Name);
                        })));

                        // If we're mappable, we need to see if the source object has a value that we can use for the lookup
                        if (mappable)
                        {
                            // Get the source property with the same name as the mapping source property
                            sourcePropertyInfo = sourceObjectType.GetProperty(mappingPropertyAssociation.SourceProperty);

                            //if the destination object has a writeable property with the same name, and their types are the same then set to cloned value
                            if (sourcePropertyInfo != null &&
                                sourcePropertyInfo.PropertyType.Equals(destinationPropertyInfo.PropertyType) &&
                                sourcePropertyInfo.CanRead)
                            {
                                // Get the source value
                                sourcePropertyValue = sourcePropertyInfo.GetValue(sourceObject, null);
                            }
                            else
                            {
                                mappable = false;
                            }
                        }

                        // See if this property is mappable and mapped
                        if (mappable && null != (valueMapping = MappingController.GetMapping(
                                                     sourceObjectType,
                                                     destinationObjectType,
                                                     sourceSystem,
                                                     destinationSystem,
                                                     mappingPropertyAssociation.SourceProperty,
                                                     mappingPropertyAssociation.DestinationProperty,
                                                     sourcePropertyValue.ToString())))
                        {
                            // Set the destination properties value to the mapped value
                            destinationPropertyInfo.SetValue(destinationObject, valueMapping.DestinationValue, null);
                        }
                        else
                        {
                            // Get the source property with the same name as the current destination property
                            sourcePropertyInfo = sourceObjectType.GetProperty(destinationPropertyInfo.Name);

                            //if the destination object has a writeable property with the same name, and their types are the same then set to cloned value
                            if (sourcePropertyInfo != null &&
                                sourcePropertyInfo.PropertyType.Equals(destinationPropertyInfo.PropertyType) &&
                                sourcePropertyInfo.CanRead)
                            {
                                // Get the value of the source property
                                sourcePropertyValue = sourcePropertyInfo.GetValue(sourceObject, null);

                                // Set the destination properties value to a clone of the source value
                                destinationPropertyInfo.SetValue(destinationObject, DeepClone(sourcePropertyValue), null);
                            }
                            else
                            {
                                // Set the destination properties value to null
                                //destinationPropertyInfo.SetValue(destinationObject, Discovery.Utility.Null.SetNull(destinationPropertyInfo), null);
                            }
                        }
                    }
                }

                //call a delegate to perform futher specific mapping
                if (advancedMappingMethod != null)
                {
                    advancedMappingMethod(sourceObject, destinationObject, sourceSystem, destinationSystem);
                }
            }
            else
            {
                throw new MappingException("The Destination object is null.");
            }
        }