-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathExampleInMemoryContext.cs
More file actions
123 lines (109 loc) · 5.4 KB
/
Copy pathExampleInMemoryContext.cs
File metadata and controls
123 lines (109 loc) · 5.4 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// =================================================================================================================================
// Copyright (c) RapidField LLC. Licensed under the MIT License. See LICENSE.txt in the project root for license information.
// =================================================================================================================================
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.Extensions.Configuration;
using RapidField.SolidInstruments.Core;
using RapidField.SolidInstruments.Core.ArgumentValidation;
using RapidField.SolidInstruments.DataAccess.EntityFramework;
using RapidField.SolidInstruments.Example.DatabaseModel.Entities;
using RapidField.SolidInstruments.Example.DatabaseModel.Repositories;
using System;
using System.Diagnostics;
using System.Linq;
namespace RapidField.SolidInstruments.Example.DatabaseModel
{
/// <summary>
/// Represents a connection to a prototypical, in-memory database.
/// </summary>
public sealed class ExampleInMemoryContext : ExampleContext
{
/// <summary>
/// Initializes a new instance of the <see cref="ExampleInMemoryContext" /> class.
/// </summary>
/// <param name="applicationConfiguration">
/// Configuration information for the application.
/// </param>
/// <param name="databaseName">
/// The name of the backing database, which matches the associated connection string key in
/// <paramref name="applicationConfiguration" />.
/// </param>
/// <exception cref="ArgumentEmptyException">
/// <paramref name="databaseName" /> is empty.
/// </exception>
/// <exception cref="ArgumentNullException">
/// <paramref name="applicationConfiguration" /> is <see langword="null" /> -or- <paramref name="databaseName" /> is
/// <see langword="null" />.
/// </exception>
public ExampleInMemoryContext(IConfiguration applicationConfiguration, String databaseName)
: base(applicationConfiguration, ContextDatabaseType.InMemory, databaseName.RejectIf().IsNullOrEmpty(nameof(databaseName)))
{
return;
}
/// <summary>
/// Populates the current <see cref="ExampleInMemoryContext" /> with test data.
/// </summary>
/// <returns>
/// The current <see cref="ExampleInMemoryContext" /> with test data.
/// </returns>
[DebuggerHidden]
internal ExampleInMemoryContext WithTestData()
{
var fibonacciNumberSeriesValues = new Int64[] { 0, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 };
using (var numberRepository = new NumberRepository(this))
{
for (var i = 0; i <= 100; i++)
{
var number = new Number()
{
Identifier = Guid.NewGuid(),
Value = i
};
numberRepository.Add(number);
}
SaveChanges();
using (var numberSeriesRepository = new NumberSeriesRepository(this))
{
numberSeriesRepository.Add(NumberSeries.Named.Fibonacci);
SaveChanges();
using (var numberSeriesNumberRepository = new NumberSeriesNumberRepository(this))
{
var fibonacciNumbers = numberRepository.FindWhere(entity => fibonacciNumberSeriesValues.Contains(entity.Value));
foreach (var fibonacciNumber in fibonacciNumbers)
{
var fibonacciNumberSeriesNumber = new NumberSeriesNumber()
{
Identifier = Guid.NewGuid(),
NumberIdentifier = fibonacciNumber.Identifier,
NumberSeriesIdentifier = NumberSeries.Named.Fibonacci.Identifier
};
numberSeriesNumberRepository.Add(fibonacciNumberSeriesNumber);
}
SaveChanges();
}
}
}
return this;
}
/// <summary>
/// Configures the in-memory database to be used for this context.
/// </summary>
/// <param name="applicationConfiguration">
/// Configuration information for the application.
/// </param>
/// <param name="optionsBuilder">
/// A builder that is used to create or modify options for this context.
/// </param>
protected sealed override void OnConfiguringInMemory(IConfiguration applicationConfiguration, InMemoryDbContextOptionsBuilder optionsBuilder) => base.OnConfiguringInMemory(applicationConfiguration, optionsBuilder);
/// <summary>
/// Configures the SQL Server database to be used for this context.
/// </summary>
/// <param name="applicationConfiguration">
/// Configuration information for the application.
/// </param>
/// <param name="optionsBuilder">
/// A builder that is used to create or modify options for this context.
/// </param>
protected sealed override void OnConfiguringSqlServer(IConfiguration applicationConfiguration, SqlServerDbContextOptionsBuilder optionsBuilder) => throw new InvalidOperationException();
}
}