Skip to content

Commit 17630d1

Browse files
committed
added scoped options
1 parent 09fe83d commit 17630d1

File tree

10 files changed

+57
-73
lines changed

10 files changed

+57
-73
lines changed

Example/Data/Context.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class Context : DbContext
2121
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
2222
{
2323
//Default: JSON-Serialize
24-
//optionsBuilder.UseFileContextDatabase();
24+
optionsBuilder.UseFileContextDatabase();
2525

2626
//optionsBuilder.UseFileContextDatabase("bson");
2727

@@ -36,7 +36,7 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
3636
//optionsBuilder.UseFileContextDatabase("csv", location: @"D:\t");
3737

3838
//Excel
39-
optionsBuilder.UseFileContextDatabase("excel", databaseName: "test");
39+
//optionsBuilder.UseFileContextDatabase("excel", databaseName: "test");
4040
}
4141

4242
protected override void OnModelCreating(ModelBuilder modelBuilder)

Example/Data/NewContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class NewContext : DbContext
2121
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
2222
{
2323
//Default: JSON-Serialize
24-
optionsBuilder.UseFileContextDatabase("new");
24+
optionsBuilder.UseFileContextDatabase(databaseName: "new");
2525

2626
//JSON-Serialize + simple Encryption
2727
//optionsBuilder.UseFileContext("json", "encrypted");

FileContextCore/Extensions/FileContextServiceCollectionExtensions.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public static class FileContextServiceCollectionExtensions
4545
/// <returns>
4646
/// The same service collection so that multiple calls can be chained.
4747
/// </returns>
48-
public static IServiceCollection AddEntityFrameworkFileContextDatabase([NotNull] this IServiceCollection serviceCollection)
48+
public static IServiceCollection AddEntityFrameworkFileContextDatabase([NotNull] this IServiceCollection serviceCollection, FileContextOptionsExtension optionsExtension = null)
4949
{
5050
Check.NotNull(serviceCollection, nameof(serviceCollection));
5151

@@ -69,6 +69,7 @@ public static IServiceCollection AddEntityFrameworkFileContextDatabase([NotNull]
6969
.TryAddProviderSpecificServices(
7070
b => b
7171
.TryAddSingleton<IFileContextSingletonOptions, FileContextSingletonOptions>()
72+
.TryAddScoped<IFileContextScopedOptions, FileContextScopedOptions>((serviceProvider) => new FileContextScopedOptions(optionsExtension))
7273
.TryAddSingleton<IFileContextStoreCache, FileContextStoreCache>()
7374
.TryAddSingleton<IFileContextTableFactory, FileContextTableFactory>()
7475
.TryAddScoped<IFileContextDatabase, FileContextDatabase>());

FileContextCore/Infrastructure/Internal/FileContextOptionsExtension.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,9 @@ public virtual FileContextOptionsExtension WithDatabaseRoot([NotNull] FileContex
142142
/// doing so can result in application failures when updating to a new Entity Framework Core release.
143143
/// </summary>
144144
public virtual void ApplyServices(IServiceCollection services)
145-
=> services.AddEntityFrameworkFileContextDatabase();
145+
{
146+
services.AddEntityFrameworkFileContextDatabase(this);
147+
}
146148

147149
/// <summary>
148150
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using Microsoft.EntityFrameworkCore.Infrastructure;
5+
6+
namespace FileContextCore.Infrastructure.Internal
7+
{
8+
public class FileContextScopedOptions : IFileContextScopedOptions
9+
{
10+
public FileContextScopedOptions(FileContextOptionsExtension options)
11+
{
12+
if (options != null)
13+
{
14+
Serializer = options.serializer;
15+
DatabaseName = options.StoreName;
16+
FileManager = options.filemanager;
17+
Location = options.location;
18+
}
19+
}
20+
21+
public virtual string DatabaseName { get; private set; }
22+
23+
public virtual string Serializer { get; private set; }
24+
25+
public virtual string FileManager { get; private set; }
26+
27+
public virtual string Location { get; private set; }
28+
}
29+
}

FileContextCore/Infrastructure/Internal/FileContextSingletonOptions.cs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,6 @@ public virtual void Initialize(IDbContextOptions options)
4242
if (inMemoryOptions != null)
4343
{
4444
DatabaseRoot = inMemoryOptions.DatabaseRoot;
45-
Serializer = inMemoryOptions.serializer;
46-
DatabaseName = inMemoryOptions.StoreName;
47-
Filemanager = inMemoryOptions.filemanager;
48-
Location = inMemoryOptions.location;
4945
}
5046
}
5147

@@ -76,10 +72,5 @@ public virtual void Validate(IDbContextOptions options)
7672
/// doing so can result in application failures when updating to a new Entity Framework Core release.
7773
/// </summary>
7874
public virtual FileContextDatabaseRoot DatabaseRoot { get; private set; }
79-
80-
public virtual string DatabaseName { get; private set; }
81-
public virtual string Serializer { get; private set; }
82-
public virtual string Filemanager { get; private set; }
83-
public virtual string Location { get; private set; }
8475
}
8576
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace FileContextCore.Infrastructure.Internal
6+
{
7+
public interface IFileContextScopedOptions
8+
{
9+
string DatabaseName { get; }
10+
string Serializer { get; }
11+
string FileManager { get; }
12+
string Location { get; }
13+
}
14+
}

FileContextCore/Infrastructure/Internal/IFileContextSingletonOptions.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,5 @@ public interface IFileContextSingletonOptions : ISingletonOptions
3434
/// doing so can result in application failures when updating to a new Entity Framework Core release.
3535
/// </summary>
3636
FileContextDatabaseRoot DatabaseRoot { get; }
37-
38-
string DatabaseName { get; }
39-
string Serializer { get; }
40-
string Filemanager { get; }
41-
string Location { get; }
4237
}
4338
}

