Skip to content

Commit 745512b

Browse files
committed
Added GettingStarted Sample
1 parent c785d7c commit 745512b

2 files changed

Lines changed: 75 additions & 0 deletions

File tree

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
using NUnit.Framework;
2+
using SharpRepository.Repository;
3+
using Should;
4+
5+
namespace SharpRepository.Samples
6+
{
7+
[TestFixture]
8+
public class GettingStarted
9+
{
10+
/*
11+
* Getting Started
12+
*
13+
* Let's keep things simple. The Repository pattern is a facade that abstracts
14+
* away the technical concerns of your persistence implementation by offering a
15+
* collection-like interface for managing domain objects.
16+
*
17+
* SharpRepository is a persistent ignorant seam which sits between your
18+
* application and data access layer. SharpRepository provides the generic base
19+
* but also numerous persistence implementations (EF, RavenDb, etc) which can
20+
* be swapped based on project needs. By offering the needed separation
21+
* of concerns, SharpRepository promotes the ability to unit test agains mocks
22+
* as opposed to intergration testing against the database itself.
23+
*
24+
* For more information on the Repository Pattern:
25+
*
26+
* http://domaindrivendesign.org/books/#DDD
27+
* http://martinfowler.com/eaaCatalog/repository.html
28+
* http://msdn.microsoft.com/en-us/library/ff649690.aspx
29+
*
30+
* CRUD Operations
31+
*
32+
* The most basic repository operations are CRUD (create, read, update, delete).
33+
* Here we define the entity type, Order, which will be persisted and we execute
34+
* the four CRUD operations.
35+
*/
36+
public class Order
37+
{
38+
public int OrderId{ get; set; }
39+
public string Name { get; set; }
40+
}
41+
42+
[Test]
43+
public void SharpRepository_Supports_Basic_Crud_Operations()
44+
{
45+
// Declare your generic InMemory Repository.
46+
// Check out HowToAbstractAwayTheGenericRepository.cs for cleaner ways to new up a repo.
47+
var repo = new InMemoryRepository<Order, int>();
48+
49+
// Create
50+
var create = new Order { Name = "Big sale" };
51+
repo.Add(create);
52+
53+
const int expectedOrderId = 1;
54+
create.OrderId.ShouldEqual(expectedOrderId);
55+
56+
// Read
57+
var read = repo.Get(expectedOrderId);
58+
read.Name.ShouldEqual(create.Name);
59+
60+
// Update
61+
read.Name = "Really big sale";
62+
repo.Update(read);
63+
64+
var update = repo.Get(expectedOrderId);
65+
update.OrderId.ShouldEqual(expectedOrderId);
66+
update.Name.ShouldEqual(read.Name);
67+
68+
// Delete
69+
repo.Delete(update);
70+
var delete = repo.Get(expectedOrderId);
71+
delete.ShouldBeNull();
72+
}
73+
}
74+
}

SharpRepository.Samples/SharpRepository.Samples.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
<Compile Include="..\CommonAssembly.cs">
5050
<Link>Properties\CommonAssembly.cs</Link>
5151
</Compile>
52+
<Compile Include="GettingStarted.cs" />
5253
<Compile Include="HowToBatch.cs" />
5354
<Compile Include="HowToAbstractAwayTheGenericRepository.cs" />
5455
<Compile Include="HowToUseTraits.cs" />

0 commit comments

Comments
 (0)