Пример #1
0
        public void Project_Orders_Custom_Expression_2()
        {
            var config = new MapConfiguration();

            config.RegisterMap <Order, OrderDTO>((o, mc) => new OrderDTO {
                Id           = o.OrderDetails.ToList().Select(od => od.OrderId).First(),
                OrderNo      = o.OrderNo,
                Price        = o.OrderDetails.Sum(od => od.UnitPrice),
                OrderDetails = mc.MapToList <OrderDetail, OrderDetailDTO>(o.OrderDetails)
            });
            config.RegisterMap <OrderDetail, OrderDetailDTO>();
            config.RegisterMap <Product, ProductDTO>();
            config.RegisterMap <Company, CompanyDTO>();

            var projector = config.GetProjector <Order, OrderDTO>(o => o.OrderDetails.Select(od => od.Product.Supplier));
            var func      = Helper.CreateProjector(projector);
            var dtoList   = _orders.Select(func).ToList();

            Assert.AreEqual(
                dtoList[3].Price,
                _orders[3].OrderDetails.Sum(od => od.UnitPrice)
                );

            Assert.AreEqual(
                dtoList[3].OrderDetails.ToList()[2].Product.Supplier.CompanyName,
                _orders[3].OrderDetails[2].Product.Supplier.CompanyName
                );
        }
Пример #2
0
        public void Project_Orders_Custom_Expression()
        {
            var config = new MapConfiguration();

            config.RegisterMap <Order, OrderDTO>((o, mc) => new OrderDTO {
                Id           = o.Id,
                OrderNo      = o.OrderNo,
                OrderDetails = o.OrderDetails.Select(od => new OrderDetailDTO {
                    Id       = od.Id,
                    Product  = mc.Map <Product, ProductDTO>(od.Product),
                    SubPrice = od.UnitPrice * od.Count
                }).ToList(),
                Price = o.Price
            });
            config.RegisterMap <Product, ProductDTO>();
            config.RegisterMap <Company, CompanyDTO>();

            var projector = config.GetProjector <Order, OrderDTO>(o => o.OrderDetails.Select(od => od.Product.Supplier));
            var func      = Helper.CreateProjector(projector);
            var dtoList   = _orders.Select(func).ToList();

            Assert.Equal(
                dtoList[3].OrderDetails.ToList()[2].SubPrice,
                _orders[3].OrderDetails[2].UnitPrice * _orders[3].OrderDetails[2].Count
                );

            Assert.Equal(
                dtoList[3].OrderDetails.ToList()[2].Product.Supplier.CompanyName,
                _orders[3].OrderDetails[2].Product.Supplier.CompanyName
                );
        }
Пример #3
0
        public void Project_Orders_Without_Navigations()
        {
            var config = new MapConfiguration();

            config.RegisterMap <Order, OrderDTO>();

            var projector = config.GetProjector <Order, OrderDTO>(false);
            var func      = Helper.CreateProjector(projector);
            var dtoList   = _orders.Select(func).ToList();

            Assert.IsNull(dtoList[3].OrderDetails);
        }
Пример #4
0
        public void Project_Invalid_Method_Throws_Exception()
        {
            var config = new MapConfiguration();

            config.RegisterMap <Order, OrderDTO>((o, mc) => new OrderDTO {
                Id      = o.Id,
                OrderNo = o.OrderNo,
                Price   = mc.MapToDictionary <int, OrderDetail, int, int>(o.OrderDetails.ToDictionary(od => od.Id, od => od)).Sum(x => x.Key)
            });
            config.RegisterMap <OrderDetail, OrderDetailDTO>();
            config.RegisterMap <Product, ProductDTO>();
            config.RegisterMap <Company, CompanyDTO>();

            Assert.Throws <InvalidOperationException>(() => config.GetProjector <Order, OrderDTO>(o => o.OrderDetails.Select(od => od.Product.Supplier)));
        }
Пример #5
0
        public void Project_Orders_With_IncludePath()
        {
            var config = new MapConfiguration(DynamicMapping.MapAndCache);

            var projector = config.GetProjector <Order, OrderDTO>(new IncludePath("OrderDetails"));
            var func      = Helper.CreateProjector(projector);
            var dtoList   = _orders.Select(func).ToList();

            Assert.AreEqual(
                dtoList[3].OrderDetails.ToList()[2].Id,
                _orders[3].OrderDetails[2].Id
                );

            Assert.IsNull(dtoList[3].OrderDetails.ToList()[2].Product);
        }
Пример #6
0
        public void Project_Orders_With_Navigations()
        {
            var config = new MapConfiguration(DynamicMapping.MapAndCache);

            config.RegisterMap <OrderDetail, OrderDetailDTO>(b => b.SkipMember(od => od.Order));
            config.RegisterMap <Product, ProductDTO>(b => b.SkipMember(p => p.Supplier));

            var projector = config.GetProjector <Order, OrderDTO>();
            var func      = Helper.CreateProjector(projector);
            var dtoList   = _orders.Select(func).ToList();

            Assert.AreEqual(
                dtoList[3].OrderDetails.ToList()[2].Product.Id,
                _orders[3].OrderDetails[2].Product.Id
                );
        }