/
ContextExtensions.cs
102 lines (92 loc) · 3.97 KB
/
ContextExtensions.cs
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
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ContextExtensions.cs" company="Kephas Software SRL">
// Copyright (c) Kephas Software SRL. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
// </copyright>
// <summary>
// Implements the context extensions class.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace Kephas.Data
{
using System;
using System.Collections.Generic;
using System.Linq;
using Kephas.Data.Capabilities;
using Kephas.Services;
/// <summary>
/// Extension methods for <see cref="IContext"/>.
/// </summary>
public static class ContextExtensions
{
/// <summary>
/// Name of the initial data configuration.
/// </summary>
public const string InitialDataConfigurationName = "InitialData";
/// <summary>
/// Gets the initial data.
/// </summary>
/// <param name="context">The context to act on.</param>
/// <returns>
/// An enumeration of entity entry.
/// </returns>
public static IEnumerable<IChangeStateTrackableEntityEntry>? InitialData(this IContext? context)
{
return context?[InitialDataConfigurationName] as IEnumerable<IChangeStateTrackableEntityEntry>;
}
/// <summary>
/// Sets the initial data.
/// </summary>
/// <typeparam name="TContext">Type of the context.</typeparam>
/// <param name="context">The context to act on.</param>
/// <param name="initialData">The initial data.</param>
/// <returns>
/// This context.
/// </returns>
public static TContext InitialData<TContext>(this TContext context, IEnumerable<(object entity, ChangeState changeState)> initialData)
where TContext : class, IContext
{
context = context ?? throw new ArgumentNullException(nameof(context));
context[InitialDataConfigurationName] =
initialData
?.Where(t => t.entity != null)
.Select(t => new EntityEntry(t.entity) { ChangeState = t.changeState });
return context;
}
/// <summary>
/// Sets the initial data.
/// </summary>
/// <typeparam name="TContext">Type of the context.</typeparam>
/// <param name="context">The context to act on.</param>
/// <param name="initialData">The initial data as an enumeration of entities.</param>
/// <returns>
/// This context.
/// </returns>
public static TContext InitialData<TContext>(this TContext context, IEnumerable<object> initialData)
where TContext : class, IContext
{
context = context ?? throw new ArgumentNullException(nameof(context));
context[InitialDataConfigurationName] =
initialData
?.Where(o => o != null)
.Select(o => new EntityEntry(o));
return context;
}
/// <summary>
/// Sets the initial data.
/// </summary>
/// <typeparam name="TContext">Type of the context.</typeparam>
/// <param name="context">The context to act on.</param>
/// <param name="initialData">The initial data as an enumeration of entities.</param>
/// <returns>
/// This context.
/// </returns>
public static TContext InitialData<TContext>(this TContext context, IEnumerable<IChangeStateTrackableEntityEntry> initialData)
where TContext : class, IContext
{
context = context ?? throw new ArgumentNullException(nameof(context));
context[InitialDataConfigurationName] = initialData?.Where(e => e != null);
return context;
}
}
}