コード例 #1
0
        protected void SetUp()
        {
            var f  = new FileStream("config.json", FileMode.Open);
            var sr = new StreamReader(f);

            try
            {
                var settings = JsonConvert
                               .DeserializeObject <MongoSettings>(sr.ReadToEnd());
                var client = new MongoClient(new MongoUrl(settings.Url));
                _database = client.GetDatabase(settings.DatabaseName);
            }
            catch (FileNotFoundException)
            {
                throw new Exception("Talk to John about why this one fails");
            }
            finally
            {
                f.Dispose();
                sr.Dispose();
            }

            _collectionName = Guid.NewGuid().ToString();
            _collection     = _database.GetCollection <Container>(_collectionName);
            var mapper = new StringMapper <BasePaymentModel>(x => x.PaymentType)
                         .Register <AchInputModel>("ach")
                         .Register <CreditCardInputModel>("cc")
                         .Register <PayPalInputModel>("paypal");

            BsonSerializer.RegisterDiscriminatorConvention(
                typeof(BasePaymentModel),
                new DiscriminatorConvention <string, BasePaymentModel>(mapper));
        }
コード例 #2
0
        public IMapper CreateMapper(object data)
        {
            IMapper mapper;

            if (IsXml(data.ToString()))
            {
                mapper = new XmlMapper();
            }
            else if (IsJson(data.ToString()))
            {
                mapper = new JsonMapper();
            }
            else
            {
                if (data.GetType().IsPrimitive)
                {
                    mapper = new PocoMapper();
                }
                else
                {
                    mapper = new StringMapper();
                }
            }
            return(mapper);
        }
コード例 #3
0
 public TermDatabase()
 {
     StringMapper = new StringMapper();
     TermMapper   = new EqualityComparerMapper <Term>(new IntegerMapperTermEqualityComparer());
     LabelToTerm  = new Dictionary <ulong, HashSet <Term> >();
     CurrentFrame = new Frame(this);
     _writer      = new Lazy <TermDatabaseWriter>(() => new TermDatabaseWriter(this));
     _reader      = new Lazy <TermDatabaseReader>(() => new TermDatabaseReader(this));
 }
コード例 #4
0
        public void GetProperty_Invoke_ReturnsBegan(string stringValue)
        {
            var item = new StringMapper();

            object value = 1;
            PropertyMapperResultType result = item.MapCellValue(new ReadCellValueResult(-1, stringValue), ref value);

            Assert.Equal(PropertyMapperResultType.SuccessIfNoOtherSuccess, result);
            Assert.Same(stringValue, value);
        }
        public void Use_string_as_discriminator()
        {
            var mapper = new StringMapper <object>(
                x => ((IHeterogeneousListItem)x).Type)
                         .Register <Polygon>(typeof(Polygon).Name)
                         .Register <Color>(typeof(Color).Name);

            var container = RoundTrip(
                new StringConverter(mapper, _resolver.GetResolvedPropertyName));

            CheckPolymorphicModels(container);
        }
コード例 #6
0
        public void Use_string_as_discriminator()
        {
            var mapper = new StringMapper <BasePaymentModel>(
                x => x.PaymentType)
                         .Register <AchInputModel>("ach")
                         .Register <CreditCardInputModel>("cc")
                         .Register <PayPalInputModel>("paypal");

            var container = RoundTrip(
                new StringConverter(mapper, _resolver.GetResolvedPropertyName));

            CheckPolymorphicModels(container);
        }
コード例 #7
0
 public NamespaceMapper(ITypeDiscoverer typeDiscoverer)
 {
     _typeDiscoverer       = typeDiscoverer;
     _clientToServerMapper = new StringMapper();
     _serverToClientMapper = new StringMapper();
 }
