forked from SharpRepository/SharpRepository
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHowToUseTraits.cs
More file actions
109 lines (100 loc) · 4.1 KB
/
HowToUseTraits.cs
File metadata and controls
109 lines (100 loc) · 4.1 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
103
104
105
106
107
108
109
using NUnit.Framework;
using SharpRepository.Repository;
using SharpRepository.Repository.Traits;
using System.Linq;
using Should;
using SharpRepository.InMemoryRepository;
namespace SharpRepository.Samples
{
[TestFixture]
public class HowToUseTraits
{
/*
* What Are Traits?
*
* Traits are based on the Interface Segregation Principle (ISP)
* which is the notion that 'many client specific interfaces are
* better than one general purpose interface.' As Uncle Bob puts
* it, "Make fine grained interfaces that are client specific"
* or don't force clients to implement interfaces they don’t use.
*
* http://en.wikipedia.org/wiki/Interface_segregation_principle
* http://en.wikipedia.org/wiki/Solid_(object-oriented_design)
*
* Traits are those segregated interfaces which expose a subset
* of what RepositoryBase and its inheritors offer. Traits map to
* repository operations. For example, ICanAdd trait is an interface
* with exposes the repository Add methods. Other examples include
* ICanUpdate and ICanDelete.
*
* Note, this implementation was inspired by Richard Dingwall's article,
* "IRepository: one size does not fit all"
* http://richarddingwall.name/2009/01/19/irepositoryt-one-size-does-not-fit-all/
*
* Why Should I Use Traits?
*
* By default, the repository exposes all CRUD operations through
* public methods. That's convenient but it might not play nicely
* with your business rules and thus the repository API you wish to
* code against. Let's say your business spec states that Orders can
* never be changed once issued. If you code directly against
* InMemoryRepository<Order, int> you'll have access to operations
* like Add(), Get(), Update() and Delete(). If you wish to reduce
* your OrderRepository API to a subset of methods -- just Add() and
* Get() -- you'll need to leverage traits and you need to start coding
* against an IOrderRepository interface rather than the implementation
* itself.
*
* How Do I Use Traits?
*
* Let's work through an example using the OrderRepository called
* out above.
*
* First, define the Order.
*/
public class Order
{
public int OrderId{ get; set; }
public string Name { get; set; }
}
/*
* Next, define a custom IOrderRepository which limits the Order
* repository to Add and Get operations.
*/
private interface IOrderRepository : ICanAdd<Order>, ICanGet<Order, int> {}
/*
* Finally, create a concrete implementation of the OrderRepository.
*/
private class OrderRepository : InMemoryRepository<Order, int>, IOrderRepository {}
/*
* That's it. Check out the examples below.
*
* In the first test, you'll see that IOrderRepository has been
* reduced to only the Add and Get operations.
*/
[Test]
public void IOrderRepository_Can_Only_Add_And_Get()
{
IOrderRepository repo = new OrderRepository();
repo.Add(new Order{Name = "Big Sale"});
var result = repo.GetAll();
result.Count().ShouldEqual(1);
}
/*
* But be sure to code against the IOrderRepository rather
* than OrderRepository directly. Otherwise, additional operations
* like Delete() are still availabe.
*/
[Test]
public void OrderRepository_Can_Do_More_Than_Add_And_Get()
{
var order = new Order {Name = "Big Sale"};
var repo = new OrderRepository();
repo.Add(order);
order.OrderId.ShouldEqual(1);
repo.GetAll().Count().ShouldEqual(1);
repo.Delete(order);
repo.GetAll().Count().ShouldEqual(0);
}
}
}