Skip to content

Commit 3cdd864

Browse files
committed
added initialization of store manager, file manager and serilizer with dependency injection
1 parent 5636fb8 commit 3cdd864

File tree

11 files changed

+71
-42
lines changed

11 files changed

+71
-42
lines changed

Example/Data/Context.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
using Example.Data.Entities;
22
using FileContextCore;
3-
using FileContextCore.StoreManager;
43
using Microsoft.EntityFrameworkCore;
5-
using OfficeOpenXml;
64

75
namespace Example.Data
86
{

FileContextCore/Extensions/FileContextDbContextOptionsExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public static DbContextOptionsBuilder UseFileContextDatabase(
9191
?? new FileContextOptionsExtension();
9292

9393
extension = extension.WithCustomOptions(databaseName, location, password,
94-
typeof(DefaultStoreManager<JSONSerializer, DefaultFileManager>));
94+
typeof(DefaultStoreManager), typeof(JSONSerializer), typeof(DefaultFileManager));
9595

9696
if (databaseRoot != null)
9797
{
@@ -183,7 +183,7 @@ public static DbContextOptionsBuilder UseFileContextDatabase<TSerializer, TFileM
183183
?? new FileContextOptionsExtension();
184184

185185
extension = extension.WithCustomOptions(databaseName, location, password,
186-
typeof(DefaultStoreManager<TSerializer, TFileManager>));
186+
typeof(DefaultStoreManager), typeof(TSerializer), typeof(TFileManager));
187187

188188
if (databaseRoot != null)
189189
{
@@ -270,7 +270,7 @@ public static DbContextOptionsBuilder UseFileContextDatabase<TStoreManager>(
270270
var extension = optionsBuilder.Options.FindExtension<FileContextOptionsExtension>()
271271
?? new FileContextOptionsExtension();
272272

273-
extension = extension.WithCustomOptions(databaseName, location, password, typeof(TStoreManager));
273+
extension = extension.WithCustomOptions(databaseName, location, password, typeof(TStoreManager), null, null);
274274

275275
if (databaseRoot != null)
276276
{

FileContextCore/Extensions/FileContextServiceCollectionExtensions.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
55

66
using FileContextCore.Diagnostics.Internal;
7+
using FileContextCore.FileManager;
78
using FileContextCore.Infrastructure.Internal;
89
using FileContextCore.Metadata.Conventions;
910
using FileContextCore.Query.Internal;
11+
using FileContextCore.Serializer;
1012
using FileContextCore.Storage.Internal;
13+
using FileContextCore.StoreManager;
1114
using FileContextCore.ValueGeneration.Internal;
1215
using FileContextCore.Utilities;
1316
using JetBrains.Annotations;
@@ -70,7 +73,16 @@ public static IServiceCollection AddEntityFrameworkFileContextDatabase([NotNull]
7073
b => b
7174
.TryAddSingleton<IFileContextSingletonOptions, FileContextSingletonOptions>()
7275
.TryAddSingleton<IFileContextStoreCache, FileContextStoreCache>()
73-
.TryAddScoped<IFileContextDatabase, FileContextDatabase>());
76+
.TryAddScoped<IFileContextDatabase, FileContextDatabase>()
77+
.TryAddTransient<EXCELStoreManager, EXCELStoreManager>()
78+
.TryAddTransient<DefaultStoreManager, DefaultStoreManager>()
79+
.TryAddTransient<BSONSerializer, BSONSerializer>()
80+
.TryAddTransient<CSVSerializer, CSVSerializer>()
81+
.TryAddTransient<JSONSerializer, JSONSerializer>()
82+
.TryAddTransient<XMLSerializer, XMLSerializer>()
83+
.TryAddTransient<DefaultFileManager, DefaultFileManager>()
84+
.TryAddTransient<EncryptedFileManager, EncryptedFileManager>()
85+
.TryAddTransient<PrivateFileManager, PrivateFileManager>());
7486

7587
builder.TryAddCoreServices();
7688

FileContextCore/Infrastructure/Internal/FileContextOptionsExtension.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public class FileContextOptionsExtension : IDbContextOptionsExtension
2828
public FileContextOptionsExtension()
2929
{
3030
_options = new FileContextScopedOptions(null, null, null,
31-
typeof(DefaultStoreManager<JSONSerializer, DefaultFileManager>));
31+
typeof(DefaultStoreManager), typeof(JSONSerializer), typeof(DefaultFileManager));
3232
}
3333

3434

@@ -49,10 +49,10 @@ public virtual DbContextOptionsExtensionInfo Info
4949
public virtual FileContextScopedOptions Options => _options;
5050

5151

52-
public virtual FileContextOptionsExtension WithCustomOptions(string databaseName, string location, string password, Type storeManagerType)
52+
public virtual FileContextOptionsExtension WithCustomOptions(string databaseName, string location, string password, Type storeManagerType, Type serializerType, Type fileManagerType)
5353
{
5454
var clone = Clone();
55-
clone._options = new FileContextScopedOptions(databaseName, location, password, storeManagerType);
55+
clone._options = new FileContextScopedOptions(databaseName, location, password, storeManagerType, serializerType, fileManagerType);
5656
return clone;
5757
}
5858

@@ -105,6 +105,9 @@ public override string LogFragment
105105
builder.Append("Location=").Append(Extension.Options.Location).Append(' ');
106106
builder.Append("DatabaseName=").Append(Extension.Options.DatabaseName).Append(' ');
107107
builder.Append("StoreManager=").Append(Extension.Options.StoreManagerType).Append(' ');
108+
builder.Append("Serializer=").Append(Extension.Options.SerializerType).Append(' ');
109+
builder.Append("FileManager=").Append(Extension.Options.FileManagerType).Append(' ');
110+
builder.Append("StoreManager=").Append(Extension.Options.StoreManagerType).Append(' ');
108111
builder.Append("Password=").Append("<Password>").Append(' ');
109112

110113
_logFragment = builder.ToString();
Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Text;
4-
using Microsoft.EntityFrameworkCore.Infrastructure;
52

63
namespace FileContextCore.Infrastructure.Internal
74
{
85
public class FileContextScopedOptions : IFileContextScopedOptions
96
{
10-
public FileContextScopedOptions(string databaseName, string location, string password, Type storeManagerType)
7+
public FileContextScopedOptions(string databaseName, string location, string password, Type storeManagerType, Type serializerType, Type fileManagerType)
118
{
129
DatabaseName = databaseName;
1310
Location = location;
1411
Password = password;
1512
StoreManagerType = storeManagerType;
13+
SerializerType = serializerType;
14+
FileManagerType = fileManagerType;
1615
}
1716

1817
public string DatabaseName { get; }
@@ -22,10 +21,14 @@ public FileContextScopedOptions(string databaseName, string location, string pas
2221
public string Password { get; }
2322

2423
public Type StoreManagerType { get; }
24+
25+
public Type SerializerType { get; }
26+
27+
public Type FileManagerType { get; }
2528

2629
public override int GetHashCode()
2730
{
28-
return (DatabaseName + Location + Password + StoreManagerType).GetHashCode();
31+
return (DatabaseName + Location + Password + StoreManagerType + SerializerType + FileManagerType).GetHashCode();
2932
}
3033

3134
public override bool Equals(object obj)
@@ -37,7 +40,8 @@ public override bool Equals(object obj)
3740

3841
FileContextScopedOptions optionsCompare = (FileContextScopedOptions) obj;
3942
return optionsCompare.DatabaseName == DatabaseName && optionsCompare.Location == Location &&
40-
optionsCompare.Password == Password && optionsCompare.StoreManagerType == StoreManagerType;
43+
optionsCompare.Password == Password && optionsCompare.StoreManagerType == StoreManagerType &&
44+
optionsCompare.SerializerType == SerializerType && optionsCompare.FileManagerType == FileManagerType;
4145
}
4246
}
4347
}

FileContextCore/Infrastructure/Internal/IFileContextScopedOptions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,7 @@ public interface IFileContextScopedOptions
88
string Location { get; }
99
string Password { get; }
1010
Type StoreManagerType { get; }
11+
Type SerializerType { get; }
12+
Type FileManagerType { get; }
1113
}
1214
}

