Skip to content

Commit

Permalink
added initialization of store manager, file manager and serilizer wit…
Browse files Browse the repository at this point in the history
…h dependency injection
  • Loading branch information
morrisjdev committed Jun 7, 2020
1 parent 5636fb8 commit 3cdd864
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 42 deletions.
2 changes: 0 additions & 2 deletions Example/Data/Context.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using Example.Data.Entities;
using FileContextCore;
using FileContextCore.StoreManager;
using Microsoft.EntityFrameworkCore;
using OfficeOpenXml;

namespace Example.Data
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public static DbContextOptionsBuilder UseFileContextDatabase(
?? new FileContextOptionsExtension();

extension = extension.WithCustomOptions(databaseName, location, password,
typeof(DefaultStoreManager<JSONSerializer, DefaultFileManager>));
typeof(DefaultStoreManager), typeof(JSONSerializer), typeof(DefaultFileManager));

if (databaseRoot != null)
{
Expand Down Expand Up @@ -183,7 +183,7 @@ public static DbContextOptionsBuilder UseFileContextDatabase<TSerializer, TFileM
?? new FileContextOptionsExtension();

extension = extension.WithCustomOptions(databaseName, location, password,
typeof(DefaultStoreManager<TSerializer, TFileManager>));
typeof(DefaultStoreManager), typeof(TSerializer), typeof(TFileManager));

