Esempio n. 1
0
        /// <summary>
        /// Try to get an instance of type T based on the dynamic object
        /// values and the IConfigurationMappingBase instance that defines
        /// the mapping between a T object and a dynamic instance
        /// </summary>
        /// <typeparam name="T">the type of the object to construct and fill</typeparam>
        /// <param name="values">a dynamic object with the values to fill</param>
        /// <returns></returns>
        public static T TryGet <T>(dynamic values) where T : class, new()
        {
            if (!ConfigurationMapper.HasMappingFor <T>())
            {
                if (Automap)
                {
                    return(TryGetWithoutMapping <T>(values));
                }
                else
                {
                    _logger.Warn("Type {0} is not mapped in the configuration", typeof(T));
                    //return new T();
                    throw new NotAMappedTypeException();
                }
            }

            T result = default(T);

            try
            {
                IConfigurationMappingsBase <T> mapper = ConfigurationMapper.GetMapper <T>();
                result = mapper.Get(values);
            }
            catch (Exception ex)
            {
                _logger.Error(ex.Message);
                throw new MapperInvocationException();
            }
            return(result);
        }
Esempio n. 2
0
        public void ShouldGetAnEmptyValueWithEmptyData()
        {
            Dictionary <string, object> dv = new Dictionary <string, object>();

            dv.Add("test_name", "Rui");
            User user = ValueFactory.TryGet <User>(dv);
            IConfigurationMappingsBase <User> mapper = ConfigurationMapper.GetMapper <User>();

            Assert.IsNotNull(mapper);
            Log(mapper.ConfiguredType);
            Log(mapper.Settings.ToList());
            var result = mapper.Get(dv);

            Assert.IsNotNull(result);
            Assert.IsTrue(ConfigurationMapper.HasMappingFor <User>());
            Log(result.ToString());
            Log(ConfigurationMapper.HasMappingFor <User>(), "Have mapping for user");
            Assert.IsNotNull(user);
            Log(user.ToString());
        }