Esempio n. 1
0
        // Methods
        public static void AddMap(Type objType, PropertyMap map)
        {
            PropertyMapCollection maps = null;

            if (!s_maps.TryGetValue(objType, out maps))
            {
                lock (s_SyncObj)
                {
                    if (!s_maps.TryGetValue(objType, out maps))
                    {
                        maps = new PropertyMapCollection();
                        if (!maps.Contains(map.Key))
                        {
                            maps.Add(map);
                        }
                        s_maps.Add(objType, maps);
                    }
                }
            }
            else if (!maps.Contains(map.Key))
            {
                lock (s_SyncObj)
                {
                    if (!maps.Contains(map.Key))
                    {
                        maps.Add(map);
                    }
                }
            }
        }
Esempio n. 2
0
        public static PropertyMapCollection GetMaps(Type objType)
        {
            PropertyMapCollection maps = null;

            s_maps.TryGetValue(objType, out maps);
            return(maps);
        }
Esempio n. 3
0
        public static PropertyMapCollection GetPropertyMaps(Type objType)
        {
            PropertyMapCollection pmc = PropertyMapCache.GetMaps(objType);

            if (pmc == null)
            {
                lock (s_SyncObj)
                {
                    pmc = PropertyMapCache.GetMaps(objType);
                    if (pmc == null)
                    {
                        pmc = BuildPropertyMaps(objType);
                    }
                }
            }
            return(pmc);
        }
Esempio n. 4
0
        // Methods
        public override MapResult <TEntity> Map <TEntity>(IRowDataSource dataSource)
        {
            Type    entityType              = typeof(TEntity);
            TEntity entity                  = (TEntity)Activator.CreateInstance(entityType);
            PropertyMapCollection pmc       = MapBuilder.GetPropertyMaps(entityType);
            MapResult <TEntity>   mapResult = new MapResult <TEntity>(entity);

            foreach (string columanName in dataSource)
            {
                MapResultItem resultItem = mapResult.Items[columanName];
                resultItem.Status = MapResultItemStatus.Fail;
                object      target    = entity;
                object      subTarget = null;
                PropertyMap map       = pmc[columanName];
                if (map == null)
                {
                    resultItem.Status = MapResultItemStatus.PropertyNotMatch;
                }
                else
                {
                    try
                    {
                        if (map.KeyParts.Count >= 2)
                        {
                            for (int i = 0; i < (map.KeyParts.Count - 1); i++)
                            {
                                PropertyMap subMap = pmc[map.KeyParts[i]];
                                if (subMap == null)
                                {
                                    continue;
                                }

                                subTarget = subMap.Property.GetValue(target);
                                if (subTarget == null)
                                {
                                    subTarget = Activator.CreateInstance(subMap.Property.PropertyType);
                                    subMap.Property.SetValue(target, subTarget);
                                }
                                target = subTarget;
                            }
                        }

                        var dataValue = dataSource[columanName];
                        resultItem.OriginalValue = dataValue == DBNull.Value ? null : dataValue;
                        object mappedValue = map.Property.SetValue(target, resultItem.OriginalValue);
                        resultItem.MappedValue = mappedValue;
                        resultItem.Status      = MapResultItemStatus.Success;
                    }
                    catch (Exception ex)
                    {
                        resultItem.Status = MapResultItemStatus.Fail;
                        resultItem.Error  = ex;
                        throw new DataMappingException(
                                  "Column:{0} ColumnType:{1} Fail Mapping To EntityProperty:{2} PropertyType:{3}".FormatWith(
                                      columanName,
                                      resultItem.OriginalValue.GetType(),
                                      map.Key,
                                      map.Property.PropertyType),
                                  ex);
                    }
                }
            }

            return(mapResult);
        }