FileContextCore/Storage/Internal/FileContextTable.cs

Lines changed: 3 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class FileContextTable<TKey> : IFileContextTable
3636
private readonly Microsoft.EntityFrameworkCore.ChangeTracking.Internal.IPrincipalKeyValueFactory<TKey> _keyValueFactory;
3737
private readonly bool _sensitiveLoggingEnabled;
3838
private readonly IEntityType _entityType;
39-
private readonly IFileContextSingletonOptions _options;
39+
private readonly IFileContextScopedOptions _options;
4040
private readonly Dictionary<TKey, object[]> _rows;
4141

4242
private IFileManager fileManager;
@@ -56,7 +56,7 @@ public FileContextTable(
5656
[NotNull] Microsoft.EntityFrameworkCore.ChangeTracking.Internal.IPrincipalKeyValueFactory<TKey> keyValueFactory,
5757
bool sensitiveLoggingEnabled,
5858
IEntityType entityType,
59-
IFileContextSingletonOptions options)
59+
IFileContextScopedOptions options)
6060
{
6161
_keyValueFactory = keyValueFactory;
6262
_sensitiveLoggingEnabled = sensitiveLoggingEnabled;
@@ -300,57 +300,9 @@ private void InitSerializer()
300300

301301
private Action<Dictionary<TKey, object[]>> UpdateMethod;
302302

303-
//private Dictionary<int, string> GetAutoGeneratedFields()
304-
//{
305-
// IProperty[] props = _entityType.GetProperties().ToArray();
306-
// Dictionary<int, string> AutoGeneratedFields = new Dictionary<int, string>();
307-
308-
// for (int i = 0; i < props.Length; i++)
309-
// {
310-
// if (props[i].ValueGenerated == ValueGenerated.OnAdd || props[i].ValueGenerated == ValueGenerated.OnAddOrUpdate || props[i].ValueGenerated == ValueGenerated.OnUpdate)
311-
// {
312-
// if (props[i].ClrType.IsInteger())
313-
// {
314-
// AutoGeneratedFields.Add(i, props[i].Name);
315-
// }
316-
// }
317-
// }
318-
319-
// return AutoGeneratedFields;
320-
//}
321-
322-
//private void GenerateLastAutoPropertyValues(Dictionary<TKey, object[]> list)
323-
//{
324-
// Dictionary<int, string> fields = GetAutoGeneratedFields();
325-
326-
// if (fields.Any())
327-
// {
328-
// Dictionary<string, long> values = new Dictionary<string, long>();
329-
330-
// foreach (KeyValuePair<int, string> val in fields)
331-
// {
332-
// object last = list.Select(p => p.Value[val.Key]).OrderByDescending(p => p).FirstOrDefault();
333-
334-
// if (last != null)
335-
// values.Add(val.Value, (long)Convert.ChangeType(last, typeof(long), CultureInfo.InvariantCulture));
336-
// else
337-
// values.Add(val.Value, 0);
338-
// }
339-
340-
// if (idCache.LastIds.ContainsKey(entityType.Name))
341-
// {
342-
// idCache.LastIds[entityType.Name] = values;
343-
// }
344-
// else
345-
// {
346-
// idCache.LastIds.Add(entityType.Name, values);
347-
// }
348-
// }
349-
//}
350-
351303
private void InitFileManager()
352304
{
353-
string fmgr = _options.Filemanager;
305+
string fmgr = _options.FileManager;
354306

355307
if (fmgr.Length >= 9 && fmgr.Substring(0, 9) == "encrypted")
356308
{

FileContextCore/Storage/Internal/FileContextTableFactory.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class FileContextTableFactory
2525
// WARNING: The in-memory provider is using EF internal code here. This should not be copied by other providers. See #15096
2626
: Microsoft.EntityFrameworkCore.ChangeTracking.Internal.IdentityMapFactoryFactoryBase, IFileContextTableFactory
2727
{
28-
private readonly IFileContextSingletonOptions _options;
28+
private readonly IFileContextScopedOptions _options;
2929
private readonly bool _sensitiveLoggingEnabled;
3030

3131
private readonly ConcurrentDictionary<IKey, Func<IFileContextTable>> _factories
@@ -37,7 +37,7 @@ private readonly ConcurrentDictionary<IKey, Func<IFileContextTable>> _factories
3737
/// any release. You should only use it directly in your code with extreme caution and knowing that
3838
/// doing so can result in application failures when updating to a new Entity Framework Core release.
3939
/// </summary>
40-
public FileContextTableFactory([NotNull] ILoggingOptions loggingOptions, IFileContextSingletonOptions options)
40+
public FileContextTableFactory([NotNull] ILoggingOptions loggingOptions, IFileContextScopedOptions options)
4141
{
4242
_options = options;
4343
Check.NotNull(loggingOptions, nameof(loggingOptions));
@@ -61,7 +61,7 @@ private Func<IFileContextTable> Create([NotNull] IKey key)
6161
.Invoke(null, new object[] { key, key.DeclaringEntityType, _sensitiveLoggingEnabled, _options });
6262

6363
[UsedImplicitly]
64-
private static Func<IFileContextTable> CreateFactory<TKey>(IKey key, IEntityType entityType, bool sensitiveLoggingEnabled, IFileContextSingletonOptions options)
64+
private static Func<IFileContextTable> CreateFactory<TKey>(IKey key, IEntityType entityType, bool sensitiveLoggingEnabled, IFileContextScopedOptions options)
6565
=> () => new FileContextTable<TKey>(
6666
// WARNING: The in-memory provider is using EF internal code here. This should not be copied by other providers. See #15096
6767
Microsoft.EntityFrameworkCore.Metadata.Internal.KeyExtensions.GetPrincipalKeyValueFactory<TKey>(key),

0 commit comments

Comments
 (0)