1. 程式人生 > 其它 >RavenDb學習(五)結果轉換

RavenDb學習(五)結果轉換

1)Result Transformers

public class Order
{
    public DateTime OrderedAt { get; set; }
 
    public Status Status { get; set; }
 
    public string CustomerId { get; set; }
 
    public IList<OrderLine> Lines { get; set; }
}

//單獨使用
public class OrderStatisticsTransformer : AbstractTransformerCreationTask<Order>
{
    public OrderStatisticsTransformer()
    {
        TransformResults = orders => from order in orders
                                     select new
                                            {
                                                order.OrderedAt,
                                                order.Status,
                                                order.CustomerId,
                                                CustomerName = LoadDocument<Customer>(order.CustomerId).Name,
                                                LinesCount = order.Lines.Count
                                            };
    }
}

public class OrderStatistics
{
    public DateTime OrderedAt { get; set; }
 
    public Status Status { get; set; }
 
    public string CustomerId { get; set; }
 
    public string CustomerName { get; set; }
 
    public int LinesCount { get; set; }
}

//配合查詢使用
IList<OrderStatistics> statistics = session.Query<Order>()
    .TransformWith<OrderStatisticsTransformer, OrderStatistics>()
    .Where(x => x.CustomerId == "customers/1")
    .ToList();

OrderStatistics statistic = session.Load<OrderStatisticsTransformer, OrderStatistics>("orders/1");

2) OfType<T> 
 OfType<T>是在客戶端進行資料轉換
比如說我們有一個這樣的索引:
public class Product_ByQuantity : AbstractIndexCreationTask<Product>
{
    public Product_ByQuantity()
    {
        Map = products => from product in products
                          select new
                          {
                              QuantityInWarehouse = product.QuantityInWarehouse
                          };
 
        TransformResults = (database, results) => from r in results
                                                  select new
                                                  {
                                                      Name = r.Name,
                                                      Description = r.Description
                                                  };
    }
}

產品類:
public class Product
{
    public string Id { get; set; }
    public string ArticleNumber { get; set; }
    public string Name { get; set; }
    public string Manufacturer { get; set; }
    public string Description { get; set; }
    public int QuantityInWarehouse { get; set; }
}

返回結果類:
public class ProductViewModel
{
    public string Name { get; set; }
    public string Description { get; set; }
}

查詢的之後,進行轉換
List<ProductViewModel> products = session.Query<Product, Product_ByQuantity>()
                                         .Where(x => x.QuantityInWarehouse > 100)
                                         .OfType<ProductViewModel>()
                                         .ToList();
3)ProjectFromIndexFieldsInto

這是map-only的索引

public class Product_ByQuantityNameAndDescription : AbstractIndexCreationTask<Product>
{
    public Product_ByQuantityNameAndDescription()
    {
        Map = products => from product in products
                          select new
                          {
                              QuantityInWarehouse = product.QuantityInWarehouse,
                              Name = product.Name,
                              Description = product.Description
                          };
 
        Stores.Add(x => x.Name, FieldStorage.Yes);
        Stores.Add(x => x.Description, FieldStorage.Yes);
    }
}


//查詢
List<ProductViewModel> products = session.Query<Product, Product_ByQuantityNameAndDescription>()
                                         .Where(x => x.QuantityInWarehouse > 50)
                                         .ProjectFromIndexFieldsInto<ProductViewModel>()
                                         .ToList();

4)客戶端轉換


索引的定義:
public class Product_ById : AbstractIndexCreationTask<ProductItem>
{
    public Product_ById()
    {
        Map = products => from product in products
                          select new
                          {
                              product.Id
                          };
    }
}

var warehouses = session.Query<dynamic, Product_ById>()
    .Customize(x => x.TransformResults((query, results) =>
        results.Cast<dynamic>().GroupBy(p => p.WarehouseId).Select(g =>
        {
            double count = 0;
            int totalSum = 0;
 
            var products = g.Select(product =>
            {
                count++;
                totalSum += product.Price;
                return new ProductItemViewModel
                           {
                               Name = product.Name,
                               Description = product.Description
                           };
            }).ToList();
 
            return new Warehouse()
                       {
                           Id = g.Key,
                           Products = products,
                           AverageProductPrice = totalSum / count,
                       };
        }))).ToList();