Skip to content

Commit 128a70b

Browse files
Added "Find" method, updated README
1 parent 1101cbc commit 128a70b

File tree

4 files changed

+73
-5
lines changed

4 files changed

+73
-5
lines changed

Lith.DocStore.Tests/ModelContextTests.cs

+32
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Lith.DocStore.Models;
33
using Lith.DocStore.Tests.SupportingUtils;
44
using Microsoft.VisualStudio.TestTools.UnitTesting;
5+
using System;
56
using System.Collections.Generic;
67
using System.Linq;
78

@@ -164,5 +165,36 @@ public void UpdateModelInStore_MustHaveNewValue()
164165
Assert.IsTrue(results.Any());
165166
}
166167
}
168+
169+
[TestMethod]
170+
public void FindAndUpdateModel_MustFindObject()
171+
{
172+
var modelHelper = new JSONModelHelper();
173+
var itemID = Guid.Empty;
174+
175+
using (var ctx = new ModelsContext(modelHelper))
176+
{
177+
var tops = ctx.Shops.FirstOrDefault(a => a.Name == "Tops");
178+
itemID = tops.ID;
179+
}
180+
181+
using (var ctx = new ModelsContext(modelHelper))
182+
{
183+
var item = ctx.Shops.Find(itemID);
184+
185+
if (item != null)
186+
{
187+
item.Name += " 2nd";
188+
189+
var result = ctx.Shops.Find(itemID);
190+
191+
Assert.AreEqual("Tops 2nd", result.Name);
192+
}
193+
else
194+
{
195+
Assert.Fail("Couldn't find Item by ID");
196+
}
197+
}
198+
}
167199
}
168200
}

Lith.DocStore.sln

+5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lith.DocStore.Models", "Lit
99
EndProject
1010
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lith.DocStore.Tests", "Lith.DocStore.Tests\Lith.DocStore.Tests.csproj", "{04A797D0-F196-47E2-A973-A1F99F1C4DD4}"
1111
EndProject
12+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{E4B4DD2F-37BC-44FA-A873-DDADCFC0BB3A}"
13+
ProjectSection(SolutionItems) = preProject
14+
README.md = README.md
15+
EndProjectSection
16+
EndProject
1217
Global
1318
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1419
Debug|Any CPU = Debug|Any CPU

Lith.DocStore/ItemSet.cs

+17
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,23 @@ public void AddRange(IEnumerable<T> models)
4949
}
5050
}
5151

52+
public T Find(Guid ID)
53+
{
54+
var result = setItems.FirstOrDefault(a => !a.IsDeleted && a.ID == ID);
55+
56+
if (result == null)
57+
{
58+
var ctxResult = context.Entities.FirstOrDefault(a => !a.IsDeleted && a.ID == ID);
59+
60+
if (ctxResult is T)
61+
{
62+
result = ctxResult as T;
63+
}
64+
}
65+
66+
return result;
67+
}
68+
5269
private List<T> GetFreshItems()
5370
{
5471
var manager = new Manager(context.ModelHelper);

README.md

+19-5
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@ Lith.DocStore provides interfaces that allow you to create physical data files w
77
The idea behind this is that you can run unit tests against models and logic without commiting to a database or having a connection.
88

99
# Benefits
10-
1. Models live in a seperate project.
10+
1. Models can live in a seperate project, which reduces the need for DTO project/objects.
1111
2. No setup apart from Creating your models
1212
3. Can be used instead of having the need to create a database when building a new project
1313
4. Any other benefits I couldn't think of.
1414

15+
# Installation
16+
nuget: [install-package lith.docstore](https://www.nuget.org/packages/Lith.DocStore)
17+
1518
# Usage
1619
1. Create Models
1720
```C#
@@ -41,7 +44,7 @@ The idea behind this is that you can run unit tests against models and logic wit
4144
public class ModelsContext : StoreContext
4245
{
4346
public ModelsContext()
44-
: base(new YourModelSerializer())
47+
: base(new JSONModelHelper())
4548
{
4649

4750
}
@@ -54,7 +57,7 @@ The idea behind this is that you can run unit tests against models and logic wit
5457
}
5558
```
5659

57-
3. Add to your Model
60+
3. Add Record
5861
```C#
5962
var shopA = new Shop
6063
{
@@ -69,7 +72,7 @@ The idea behind this is that you can run unit tests against models and logic wit
6972
}
7073
```
7174

72-
4. Query your Model
75+
4. Query Record
7376
```C#
7477
using (var ctx = new ModelsContext())
7578
{
@@ -80,4 +83,15 @@ The idea behind this is that you can run unit tests against models and logic wit
8083
}
8184
```
8285

83-
5. Have a cold one ;)
86+
5. Find and Update Record
87+
```C#
88+
using(var ctx = new ModelsContext())
89+
{
90+
var item = ctx.Shops.Find(id);
91+
item.Name = "DEF";
92+
93+
ctx.Save();
94+
}
95+
```
96+
97+
6. Have a cold one ;)

0 commit comments

Comments
 (0)