Example #1
0
        static ActiveRecordConnection()
        {
            _mapperFactory = new MapperFactory();

            _mapperFactory.ConfigureMapper <IDataReader, TRecord>(true, true, true, "Q");
            _mapperOne = _mapperFactory.GetMapper <IDataReader, TRecord>();

            _mapperFactory.ConfigureMapper <IDataReader, List <TRecord> >(true, true, true);
            _mapperMany = _mapperFactory.GetMapper <IDataReader, List <TRecord> >();
        }
 static WcfConfigurationService()
 {
     //predefine the custom mapper from IDataReader to WcfEndpoint
     _mapperFac.ConfigureMapper <IDataReader, WcfEndpoint>(
         true, true, false,
         "BindingXml", "BindingTypeCode", "EndpointBehaviorXml", "IdentityXml"
         )
     .From(rdr => new BindingXml(
               rdr.GetString(rdr.GetOrdinal("BindingTypeCode")),
               rdr.IsDBNull(rdr.GetOrdinal("BindingXml")) ? null : rdr.GetString(rdr.GetOrdinal("BindingXml"))
               ))
     .To <BindingXml>((endpoint, val) =>
     {
         endpoint.BindingXml = val;
         return(endpoint);
     })
     .From(rdr => rdr.IsDBNull(rdr.GetOrdinal("EndpointBehaviorXml")) ? null : new EndpointBehaviorXml(rdr.GetString(rdr.GetOrdinal("EndpointBehaviorXml"))))
     .To <EndpointBehaviorXml>((endpoint, val) =>
     {
         endpoint.EndpointBehaviorXml = val;
         return(endpoint);
     })
     .From(rdr => rdr.IsDBNull(rdr.GetOrdinal("IdentityXml")) ? null : new IdentityXml(rdr.GetString(rdr.GetOrdinal("IdentityXml"))))
     .To <IdentityXml>((endpoint, val) =>
     {
         endpoint.IdentityXml = val;
         return(endpoint);
     });
 }
Example #3
0
        public void TestMappers()
        {
            var fac = new MapperFactory();
            var intToNullableDecimalMapper = fac.GetMapper <int, decimal?>();

            Assert.AreEqual(1, intToNullableDecimalMapper(1));

            var intListToNullableDecimalArrayMapper = fac.GetMapper <IList <int>, decimal?[]>();
            var nullableDecimalArray = intListToNullableDecimalArrayMapper(new List <int> {
                1, 2, 3
            });

            Assert.AreEqual(2, nullableDecimalArray[1]);

            var intArrayToLongListMapper = fac.GetMapper <int[], List <long> >();
            var longList = intArrayToLongListMapper(new[] { 1, 2, 3 });

            Assert.AreEqual(2L, longList[1]);

            fac.ConfigureMapper <string, double>()
            .From(from => from).To <string>((to, val) => double.Parse(val));
            var stringListToDoubleArrayMapper = fac.GetMapper <List <string>, double[]>();
            var doubleArray = stringListToDoubleArrayMapper(new List <string> {
                "1.1", "2.2", "3.3"
            });

            Assert.AreEqual(2.2, doubleArray[1]);

            fac.ConfigureMapper <MappingFrom, MappingTo>(true, true, true, "guid")
            .From(from => from.Other)
            .To <double>(
                (to, val) =>
            {
                to.Other2 = val.ToString();
                return(to);
            });
            var customMapper = fac.GetMapper <MappingFrom, MappingTo>();
            var guid         = Guid.NewGuid();
            var customFrom   = new MappingFrom {
                FromID = 1, Name = "name", Status = MappingFromStatus.Value2, Guid = guid
            };
            var customTo = customMapper(customFrom);

            Assert.AreEqual(1, customTo.From_id);
            Assert.AreEqual("name", customTo.Name);
            Assert.AreEqual("0", customTo.Other2);
            Assert.AreEqual(1, customTo.Status);
            Assert.AreNotEqual(guid, customTo.Guid);

            var dt = new DataTable("table");

            dt.Columns.Add(new DataColumn("FromID", typeof(int)));
            dt.Columns.Add(new DataColumn("Name", typeof(string)));
            dt.Columns[1].AllowDBNull = true;
            dt.Columns.Add(new DataColumn("Other", typeof(int)));
            dt.Rows.Add(1, "name", 0);

            fac.ConfigureMapper <DataRow, MappingTo>(true, true, true)
            .From(from => (int)from["Other"])
            .To <double>(
                (to, val) =>
            {
                to.Other2 = val.ToString();
                return(to);
            }
                );
            var dataRowToCustomMapper = fac.GetMapper <DataRow, MappingTo>();

            customTo = dataRowToCustomMapper(dt.Rows[0]);
            Assert.AreEqual(1, customTo.From_id);
            Assert.AreEqual("name", customTo.Name);
            Assert.AreEqual("0", customTo.Other2);
            dt.Rows[0]["name"] = DBNull.Value;
            customTo           = dataRowToCustomMapper(dt.Rows[0]);
            Assert.AreEqual(null, customTo.Name);

            var dataTableToCustomCollectionMapper = fac.GetMapper <DataTable, List <MappingTo> >();
            var customToCollection = dataTableToCustomCollectionMapper(dt);

            Assert.AreEqual(1, customToCollection.Count);
            Assert.AreEqual(1, customToCollection[0].From_id);
            Assert.AreEqual(null, customToCollection[0].Name);
            Assert.AreEqual("0", customToCollection[0].Other2);

            fac.ConfigureMapper <DataTableReader, MappingTo>(true, true, true)
            .From(from => from.GetInt32(from.GetOrdinal("Other")))
            .To <double>(
                (to, val) =>
            {
                to.Other2 = val.ToString();
                return(to);
            }
                );
            var dataReaderToCustomMapper = fac.GetMapper <DataTableReader, MappingTo>();
            var rdr = dt.CreateDataReader();

            rdr.Read();
            customTo = dataReaderToCustomMapper(rdr);
            rdr.Close();
            Assert.AreEqual(1, customTo.From_id);
            Assert.AreEqual(null, customTo.Name);
            Assert.AreEqual("0", customTo.Other2);

            var dataReaderToCustomCollectionMapper = fac.GetMapper <DataTableReader, List <MappingTo> >();

            customToCollection = dataReaderToCustomCollectionMapper(dt.CreateDataReader());
            Assert.AreEqual(1, customToCollection.Count);
            Assert.AreEqual(1, customToCollection[0].From_id);
            Assert.AreEqual(null, customToCollection[0].Name);
            Assert.AreEqual("0", customToCollection[0].Other2);

            var dataReaderToCustomArrayMapper = fac.GetMapper <DataTableReader, MappingTo[]>();
            var customToArray = dataReaderToCustomArrayMapper(dt.CreateDataReader());

            Assert.AreEqual(1, customToArray.Length);
            Assert.AreEqual(1, customToArray[0].From_id);
            Assert.AreEqual(null, customToArray[0].Name);
            Assert.AreEqual("0", customToArray[0].Other2);
        }