Esempio n. 1
0
        /// <summary>
        /// Map model instance
        /// </summary>
        public TDomain MapModelInstance <TModel, TDomain>(TModel modelInstance) where TDomain : new()
        {
            // Set the identity source
            IEntitySourceProvider currentProvider = null;


            ClassMap classMap = this.m_mapFile.GetModelClassMap(typeof(TModel), typeof(TDomain));

            if (classMap == null)
            {
                classMap = this.m_mapFile.GetModelClassMap(typeof(TModel));
            }

            if (classMap == null || modelInstance == null)
            {
                return(default(TDomain));
            }

            // Now the property maps
            TDomain retVal = new TDomain();

            // Properties
            PropertyInfo[] properties = null;
            Dictionary <String, PropertyInfo[]> propertyClassMap = null;

            if (!s_modelPropertyCache.TryGetValue(typeof(TModel), out propertyClassMap))
            {
                lock (s_modelPropertyCache)
                {
                    if (!s_modelPropertyCache.TryGetValue(typeof(TModel), out propertyClassMap))
                    {
                        propertyClassMap = new Dictionary <string, PropertyInfo[]>();
                    }
                    if (!s_modelPropertyCache.ContainsKey(typeof(TModel)))
                    {
                        s_modelPropertyCache.Add(typeof(TModel), propertyClassMap);
                    }
                }
            }

            if (!propertyClassMap.TryGetValue(String.Empty, out properties))
            {
                lock (s_modelPropertyCache)
                {
                    properties = typeof(TModel).GetRuntimeProperties().Where(m => m != null &&
                                                                             m.GetCustomAttribute <DataIgnoreAttribute>() == null &&
                                                                             (primitives.Contains(m.PropertyType) || m.PropertyType.GetTypeInfo().IsEnum) &&
                                                                             m.CanWrite).ToArray();
                    if (!propertyClassMap.ContainsKey(String.Empty))
                    {
                        propertyClassMap.Add(String.Empty, properties);
                    }
                }
            }

            // Iterate through properties
            foreach (var propInfo in properties)
            {
                var propValue = propInfo.GetValue(modelInstance);
                // Property info
                if (propValue == null)
                {
                    continue;
                }

                if (!propInfo.PropertyType.GetTypeInfo().IsPrimitive&& propInfo.PropertyType != typeof(Guid) &&
                    (!propInfo.PropertyType.GetTypeInfo().IsGenericType || propInfo.PropertyType.GetGenericTypeDefinition() != typeof(Nullable <>)) &&
                    propInfo.PropertyType != typeof(String) &&
                    propInfo.PropertyType != typeof(DateTime) &&
                    propInfo.PropertyType != typeof(DateTimeOffset) &&
                    propInfo.PropertyType != typeof(Type) &&
                    propInfo.PropertyType != typeof(Decimal) &&
                    propInfo.PropertyType != typeof(byte[]) &&
                    !propInfo.PropertyType.GetTypeInfo().IsEnum)
                {
                    continue;
                }

                // Map property
                PropertyMap propMap = null;
                classMap.TryGetModelProperty(propInfo.Name, out propMap);
                PropertyInfo domainProperty = null;
                Object       targetObject   = retVal;

                // Set
                if (propMap == null)
                {
                    domainProperty = typeof(TDomain).GetRuntimeProperty(propInfo.Name);
                }
                else
                {
                    domainProperty = typeof(TDomain).GetRuntimeProperty(propMap.DomainName);
                }

                object domainValue = null;
                // Set value
                if (domainProperty == null)
                {
                    continue;
                }
                //Debug.WriteLine ("Unmapped property ({0}).{1}", typeof(TModel).Name, propInfo.Name);
                else if (domainProperty.PropertyType == typeof(byte[]) && propInfo.PropertyType.StripNullable() == typeof(Guid))
                {
                    domainProperty.SetValue(targetObject, ((Guid)propValue).ToByteArray());
                }
                else if (
                    (domainProperty.PropertyType == typeof(DateTime) || domainProperty.PropertyType == typeof(DateTime?)) &&
                    (propInfo.PropertyType == typeof(DateTimeOffset) || propInfo.PropertyType == typeof(DateTimeOffset?)))
                {
                    domainProperty.SetValue(targetObject, ((DateTimeOffset)propValue).DateTime);
                }
                else if (domainProperty.PropertyType.GetTypeInfo().IsAssignableFrom(propInfo.PropertyType.GetTypeInfo()))
                {
                    domainProperty.SetValue(targetObject, propValue);
                }
                else if (propInfo.PropertyType == typeof(Type) && domainProperty.PropertyType == typeof(String))
                {
                    domainProperty.SetValue(targetObject, (propValue as Type).AssemblyQualifiedName);
                }
                else if (MapUtil.TryConvert(propValue, domainProperty.PropertyType, out domainValue))
                {
                    domainProperty.SetValue(targetObject, domainValue);
                }
            }

            return(retVal);
        }
Esempio n. 2
0
 /// <summary>
 /// Delay loader ctor
 /// </summary>
 public EntitySource(IEntitySourceProvider provider)
 {
     m_provider = provider;
 }