-
Notifications
You must be signed in to change notification settings - Fork 163
Expand file tree
/
Copy pathHowToUseGetSelectors.cs
More file actions
102 lines (87 loc) · 3.91 KB
/
Copy pathHowToUseGetSelectors.cs
File metadata and controls
102 lines (87 loc) · 3.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
using System;
using System.Linq;
using NUnit.Framework;
using SharpRepository.Repository;
using Shouldly;
using SharpRepository.InMemoryRepository;
namespace SharpRepository.Samples
{
[TestFixture]
public class HowToUseGetSelectors
{
/*
* What are Get Selectors?
*
* There are overloaded versions of the Get, GetAll, Find and FindAll methods
* that allow the use of a selector expression. You can think of it as what you
* would pass to the LINQ .Select() method.
*
* This allows you to only bring back the columns you are looking for to decrease
* network traffic, or map from your Entity to a ViewModel or other object structure
* that you need it in.
*
* What does this look like in practice?
*
* Let's try it out with some Orders and a OrderRepository
*/
public class Order
{
public int OrderId { get; set; }
public string Name { get; set; }
public double Total { get; set; }
public DateTime OrderDate { get; set; }
}
public class OrderViewModel
{
public string Name { get; set; }
public bool IsExpensiveOrder { get; set; }
}
[Test]
public void Repository_Supports_Selectors()
{
var repo = new InMemoryRepository<Order>();
// let's add a couple of orders to work with
repo.Add(new Order()
{
Name = "Order 1",
Total = 120.00,
OrderDate = new DateTime(2013, 4, 26)
});
repo.Add(new Order()
{
Name = "Order 2",
Total = 80.00,
OrderDate = new DateTime(2013, 4, 24)
});
// normal Get method
var order = repo.Get(1);
order.OrderId.ShouldBe(1);
// in this case we only need the order name
var orderName = repo.Get(1, x => x.Name);
orderName.ShouldBe("Order 1");
// we can also bring back an anonymous type if needed
var anonymousType = repo.Get(1, x => new { Name = x.Name, IsExpensiveOrder = x.Total > 100.0 });
anonymousType.IsExpensiveOrder.ShouldBeTrue();
// or we can map it to a specific type we have defined like a ViewModel
var viewModel = repo.Get(1, x => new OrderViewModel() {Name = x.Name, IsExpensiveOrder = x.Total > 100.0});
viewModel.IsExpensiveOrder.ShouldBeTrue();
// We have the same options with the GetAll, Find and FindAll as well
orderName = repo.Find(x => x.OrderId == 2, x => x.Name);
orderName.ShouldBe("Order 2");
// we can also bring back an anonymous type if needed
var anonymousTypes = repo.GetAll(x => new { Name = x.Name, IsExpensiveOrder = x.Total > 100.0 }).ToList();
anonymousTypes.Count.ShouldBe(2);
anonymousTypes.First().Name.ShouldBe("Order 1");
anonymousTypes.First().IsExpensiveOrder.ShouldBeTrue();
anonymousTypes.Last().Name.ShouldBe("Order 2");
anonymousTypes.Last().IsExpensiveOrder.ShouldBeFalse();
// or we can map it to a specific type we have defined like a ViewModel
var viewModels = repo.FindAll(x => x.OrderId < 5, x => new OrderViewModel() { Name = x.Name, IsExpensiveOrder = x.Total > 100.0 }).ToList();
viewModels.Count.ShouldBe(2);
viewModels.First().Name.ShouldBe("Order 1");
viewModels.First().IsExpensiveOrder.ShouldBeTrue();
viewModels.Last().Name.ShouldBe("Order 2");
viewModels.Last().IsExpensiveOrder.ShouldBeFalse();
}
}
}