コード例 #1
0
ファイル: ValueFactory.cs プロジェクト: rhwy/ConfArt
        /// <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);
        }
コード例 #2
0
ファイル: ValueFactoryTests.cs プロジェクト: rhwy/ConfArt
        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());
        }