Skip to content

Commit 8f73ce2

Browse files
committed
Improve Lifetimes example in chapter 10 (to show previous values)
1 parent 329c681 commit 8f73ce2

File tree

6 files changed

+84
-17
lines changed

6 files changed

+84
-17
lines changed

Chapter10/D_LifetimeExamples/LifetimeExamples/Pages/RowCount/Captured.cshtml.cs

+9-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ namespace LifetimeExamples.Pages
1111
{
1212
public class CapturedModel : PageModel
1313
{
14+
private readonly static List<RowCountViewModel.Count> _previousCounts = new(); // c#9 feature - target typing
1415
public RowCountViewModel RowCounts { get; set; }
1516

1617
private readonly CapturingRepository _capturingRepo;
@@ -27,10 +28,16 @@ ScopedDataContext scopedDataContext
2728

2829
public void OnGet()
2930
{
31+
var count = new RowCountViewModel.Count
32+
{
33+
DataContext = _scopedDataContext.RowCount,
34+
Repository = _capturingRepo.RowCount,
35+
};
36+
_previousCounts.Insert(0, count);
3037
RowCounts = new RowCountViewModel
3138
{
32-
DataContextCount = _scopedDataContext.RowCount,
33-
RepositoryCount = _capturingRepo.RowCount,
39+
Current = count,
40+
Previous = _previousCounts,
3441
};
3542
}
3643
}

Chapter10/D_LifetimeExamples/LifetimeExamples/Pages/RowCount/Scoped.cshtml.cs

+9-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ namespace LifetimeExamples.Pages
1111
{
1212
public class ScopedModel : PageModel
1313
{
14+
private readonly static List<RowCountViewModel.Count> _previousCounts = new(); // c#9 feature - target typing
1415
public RowCountViewModel RowCounts { get; set; }
1516

1617
private readonly ScopedRepository _scopedRepo;
@@ -27,10 +28,16 @@ ScopedDataContext scopedDataContext
2728

2829
public void OnGet()
2930
{
31+
var count = new RowCountViewModel.Count
32+
{
33+
DataContext = _scopedDataContext.RowCount,
34+
Repository = _scopedRepo.RowCount,
35+
};
36+
_previousCounts.Insert(0, count);
3037
RowCounts = new RowCountViewModel
3138
{
32-
DataContextCount = _scopedDataContext.RowCount,
33-
RepositoryCount = _scopedRepo.RowCount,
39+
Current = count,
40+
Previous = _previousCounts,
3441
};
3542
}
3643
}

Chapter10/D_LifetimeExamples/LifetimeExamples/Pages/RowCount/Singleton.cshtml.cs

+9-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ namespace LifetimeExamples.Pages
1111
{
1212
public class SingletonModel : PageModel
1313
{
14+
private readonly static List<RowCountViewModel.Count> _previousCounts = new(); // c#9 feature - target typing
1415
public RowCountViewModel RowCounts { get; set; }
1516

1617
private readonly SingletonRepository _singletonRepo;
@@ -27,10 +28,16 @@ SingletonDataContext singletonDataContext
2728

2829
public void OnGet()
2930
{
31+
var count = new RowCountViewModel.Count
32+
{
33+
DataContext = _singletonDataContext.RowCount,
34+
Repository = _singletonRepo.RowCount,
35+
};
36+
_previousCounts.Insert(0, count);
3037
RowCounts = new RowCountViewModel
3138
{
32-
DataContextCount = _singletonDataContext.RowCount,
33-
RepositoryCount = _singletonRepo.RowCount,
39+
Current = count,
40+
Previous = _previousCounts,
3441
};
3542
}
3643
}

Chapter10/D_LifetimeExamples/LifetimeExamples/Pages/RowCount/Transient.cshtml.cs

+9-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ namespace LifetimeExamples.Pages
1111
{
1212
public class TransientModel : PageModel
1313
{
14+
private readonly static List<RowCountViewModel.Count> _previousCounts = new(); // c#9 feature - target typing
1415
public RowCountViewModel RowCounts { get; set; }
1516

1617
private readonly TransientRepository _transientRepo;
@@ -27,10 +28,16 @@ TransientDataContext transientDataContext
2728

2829
public void OnGet()
2930
{
31+
var count = new RowCountViewModel.Count
32+
{
33+
DataContext = _transientDataContext.RowCount,
34+
Repository = _transientRepo.RowCount,
35+
};
36+
_previousCounts.Insert(0, count);
3037
RowCounts = new RowCountViewModel
3138
{
32-
DataContextCount = _transientDataContext.RowCount,
33-
RepositoryCount = _transientRepo.RowCount,
39+
Current = count,
40+
Previous = _previousCounts,
3441
};
3542
}
3643
}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,40 @@
11
@model RowCountViewModel
22

33
<h4>Random row count values</h4>
4-
<dl class="dl-horizontal">
5-
<dt>The <code>DatabaseContext.RowCount</code> is</dt>
6-
<dd>@Model.DataContextCount.ToString("###,###,###")</dd>
7-
<dt>The <code>Repository.RowCount</code> is</dt>
8-
<dd>@Model.RepositoryCount.ToString("###,###,###")</dd>
9-
</dl>
4+
5+
<dl class="row">
6+
<div class="col">
7+
<dt>The <em>Current</em> <code>DatabaseContext.RowCount</code> is</dt>
8+
<dd>@Model.Current.DataContext.ToString("###,###,###")</dd>
9+
</div>
10+
<div class="col">
11+
<dt>The <em>Current</em> <code>Repository.RowCount</code> is</dt>
12+
<dd>@Model.Current.Repository.ToString("###,###,###")</dd>
13+
</div>
14+
</dl>
15+
16+
<h4>Previous values:</h4>
17+
@if (Model.Previous?.Count > 1)
18+
{
19+
@*Not ideal as itterates list twice, could be refactored*@
20+
<dl class="row">
21+
<div class="col">
22+
<dt>The <em>Previous</em> <code>DatabaseContext.RowCount</code>s are</dt>
23+
@foreach (var counts in Model.Previous.Skip(1))
24+
{
25+
<dd class="col">@counts.DataContext.ToString("###,###,###")</dd>
26+
}
27+
</div>
28+
<div class="col">
29+
<dt>The <em>Previous</em> <code>Repository.RowCount</code>s are</dt>
30+
@foreach (var counts in Model.Previous.Skip(1))
31+
{
32+
<dd class="col">@counts.Repository.ToString("###,###,###")</dd>
33+
}
34+
</div>
35+
</dl>
36+
}
37+
else
38+
{
39+
<p><em>Hit refresh to generate new random values. Previous values will be listed here</em></p>
40+
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
1-
namespace LifetimeExamples
1+
using System.Collections.Generic;
2+
3+
namespace LifetimeExamples
24
{
35
public class RowCountViewModel
46
{
5-
public int DataContextCount { get; set; }
6-
public int RepositoryCount { get; set; }
7+
public Count Current { get; set; }
8+
public List<Count> Previous { get; set; }
9+
10+
public class Count
11+
{
12+
public int DataContext { get; set; }
13+
public int Repository { get; set; }
14+
}
715
}
816
}

0 commit comments

Comments
 (0)