コード例 #8
0
ファイル: AutoMapper.cs プロジェクト: redbaty/excel-mapper
        private static bool AutoMap(this Type memberType, FallbackStrategy emptyValueStrategy, out ICellValueMapper mapper, out IFallbackItem emptyFallback, out IFallbackItem invalidFallback)
        {
            Type type = memberType.GetNullableTypeOrThis(out bool isNullable);

            Type[] interfaces = type.GetTypeInfo().ImplementedInterfaces.ToArray();

            IFallbackItem ReconcileFallback(FallbackStrategy strategyToPursue, bool empty)
            {
                // Empty nullable values should be set to null.
                if (empty && isNullable)
                {
                    return(new FixedValueFallback(null));
                }
                else if (strategyToPursue == FallbackStrategy.SetToDefaultValue || emptyValueStrategy == FallbackStrategy.SetToDefaultValue)
                {
                    return(new FixedValueFallback(type.DefaultValue()));
                }
                else
                {
                    Debug.Assert(emptyValueStrategy == FallbackStrategy.ThrowIfPrimitive);

                    // The user specified that we should set to the default value if it was empty.
                    return(new ThrowFallback());
                }
            }

            if (type == typeof(DateTime))
            {
                mapper          = new DateTimeMapper();
                emptyFallback   = ReconcileFallback(FallbackStrategy.ThrowIfPrimitive, empty: true);
                invalidFallback = ReconcileFallback(FallbackStrategy.ThrowIfPrimitive, empty: false);
            }
            else if (type == typeof(bool))
            {
                mapper          = new BoolMapper();
                emptyFallback   = ReconcileFallback(FallbackStrategy.ThrowIfPrimitive, empty: true);
                invalidFallback = ReconcileFallback(FallbackStrategy.ThrowIfPrimitive, empty: false);
            }
            else if (type.GetTypeInfo().IsEnum)
            {
                mapper          = new EnumMapper(type);
                emptyFallback   = ReconcileFallback(FallbackStrategy.ThrowIfPrimitive, empty: true);
                invalidFallback = ReconcileFallback(FallbackStrategy.ThrowIfPrimitive, empty: false);
            }
            else if (type == typeof(string) || type == typeof(object) || type == typeof(IConvertible))
            {
                mapper          = new StringMapper();
                emptyFallback   = ReconcileFallback(FallbackStrategy.SetToDefaultValue, empty: true);
                invalidFallback = ReconcileFallback(FallbackStrategy.SetToDefaultValue, empty: false);
            }
            else if (type == typeof(Uri))
            {
                mapper          = new UriMapper();
                emptyFallback   = ReconcileFallback(FallbackStrategy.SetToDefaultValue, empty: true);
                invalidFallback = ReconcileFallback(FallbackStrategy.ThrowIfPrimitive, empty: false);
            }
            else if (interfaces.Any(t => t == typeof(IConvertible)))
            {
                mapper          = new ChangeTypeMapper(type);
                emptyFallback   = ReconcileFallback(FallbackStrategy.ThrowIfPrimitive, empty: true);
                invalidFallback = ReconcileFallback(FallbackStrategy.ThrowIfPrimitive, empty: false);
            }
            else
            {
                mapper          = null;
                emptyFallback   = null;
                invalidFallback = null;
                return(false);
            }

            return(true);
        }
コード例 #9
0
 public StringMapperTests()
 {
     _mapper = new StringMapper();
 }
コード例 #10
0
 public void SetUp()
 {
     Mapper = new StringMapper <BaseClass>(x => x.Name)
              .Register(new Concrete1())
              .Register(new Concrete2());
 }
コード例 #11
-1
        protected void SetUp()
        {
            var f = new FileStream("config.json", FileMode.Open);
            var sr = new StreamReader(f);
            try
            {
                var settings = JsonConvert
                    .DeserializeObject<MongoSettings>(sr.ReadToEnd());
                var client = new MongoClient(new MongoUrl(settings.Url));
                _database = client.GetDatabase(settings.DatabaseName);
            }
            catch (FileNotFoundException)
            {
                throw new Exception("Talk to John about why this one fails");
            }
            finally
            {
                f.Dispose();
                sr.Dispose();
            }

            _collectionName = Guid.NewGuid().ToString();
            _collection = _database.GetCollection<Container>(_collectionName);
            var mapper = new StringMapper<BasePaymentModel>(x => x.PaymentType)
                .Register<AchInputModel>("ach")
                .Register<CreditCardInputModel>("cc")
                .Register<PayPalInputModel>("paypal");
            BsonSerializer.RegisterDiscriminatorConvention(
                typeof(BasePaymentModel),
                new DiscriminatorConvention<string, BasePaymentModel>(mapper));
        }