Skip to content

Commit f86415c

Browse files
authored
Merge pull request #4 from manfredbjorlin/feature-MaxAgeOnList
Feature max age on list
2 parents 70004c9 + 7f30d59 commit f86415c

File tree

7 files changed

+51
-19
lines changed

7 files changed

+51
-19
lines changed

Src/LookupUtility/LookupUtilityService.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ public LookupUtilityService(ILookupRepository lookupRepository)
2020
_lookupRepository = lookupRepository;
2121
}
2222

23-
public string GetValue(string list, string key, string defaultValue)
23+
public string GetValue(string list, string key, string defaultValue, TimeSpan maxAge = default(TimeSpan))
2424
{
25-
var dict = GetList(list);
25+
var dict = GetList(list, maxAge);
2626
string val;
2727
if (!dict.TryGetValue(key, out val))
2828
{
@@ -32,9 +32,9 @@ public string GetValue(string list, string key, string defaultValue)
3232
return val;
3333
}
3434

35-
public string GetValue(string list, string key, bool throwIfNotExists = false, bool allowDefaults = false)
35+
public string GetValue(string list, string key, bool throwIfNotExists = false, bool allowDefaults = false, TimeSpan maxAge = default(TimeSpan))
3636
{
37-
var dict = GetList(list);
37+
var dict = GetList(list, maxAge);
3838
string val;
3939
if (!dict.TryGetValue(key, out val))
4040
{
@@ -54,13 +54,13 @@ public string GetValue(string list, string key, bool throwIfNotExists = false, b
5454
return val;
5555
}
5656

57-
private Dictionary<string, string> GetList(string list)
57+
private Dictionary<string, string> GetList(string list, TimeSpan maxAge = default(TimeSpan))
5858
{
5959
var dict = new Dictionary<string, string>();
6060

6161
if (!_lookupValues.TryGetValue(list, out dict))
6262
{
63-
dict = _lookupRepository.LoadList(list);
63+
dict = _lookupRepository.LoadList(list, maxAge);
6464

6565
if (dict == null)
6666
{

Src/LookupUtility/Repository/ILookupRepository.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ namespace BizTalkComponents.Utilities.LookupUtility.Repository
88
{
99
public interface ILookupRepository
1010
{
11-
Dictionary<string, string> LoadList(string list);
11+
Dictionary<string, string> LoadList(string list, TimeSpan maxAge = default(TimeSpan));
1212
}
1313
}

Src/LookupUtility/Repository/LookupRepositoryMock.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ namespace BizTalkComponents.Utilities.LookupUtility.Repository
88
{
99
public class LookupRepositoryMock : ILookupRepository
1010
{
11-
public Dictionary<string, string> LoadList(string list)
11+
public Dictionary<string, string> LoadList(string list, TimeSpan maxAge = default(TimeSpan))
1212
{
13+
if (maxAge != default(TimeSpan) && maxAge > new TimeSpan(1, 0, 0))
14+
throw new ArgumentException("List too old");
1315
return new Dictionary<string, string>() { { "MockKey", "MockValue" } };
1416
}
1517
}

Src/LookupUtility/Repository/SharepointLookupRepository.cs

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,22 @@ namespace BizTalkComponents.Utilities.LookupUtility.Repository
1010
{
1111
public class SharepointLookupRepository : ILookupRepository
1212
{
13-
public Dictionary<string, string> LoadList(string list)
13+
Microsoft.SharePoint.Client.List spList;
14+
ClientContext clientContext;
15+
16+
public SharepointLookupRepository()
1417
{
1518
var site = ConfigurationManager.AppSettings["SharePointSite"];
19+
clientContext = new ClientContext(site);
1620

17-
ClientContext clientContext = new ClientContext(site);
18-
Web oWebsite = clientContext.Web;
19-
ListCollection collList = oWebsite.Lists;
21+
}
2022

21-
var spList = collList.GetByTitle(list);
23+
public Dictionary<string, string> LoadList(string list, TimeSpan maxAge = default(TimeSpan))
24+
{
25+
SetList(list);
26+
27+
if (maxAge != default(TimeSpan) && maxAge < GetAgeOfList(list))
28+
spList.RefreshLoad();
2229

2330
var q = new CamlQuery();
2431
ListItemCollection collListItem = spList.GetItems(q);
@@ -44,5 +51,17 @@ public Dictionary<string, string> LoadList(string list)
4451

4552
return dictionary;
4653
}
54+
55+
private void SetList(string list)
56+
{
57+
Web oWebsite = clientContext.Web;
58+
ListCollection collList = oWebsite.Lists;
59+
spList = collList.GetByTitle(list);
60+
}
61+
62+
private TimeSpan GetAgeOfList(string list)
63+
{
64+
return DateTime.Now.Subtract(spList.LastItemModifiedDate);
65+
}
4766
}
4867
}

Src/LookupUtility/Repository/SqlLookupRepository.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace BizTalkComponents.Utilities.LookupUtility.Repository
1010
{
1111
public class SqlLookupRepository : ILookupRepository
1212
{
13-
public Dictionary<string, string> LoadList(string list)
13+
public Dictionary<string, string> LoadList(string list, TimeSpan maxAge = default(TimeSpan))
1414
{
1515
string query = string.Format("SELECT [Key], Value FROM {0}", list);
1616

Test/UnitTest/LookupUtilityTest.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,26 @@ public class LookupUtilityTest
1616
[ClassInitialize]
1717
public static void Init(TestContext context)
1818
{
19-
2019
mock = new Mock<ILookupRepository>();
2120
util = new LookupUtilityService(mock.Object);
2221
var dictionary = new Dictionary<string, string>();
2322
dictionary.Add("ConfigKey", "ConfigValue");
2423
dictionary.Add("default", "DefaultValue");
25-
mock.Setup(s => s.LoadList(list)).Returns(dictionary);
24+
mock.Setup(s => s.LoadList(list, default(TimeSpan))).Returns(dictionary);
2625
}
2726

2827
[TestMethod]
2928
public void TestHappyPath()
30-
{
29+
{
3130
Assert.AreEqual("ConfigValue", util.GetValue(list, "ConfigKey"));
32-
mock.Verify(util => util.LoadList(list), Times.Once);
31+
mock.Verify(_util => _util.LoadList(list, default(TimeSpan)), Times.Once);
3332
}
3433

3534
[TestMethod]
3635
public void TestCache()
3736
{
3837
util.GetValue(list, "ConfigKey");
39-
mock.Verify(util => util.LoadList(list), Times.Once);
38+
mock.Verify(_util => _util.LoadList(list, default(TimeSpan)), Times.Once);
4039
}
4140

4241
[TestMethod]
@@ -76,5 +75,12 @@ public void TestNonExistingList()
7675
{
7776
util.GetValue("NonExistingList", "ConfigKey");
7877
}
78+
79+
[TestMethod]
80+
public void TestAgeListOK()
81+
{
82+
var ts = new TimeSpan(0, 30, 0);
83+
util.GetValue(list, "ConfigKey", maxAge: ts);
84+
}
7985
}
8086
}

packages/repositories.config

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<repositories>
3+
<repository path="..\Src\LookupUtility\packages.config" />
4+
<repository path="..\Test\UnitTest\packages.config" />
5+
</repositories>

0 commit comments

Comments
 (0)