-
Notifications
You must be signed in to change notification settings - Fork 5
/
DataSpace.cs
193 lines (174 loc) · 8.11 KB
/
DataSpace.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="DataSpace.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 data context collection class.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace Kephas.Data
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Kephas.Collections;
using Kephas.Data.Store;
using Kephas.Services;
using Kephas.Reflection;
using Kephas.Services;
/// <summary>
/// Container class for data contexts indexed by contained entity types.
/// </summary>
public class DataSpace : Context, IDataSpace
{
private readonly IDataContextFactory dataContextFactory;
private readonly IDataStoreProvider dataStoreProvider;
private IContext? initializationContext;
private IDictionary<string, IDataContext> dataContextMap = new Dictionary<string, IDataContext>();
/// <summary>
/// Initializes a new instance of the <see cref="DataSpace"/> class.
/// </summary>
/// <param name="serviceProvider">The injector.</param>
/// <param name="dataContextFactory">The data context factory.</param>
/// <param name="dataStoreProvider">The data store provider.</param>
public DataSpace(
IServiceProvider serviceProvider,
IDataContextFactory dataContextFactory,
IDataStoreProvider dataStoreProvider)
: this(serviceProvider, dataContextFactory, dataStoreProvider, false)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="DataSpace"/> class.
/// </summary>
/// <param name="serviceProvider">The injector.</param>
/// <param name="dataContextFactory">The data context factory.</param>
/// <param name="dataStoreProvider">The data store provider.</param>
/// <param name="isThreadSafe">True if this object is thread safe.</param>
protected DataSpace(
IServiceProvider serviceProvider,
IDataContextFactory dataContextFactory,
IDataStoreProvider dataStoreProvider,
bool isThreadSafe)
: base(serviceProvider, isThreadSafe)
{
dataContextFactory = dataContextFactory ?? throw new System.ArgumentNullException(nameof(dataContextFactory));
dataStoreProvider = dataStoreProvider ?? throw new ArgumentNullException(nameof(dataStoreProvider));
this.dataContextFactory = dataContextFactory;
this.dataStoreProvider = dataStoreProvider;
}
/// <summary>Gets the number of elements in the collection.</summary>
/// <returns>The number of elements in the collection. </returns>
public int Count => this.dataContextMap.Count;
/// <summary>
/// Gets the data context for the provided entity type.
/// </summary>
/// <param name="entityType">Type of the entity.</param>
/// <returns>
/// The data context.
/// </returns>
IDataContext IDataSpace.this[Type entityType] => this.GetDataContext(entityType);
/// <summary>
/// Gets the data context for the provided entity type.
/// </summary>
/// <param name="entityType">Type of the entity.</param>
/// <returns>
/// The data context.
/// </returns>
IDataContext IDataSpace.this[ITypeInfo entityType] => this.GetDataContext(entityType);
/// <summary>Returns an enumerator that iterates through the collection.</summary>
/// <returns>An enumerator that can be used to iterate through the collection.</returns>
public IEnumerator<IDataContext> GetEnumerator()
{
return this.dataContextMap.Values.GetEnumerator();
}
/// <summary>Returns an enumerator that iterates through a collection.</summary>
/// <returns>An <see cref="T:System.Collections.IEnumerator" /> object that can be used to iterate through the collection.</returns>
IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
/// <summary>
/// Initializes the service.
/// </summary>
/// <param name="context">An optional context for initialization.</param>
public virtual void Initialize(IContext? context = null)
{
this.initializationContext = context;
if (this.Identity == null)
{
this.Identity = context?.Identity;
}
this.ClearDataContextMap();
var entityEntries = context?.InitialData();
if (entityEntries != null)
{
this.dataContextMap = entityEntries
.GroupBy(e => this.dataStoreProvider.GetDataStoreName(e.Entity.GetType(), this.initializationContext), e => e)
.ToDictionary(
g => g.Key,
g =>
{
var initContext = new Context(this.ServiceProvider)
{
Identity = this.Identity,
}.InitialData(g);
return this.dataContextFactory.CreateDataContext(
g.Key,
initContext);
});
}
}
/// <summary>
/// Releases the unmanaged resources used by the Kephas.Services.Context and optionally releases
/// the managed resources.
/// </summary>
/// <param name="disposing">True to release both managed and unmanaged resources; false to
/// release only unmanaged resources.</param>
protected override void Dispose(bool disposing)
{
this.ClearDataContextMap();
base.Dispose(disposing);
}
/// <summary>
/// Gets the data context for the provided entity type.
/// </summary>
/// <param name="entityType">Type of the entity.</param>
/// <returns>
/// The data context.
/// </returns>
protected virtual IDataContext GetDataContext(ITypeInfo entityType) =>
this.GetDataContext(entityType.AsType());
/// <summary>
/// Gets the data context for the provided entity type.
/// </summary>
/// <param name="entityType">Type of the entity.</param>
/// <returns>
/// The data context.
/// </returns>
protected virtual IDataContext GetDataContext(Type entityType)
{
var dataStoreName = this.dataStoreProvider.GetDataStoreName(entityType, this.initializationContext);
var dataContext = this.dataContextMap.TryGetValue(dataStoreName);
if (dataContext == null)
{
dataContext = this.dataContextFactory.CreateDataContext(dataStoreName, this.initializationContext);
this.dataContextMap.Add(dataStoreName, dataContext);
}
return dataContext;
}
/// <summary>
/// Clears the data context map.
/// </summary>
protected virtual void ClearDataContextMap()
{
foreach (var dataContext in this.dataContextMap.Values)
{
dataContext.Dispose();
}
this.dataContextMap.Clear();
}
}
}