if (databaseRoot != null)
{
Expand Down Expand Up @@ -270,7 +270,7 @@ public static DbContextOptionsBuilder UseFileContextDatabase<TStoreManager>(
var extension = optionsBuilder.Options.FindExtension<FileContextOptionsExtension>()
?? new FileContextOptionsExtension();

extension = extension.WithCustomOptions(databaseName, location, password, typeof(TStoreManager));
extension = extension.WithCustomOptions(databaseName, location, password, typeof(TStoreManager), null, null);

if (databaseRoot != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using FileContextCore.Diagnostics.Internal;
using FileContextCore.FileManager;
using FileContextCore.Infrastructure.Internal;
using FileContextCore.Metadata.Conventions;
using FileContextCore.Query.Internal;
using FileContextCore.Serializer;
using FileContextCore.Storage.Internal;
using FileContextCore.StoreManager;
using FileContextCore.ValueGeneration.Internal;
using FileContextCore.Utilities;
using JetBrains.Annotations;
Expand Down Expand Up @@ -70,7 +73,16 @@ public static IServiceCollection AddEntityFrameworkFileContextDatabase([NotNull]
b => b
.TryAddSingleton<IFileContextSingletonOptions, FileContextSingletonOptions>()
.TryAddSingleton<IFileContextStoreCache, FileContextStoreCache>()
.TryAddScoped<IFileContextDatabase, FileContextDatabase>());
.TryAddScoped<IFileContextDatabase, FileContextDatabase>()
.TryAddTransient<EXCELStoreManager, EXCELStoreManager>()
.TryAddTransient<DefaultStoreManager, DefaultStoreManager>()
.TryAddTransient<BSONSerializer, BSONSerializer>()
.TryAddTransient<CSVSerializer, CSVSerializer>()
.TryAddTransient<JSONSerializer, JSONSerializer>()
.TryAddTransient<XMLSerializer, XMLSerializer>()
.TryAddTransient<DefaultFileManager, DefaultFileManager>()
.TryAddTransient<EncryptedFileManager, EncryptedFileManager>()
.TryAddTransient<PrivateFileManager, PrivateFileManager>());

builder.TryAddCoreServices();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class FileContextOptionsExtension : IDbContextOptionsExtension
public FileContextOptionsExtension()
{
_options = new FileContextScopedOptions(null, null, null,
typeof(DefaultStoreManager<JSONSerializer, DefaultFileManager>));
typeof(DefaultStoreManager), typeof(JSONSerializer), typeof(DefaultFileManager));
}


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


public virtual FileContextOptionsExtension WithCustomOptions(string databaseName, string location, string password, Type storeManagerType)
public virtual FileContextOptionsExtension WithCustomOptions(string databaseName, string location, string password, Type storeManagerType, Type serializerType, Type fileManagerType)
{
var clone = Clone();
clone._options = new FileContextScopedOptions(databaseName, location, password, storeManagerType);
clone._options = new FileContextScopedOptions(databaseName, location, password, storeManagerType, serializerType, fileManagerType);
return clone;
}

Expand Down Expand Up @@ -105,6 +105,9 @@ public override string LogFragment
builder.Append("Location=").Append(Extension.Options.Location).Append(' ');
builder.Append("DatabaseName=").Append(Extension.Options.DatabaseName).Append(' ');
builder.Append("StoreManager=").Append(Extension.Options.StoreManagerType).Append(' ');
builder.Append("Serializer=").Append(Extension.Options.SerializerType).Append(' ');
builder.Append("FileManager=").Append(Extension.Options.FileManagerType).Append(' ');
builder.Append("StoreManager=").Append(Extension.Options.StoreManagerType).Append(' ');
builder.Append("Password=").Append("<Password>").Append(' ');

_logFragment = builder.ToString();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.EntityFrameworkCore.Infrastructure;

namespace FileContextCore.Infrastructure.Internal
{
public class FileContextScopedOptions : IFileContextScopedOptions
{
public FileContextScopedOptions(string databaseName, string location, string password, Type storeManagerType)
public FileContextScopedOptions(string databaseName, string location, string password, Type storeManagerType, Type serializerType, Type fileManagerType)
{
DatabaseName = databaseName;
Location = location;
Password = password;
StoreManagerType = storeManagerType;
SerializerType = serializerType;
FileManagerType = fileManagerType;
}

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

public Type StoreManagerType { get; }

public Type SerializerType { get; }

public Type FileManagerType { get; }

public override int GetHashCode()
{
return (DatabaseName + Location + Password + StoreManagerType).GetHashCode();
return (DatabaseName + Location + Password + StoreManagerType + SerializerType + FileManagerType).GetHashCode();
}

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

FileContextScopedOptions optionsCompare = (FileContextScopedOptions) obj;
return optionsCompare.DatabaseName == DatabaseName && optionsCompare.Location == Location &&
optionsCompare.Password == Password && optionsCompare.StoreManagerType == StoreManagerType;
optionsCompare.Password == Password && optionsCompare.StoreManagerType == StoreManagerType &&
optionsCompare.SerializerType == SerializerType && optionsCompare.FileManagerType == FileManagerType;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@ public interface IFileContextScopedOptions
string Location { get; }
string Password { get; }
Type StoreManagerType { get; }
Type SerializerType { get; }
Type FileManagerType { get; }
}
}
9 changes: 4 additions & 5 deletions FileContextCore/Serializer/JSONSerializer.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using Microsoft.EntityFrameworkCore.Metadata;
using Newtonsoft.Json.Linq;
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using FileContextCore.Infrastructure.Internal;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking.Internal;
using Microsoft.EntityFrameworkCore.Metadata;
using Newtonsoft.Json.Linq;

namespace FileContextCore.Serializer
{
Expand All @@ -15,7 +14,7 @@ public class JSONSerializer : ISerializer
private object _keyValueFactory;
private string[] _propertyKeys;
private Type[] _typeList;

public void Initialize(IFileContextScopedOptions _, IEntityType entityType, object keyValueFactory)
{
_keyValueFactory = keyValueFactory;
Expand Down
8 changes: 6 additions & 2 deletions FileContextCore/Storage/Internal/FileContextStoreCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// Modified version by morrisjdev
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Concurrent;
using System.Threading;
using FileContextCore.Infrastructure.Internal;
Expand All @@ -15,15 +16,18 @@ namespace FileContextCore.Storage.Internal
public class FileContextStoreCache : IFileContextStoreCache
{
[NotNull] private readonly ILoggingOptions _loggingOptions;
private readonly IServiceProvider _serviceProvider;
private readonly bool _useNameMatching;
private readonly ConcurrentDictionary<IFileContextScopedOptions, IFileContextStore> _namedStores;


public FileContextStoreCache(
[NotNull] ILoggingOptions loggingOptions,
[CanBeNull] IFileContextSingletonOptions options)
[CanBeNull] IFileContextSingletonOptions options,
IServiceProvider serviceProvider)
{
_loggingOptions = loggingOptions;
_serviceProvider = serviceProvider;
if (options?.DatabaseRoot != null)
{
_useNameMatching = true;
Expand All @@ -43,7 +47,7 @@ public FileContextStoreCache(

public virtual IFileContextStore GetStore(IFileContextScopedOptions options)
{
return _namedStores.GetOrAdd(options, _ => new FileContextStore(new FileContextTableFactory(_loggingOptions, options), _useNameMatching));
return _namedStores.GetOrAdd(options, _ => new FileContextStore(new FileContextTableFactory(_loggingOptions, options, _serviceProvider), _useNameMatching));
}
}
}
7 changes: 5 additions & 2 deletions FileContextCore/Storage/Internal/FileContextTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class FileContextTable<TKey> : IFileContextTable
private readonly bool _sensitiveLoggingEnabled;
private readonly IEntityType _entityType;
private readonly IFileContextScopedOptions _options;
private readonly IServiceProvider _serviceProvider;
private readonly Dictionary<TKey, object[]> _rows;

private IStoreManager _storeManager;
Expand All @@ -44,12 +45,14 @@ public FileContextTable(
[NotNull] Microsoft.EntityFrameworkCore.ChangeTracking.Internal.IPrincipalKeyValueFactory<TKey> keyValueFactory,
bool sensitiveLoggingEnabled,
IEntityType entityType,
IFileContextScopedOptions options)
IFileContextScopedOptions options,
IServiceProvider serviceProvider)
{
_keyValueFactory = keyValueFactory;
_sensitiveLoggingEnabled = sensitiveLoggingEnabled;
_entityType = entityType;
_options = options;
_serviceProvider = serviceProvider;

_rows = Init();
}
Expand Down Expand Up @@ -242,7 +245,7 @@ protected virtual void ThrowUpdateConcurrencyException([NotNull] IUpdateEntry en

private Dictionary<TKey, object[]> Init()
{
_storeManager = (IStoreManager)Activator.CreateInstance(_options.StoreManagerType);
_storeManager = (IStoreManager)_serviceProvider.GetService(_options.StoreManagerType);
_storeManager.Initialize(_options, _entityType, _keyValueFactory);

Dictionary<TKey, object[]> newList = new Dictionary<TKey, object[]>(_keyValueFactory.EqualityComparer);
Expand Down
21 changes: 11 additions & 10 deletions FileContextCore/Storage/Internal/FileContextTableFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,34 @@

using System;
using System.Collections.Concurrent;
using System.Linq;
using System.Reflection;
using FileContextCore.Infrastructure.Internal;
using FileContextCore.Utilities;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking.Internal;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Utilities;
using Microsoft.EntityFrameworkCore.Metadata.Internal;

namespace FileContextCore.Storage.Internal
{

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

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


public FileContextTableFactory([NotNull] ILoggingOptions loggingOptions, [NotNull] IFileContextScopedOptions options)
public FileContextTableFactory([NotNull] ILoggingOptions loggingOptions, [NotNull] IFileContextScopedOptions options, IServiceProvider serviceProvider)
{
_options = options;
_serviceProvider = serviceProvider;
Check.NotNull(loggingOptions, nameof(loggingOptions));

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

[UsedImplicitly]
private static Func<IFileContextTable> CreateFactory<TKey>(IKey key, IEntityType entityType, bool sensitiveLoggingEnabled, IFileContextScopedOptions options)
private static Func<IFileContextTable> CreateFactory<TKey>(IKey key, IEntityType entityType, bool sensitiveLoggingEnabled, IFileContextScopedOptions options, IServiceProvider serviceProvider)
=> () => new FileContextTable<TKey>(
// WARNING: The in-memory provider is using EF internal code here. This should not be copied by other providers. See #15096
Microsoft.EntityFrameworkCore.Metadata.Internal.KeyExtensions.GetPrincipalKeyValueFactory<TKey>(key),
KeyExtensions.GetPrincipalKeyValueFactory<TKey>(key),
sensitiveLoggingEnabled,
entityType,
options);
options,
serviceProvider);
}
}
19 changes: 11 additions & 8 deletions FileContextCore/StoreManager/DefaultStoreManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,28 @@

namespace FileContextCore.StoreManager
{
public class DefaultStoreManager<TSerializer, TFileManager> : IStoreManager
where TSerializer : ISerializer
where TFileManager : IFileManager
{
class DefaultStoreManager : IStoreManager {
private readonly IServiceProvider _serviceProvider;
private ISerializer _serializer;
private IFileManager _fileManager;
private object _keyValueFactory;
private IEntityType _entityType;

public DefaultStoreManager(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
}

public void Initialize(IFileContextScopedOptions options, IEntityType entityType,
object keyValueFactory)
{
_keyValueFactory = keyValueFactory;
_entityType = entityType;
_serializer = (ISerializer)Activator.CreateInstance(typeof(TSerializer));

_serializer = (ISerializer)_serviceProvider.GetService(options.SerializerType);
_serializer.Initialize(options, _entityType, _keyValueFactory);
_fileManager = (IFileManager)Activator.CreateInstance(typeof(TFileManager));

_fileManager = (IFileManager)_serviceProvider.GetService(options.FileManagerType);
_fileManager.Initialize(options, entityType, _serializer.FileType);
}

Expand Down

0 comments on commit 3cdd864

Please sign in to comment.