FileContextCore/Serializer/JSONSerializer.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
using Microsoft.EntityFrameworkCore.Metadata;
2-
using Newtonsoft.Json.Linq;
3-
using System;
1+
using System;
42
using System.Collections.Generic;
53
using System.Linq;
64
using FileContextCore.Infrastructure.Internal;
75
using Microsoft.EntityFrameworkCore;
8-
using Microsoft.EntityFrameworkCore.ChangeTracking.Internal;
6+
using Microsoft.EntityFrameworkCore.Metadata;
7+
using Newtonsoft.Json.Linq;
98

109
namespace FileContextCore.Serializer
1110
{
@@ -15,7 +14,7 @@ public class JSONSerializer : ISerializer
1514
private object _keyValueFactory;
1615
private string[] _propertyKeys;
1716
private Type[] _typeList;
18-
17+
1918
public void Initialize(IFileContextScopedOptions _, IEntityType entityType, object keyValueFactory)
2019
{
2120
_keyValueFactory = keyValueFactory;

FileContextCore/Storage/Internal/FileContextStoreCache.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// Modified version by morrisjdev
44
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
55

6+
using System;
67
using System.Collections.Concurrent;
78
using System.Threading;
89
using FileContextCore.Infrastructure.Internal;
@@ -15,15 +16,18 @@ namespace FileContextCore.Storage.Internal
1516
public class FileContextStoreCache : IFileContextStoreCache
1617
{
1718
[NotNull] private readonly ILoggingOptions _loggingOptions;
19+
private readonly IServiceProvider _serviceProvider;
1820
private readonly bool _useNameMatching;
1921
private readonly ConcurrentDictionary<IFileContextScopedOptions, IFileContextStore> _namedStores;
2022

2123

2224
public FileContextStoreCache(
2325
[NotNull] ILoggingOptions loggingOptions,
24-
[CanBeNull] IFileContextSingletonOptions options)
26+
[CanBeNull] IFileContextSingletonOptions options,
27+
IServiceProvider serviceProvider)
2528
{
2629
_loggingOptions = loggingOptions;
30+
_serviceProvider = serviceProvider;
2731
if (options?.DatabaseRoot != null)
2832
{
2933
_useNameMatching = true;
@@ -43,7 +47,7 @@ public FileContextStoreCache(
4347

4448
public virtual IFileContextStore GetStore(IFileContextScopedOptions options)
4549
{
46-
return _namedStores.GetOrAdd(options, _ => new FileContextStore(new FileContextTableFactory(_loggingOptions, options), _useNameMatching));
50+
return _namedStores.GetOrAdd(options, _ => new FileContextStore(new FileContextTableFactory(_loggingOptions, options, _serviceProvider), _useNameMatching));
4751
}
4852
}
4953
}

FileContextCore/Storage/Internal/FileContextTable.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public class FileContextTable<TKey> : IFileContextTable
3333
private readonly bool _sensitiveLoggingEnabled;
3434
private readonly IEntityType _entityType;
3535
private readonly IFileContextScopedOptions _options;
36+
private readonly IServiceProvider _serviceProvider;
3637
private readonly Dictionary<TKey, object[]> _rows;
3738

3839
private IStoreManager _storeManager;
@@ -44,12 +45,14 @@ public FileContextTable(
4445
[NotNull] Microsoft.EntityFrameworkCore.ChangeTracking.Internal.IPrincipalKeyValueFactory<TKey> keyValueFactory,
4546
bool sensitiveLoggingEnabled,
4647
IEntityType entityType,
47-
IFileContextScopedOptions options)
48+
IFileContextScopedOptions options,
49+
IServiceProvider serviceProvider)
4850
{
4951
_keyValueFactory = keyValueFactory;
5052
_sensitiveLoggingEnabled = sensitiveLoggingEnabled;
5153
_entityType = entityType;
5254
_options = options;
55+
_serviceProvider = serviceProvider;
5356

5457
_rows = Init();
5558
}
@@ -242,7 +245,7 @@ protected virtual void ThrowUpdateConcurrencyException([NotNull] IUpdateEntry en
242245

243246
private Dictionary<TKey, object[]> Init()
244247
{
245-
_storeManager = (IStoreManager)Activator.CreateInstance(_options.StoreManagerType);
248+
_storeManager = (IStoreManager)_serviceProvider.GetService(_options.StoreManagerType);
246249
_storeManager.Initialize(_options, _entityType, _keyValueFactory);
247250

248251
Dictionary<TKey, object[]> newList = new Dictionary<TKey, object[]>(_keyValueFactory.EqualityComparer);

FileContextCore/Storage/Internal/FileContextTableFactory.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,34 @@
55

66
using System;
77
using System.Collections.Concurrent;
8-
using System.Linq;
98
using System.Reflection;
109
using FileContextCore.Infrastructure.Internal;
1110
using FileContextCore.Utilities;
1211
using JetBrains.Annotations;
13-
using Microsoft.EntityFrameworkCore;
12+
using Microsoft.EntityFrameworkCore.ChangeTracking.Internal;
1413
using Microsoft.EntityFrameworkCore.Diagnostics;
15-
using Microsoft.EntityFrameworkCore.Infrastructure;
1614
using Microsoft.EntityFrameworkCore.Metadata;
17-
using Microsoft.EntityFrameworkCore.Utilities;
15+
using Microsoft.EntityFrameworkCore.Metadata.Internal;
1816

1917
namespace FileContextCore.Storage.Internal
2018
{
2119

2220
public class FileContextTableFactory
2321
// WARNING: The in-memory provider is using EF internal code here. This should not be copied by other providers. See #15096
24-
: Microsoft.EntityFrameworkCore.ChangeTracking.Internal.IdentityMapFactoryFactoryBase, IFileContextTableFactory
22+
: IdentityMapFactoryFactoryBase, IFileContextTableFactory
2523
{
2624
private readonly IFileContextScopedOptions _options;
25+
private readonly IServiceProvider _serviceProvider;
2726
private readonly bool _sensitiveLoggingEnabled;
2827

2928
private readonly ConcurrentDictionary<IKey, Func<IFileContextTable>> _factories
3029
= new ConcurrentDictionary<IKey, Func<IFileContextTable>>();
3130

3231

33-
public FileContextTableFactory([NotNull] ILoggingOptions loggingOptions, [NotNull] IFileContextScopedOptions options)
32+
public FileContextTableFactory([NotNull] ILoggingOptions loggingOptions, [NotNull] IFileContextScopedOptions options, IServiceProvider serviceProvider)
3433
{
3534
_options = options;
35+
_serviceProvider = serviceProvider;
3636
Check.NotNull(loggingOptions, nameof(loggingOptions));
3737

3838
_sensitiveLoggingEnabled = loggingOptions.IsSensitiveDataLoggingEnabled;
@@ -46,15 +46,16 @@ private Func<IFileContextTable> Create([NotNull] IKey key)
4646
=> (Func<IFileContextTable>)typeof(FileContextTableFactory).GetTypeInfo()
4747
.GetDeclaredMethod(nameof(CreateFactory))
4848
.MakeGenericMethod(GetKeyType(key))
49-
.Invoke(null, new object[] { key, key.DeclaringEntityType, _sensitiveLoggingEnabled, _options });
49+
.Invoke(null, new object[] { key, key.DeclaringEntityType, _sensitiveLoggingEnabled, _options, _serviceProvider });
5050

5151
[UsedImplicitly]
52-
private static Func<IFileContextTable> CreateFactory<TKey>(IKey key, IEntityType entityType, bool sensitiveLoggingEnabled, IFileContextScopedOptions options)
52+
private static Func<IFileContextTable> CreateFactory<TKey>(IKey key, IEntityType entityType, bool sensitiveLoggingEnabled, IFileContextScopedOptions options, IServiceProvider serviceProvider)
5353
=> () => new FileContextTable<TKey>(
5454
// WARNING: The in-memory provider is using EF internal code here. This should not be copied by other providers. See #15096
55-
Microsoft.EntityFrameworkCore.Metadata.Internal.KeyExtensions.GetPrincipalKeyValueFactory<TKey>(key),
55+
KeyExtensions.GetPrincipalKeyValueFactory<TKey>(key),
5656
sensitiveLoggingEnabled,
5757
entityType,
58-
options);
58+
options,
59+
serviceProvider);
5960
}
6061
}

0 commit comments

Comments
 (0)