Skip to content

Commit 014bf39

Browse files
committed
added filemanager and serializers
1 parent 3bf3447 commit 014bf39

25 files changed

+1304
-89
lines changed

Example/Data/Context.cs

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

26-
//optionsBuilder.UseFileContext("bson");
26+
//optionsBuilder.UseFileContextDatabase("bson");
2727

2828
//JSON-Serialize + simple Encryption
29-
//optionsBuilder.UseFileContext("json", "encrypted");
29+
//optionsBuilder.UseFileContextDatabase("json", "encrypted");
3030

3131
//XML
32-
//optionsBuilder.UseFileContext("xml");
33-
//optionsBuilder.UseFileContext("xml", "private");
32+
//optionsBuilder.UseFileContextDatabase("xml");
33+
//optionsBuilder.UseFileContextDatabase("xml", "private");
3434

3535
//CSV
36-
//optionsBuilder.UseFileContext("csv", location: @"C:\Users\mjanatzek\Documents\Projects\t");
36+
//optionsBuilder.UseFileContextDatabase("csv", location: @"D:\t");
3737

3838
//Excel
39-
//optionsBuilder.UseFileContext("excel", databasename: "test", location: @"C:\Users\mjanatzek\Documents\Projects\t");
39+
optionsBuilder.UseFileContextDatabase("excel", databaseName: "test");
4040
}
4141

4242
protected override void OnModelCreating(ModelBuilder modelBuilder)

FileContextCore/Extensions/FileContextDbContextOptionsExtensions.cs

Lines changed: 18 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -24,50 +24,6 @@ namespace FileContextCore
2424
/// </summary>
2525
public static class FileContextDbContextOptionsExtensions
2626
{
27-
/// <summary>
28-
/// Configures the context to connect to an in-memory database.
29-
/// The in-memory database is shared anywhere the same name is used, but only for a given
30-
/// service provider. To use the same in-memory database across service providers, call
31-
/// <see
32-
/// cref="UseFileContextDatabase{TContext}(DbContextOptionsBuilder{TContext},string,FileContextDatabaseRoot,Action{FileContextDbContextOptionsBuilder})" />
33-
/// passing a shared <see cref="FileContextDatabaseRoot" /> on which to root the database.
34-
/// </summary>
35-
/// <typeparam name="TContext"> The type of context being configured. </typeparam>
36-
/// <param name="optionsBuilder"> The builder being used to configure the context. </param>
37-
/// <param name="databaseName">
38-
/// The name of the in-memory database. This allows the scope of the in-memory database to be controlled
39-
/// independently of the context. The in-memory database is shared anywhere the same name is used.
40-
/// </param>
41-
/// <param name="inMemoryOptionsAction">An optional action to allow additional in-memory specific configuration.</param>
42-
/// <returns> The options builder so that further configuration can be chained. </returns>
43-
public static DbContextOptionsBuilder<TContext> UseFileContextDatabase<TContext>(
44-
[NotNull] this DbContextOptionsBuilder<TContext> optionsBuilder,
45-
[NotNull] string databaseName,
46-
[CanBeNull] Action<FileContextDbContextOptionsBuilder> inMemoryOptionsAction = null)
47-
where TContext : DbContext
48-
=> (DbContextOptionsBuilder<TContext>)UseFileContextDatabase(
49-
(DbContextOptionsBuilder)optionsBuilder, databaseName, inMemoryOptionsAction);
50-
51-
/// <summary>
52-
/// Configures the context to connect to a named in-memory database.
53-
/// The in-memory database is shared anywhere the same name is used, but only for a given
54-
/// service provider. To use the same in-memory database across service providers, call
55-
/// <see cref="UseFileContextDatabase(DbContextOptionsBuilder,string,FileContextDatabaseRoot,Action{FileContextDbContextOptionsBuilder})" />
56-
/// passing a shared <see cref="FileContextDatabaseRoot" /> on which to root the database.
57-
/// </summary>
58-
/// <param name="optionsBuilder"> The builder being used to configure the context. </param>
59-
/// <param name="databaseName">
60-
/// The name of the in-memory database. This allows the scope of the in-memory database to be controlled
61-
/// independently of the context. The in-memory database is shared anywhere the same name is used.
62-
/// </param>
63-
/// <param name="inMemoryOptionsAction">An optional action to allow additional in-memory specific configuration.</param>
64-
/// <returns> The options builder so that further configuration can be chained. </returns>
65-
public static DbContextOptionsBuilder UseFileContextDatabase(
66-
[NotNull] this DbContextOptionsBuilder optionsBuilder,
67-
[NotNull] string databaseName,
68-
[CanBeNull] Action<FileContextDbContextOptionsBuilder> inMemoryOptionsAction = null)
69-
=> UseFileContextDatabase(optionsBuilder, databaseName, null, inMemoryOptionsAction);
70-
7127
/// <summary>
7228
/// Configures the context to connect to an in-memory database.
7329
/// The in-memory database is shared anywhere the same name is used, but only for a given
@@ -79,21 +35,27 @@ public static DbContextOptionsBuilder UseFileContextDatabase(
7935
/// The name of the in-memory database. This allows the scope of the in-memory database to be controlled
8036
/// independently of the context. The in-memory database is shared anywhere the same name is used.
8137
/// </param>
38+
/// <param name="location">An optional parameter to define the location where the files are stored</param>
8239
/// <param name="databaseRoot">
8340
/// All in-memory databases will be rooted in this object, allowing the application
8441
/// to control their lifetime. This is useful when sometimes the context instance
8542
/// is created explicitly with <c>new</c> while at other times it is resolved using dependency injection.
8643
/// </param>
8744
/// <param name="inMemoryOptionsAction">An optional action to allow additional in-memory specific configuration.</param>
45+
/// <param name="serializer">The serializer to be used. Defaults to json</param>
46+
/// <param name="filemanager">The file manager to be used.</param>
8847
/// <returns> The options builder so that further configuration can be chained. </returns>
8948
public static DbContextOptionsBuilder<TContext> UseFileContextDatabase<TContext>(
9049
[NotNull] this DbContextOptionsBuilder<TContext> optionsBuilder,
91-
[NotNull] string databaseName,
92-
[CanBeNull] FileContextDatabaseRoot databaseRoot,
50+
string serializer = "json",
51+
string filemanager = "default",
52+
string databaseName = "",
53+
string location = null,
54+
[CanBeNull] FileContextDatabaseRoot databaseRoot = null,
9355
[CanBeNull] Action<FileContextDbContextOptionsBuilder> inMemoryOptionsAction = null)
9456
where TContext : DbContext
9557
=> (DbContextOptionsBuilder<TContext>)UseFileContextDatabase(
96-
(DbContextOptionsBuilder)optionsBuilder, databaseName, databaseRoot, inMemoryOptionsAction);
58+
(DbContextOptionsBuilder)optionsBuilder, serializer, filemanager, databaseName, location, databaseRoot, inMemoryOptionsAction);
9759

9860
/// <summary>
9961
/// Configures the context to connect to a named in-memory database.
@@ -105,26 +67,32 @@ public static DbContextOptionsBuilder<TContext> UseFileContextDatabase<TContext>
10567
/// The name of the in-memory database. This allows the scope of the in-memory database to be controlled
10668
/// independently of the context. The in-memory database is shared anywhere the same name is used.
10769
/// </param>
70+
/// <param name="location">An optional parameter to define the location where the files are stored</param>
10871
/// <param name="databaseRoot">
10972
/// All in-memory databases will be rooted in this object, allowing the application
11073
/// to control their lifetime. This is useful when sometimes the context instance
11174
/// is created explicitly with <c>new</c> while at other times it is resolved using dependency injection.
11275
/// </param>
11376
/// <param name="inMemoryOptionsAction">An optional action to allow additional in-memory specific configuration.</param>
77+
/// <param name="serializer">The serializer to be used. Defaults to json</param>
78+
/// <param name="filemanager">The file manager to be used.</param>
11479
/// <returns> The options builder so that further configuration can be chained. </returns>
11580
public static DbContextOptionsBuilder UseFileContextDatabase(
11681
[NotNull] this DbContextOptionsBuilder optionsBuilder,
117-
[NotNull] string databaseName,
118-
[CanBeNull] FileContextDatabaseRoot databaseRoot,
82+
string serializer = "json",
83+
string filemanager = "default",
84+
string databaseName = "",
85+
string location = null,
86+
[CanBeNull] FileContextDatabaseRoot databaseRoot = null,
11987
[CanBeNull] Action<FileContextDbContextOptionsBuilder> inMemoryOptionsAction = null)
12088
{
12189
Check.NotNull(optionsBuilder, nameof(optionsBuilder));
122-
Check.NotEmpty(databaseName, nameof(databaseName));
12390

12491
var extension = optionsBuilder.Options.FindExtension<FileContextOptionsExtension>()
12592
?? new FileContextOptionsExtension();
12693

12794
extension = extension.WithStoreName(databaseName);
95+
extension = extension.WithCustomOptions(serializer, filemanager, location);
12896

12997
if (databaseRoot != null)
13098
{

FileContextCore/FileContextCore.csproj

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<SignAssembly Condition="'$(OS)' == 'Windows_NT'">true</SignAssembly>
66
<AssemblyOriginatorKeyFile>FileContextCoreCert.pfx</AssemblyOriginatorKeyFile>
77
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
8-
<Version>3.0.1</Version>
8+
<Version>3.2.0</Version>
99
<Company>morrisjdev</Company>
1010
<Authors>morrisjdev</Authors>
1111
<Description>File provider for Entity Framework Core (to be used for development purposes)</Description>
@@ -19,12 +19,13 @@
1919
<PackageProjectUrl>https://github.com/morrisjdev/FileContextCore</PackageProjectUrl>
2020
<NeutralLanguage>en-US</NeutralLanguage>
2121
<DelaySign>false</DelaySign>
22-
<AssemblyVersion>3.0.1.0</AssemblyVersion>
22+
<AssemblyVersion>3.2.0.0</AssemblyVersion>
23+
<FileVersion>3.2.0.0</FileVersion>
2324
</PropertyGroup>
2425

2526
<ItemGroup>
2627
<PackageReference Include="CsvHelper" Version="12.1.3" />
27-
<PackageReference Include="EPPlus.Core" Version="1.5.4" />
28+
<PackageReference Include="EPPlus" Version="4.5.3.2" />
2829
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.0.0" />
2930
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="3.0.0" />
3031
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
33
<PropertyGroup>
4-
<_LastSelectedProfileId>C:\Users\mjanatzek\Documents\Projects\private\FileContextCore\FileContextCore\Properties\PublishProfiles\FolderProfile.pubxml</_LastSelectedProfileId>
4+
<_LastSelectedProfileId>C:\Users\morri\Documents\Projects\productive\FileContextCore\FileContextCore\Properties\PublishProfiles\FolderProfile.pubxml</_LastSelectedProfileId>
55
</PropertyGroup>
66
</Project>
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
using Microsoft.EntityFrameworkCore.Metadata;
2+
using System;
3+
using System.IO;
4+
using Microsoft.EntityFrameworkCore;
5+
6+
namespace FileContextCore.FileManager
7+
{
8+
class DefaultFileManager : IFileManager
9+
{
10+
private readonly object thisLock = new object();
11+
12+
IEntityType type;
13+
private readonly string filetype;
14+
private readonly string databasename;
15+
private readonly string _location;
16+
17+
public DefaultFileManager(IEntityType _type, string _filetype, string _databasename, string location)
18+
{
19+
type = _type;
20+
filetype = _filetype;
21+
databasename = _databasename;
22+
_location = location;
23+
}
24+
25+
public string GetFileName()
26+
{
27+
string name = type.GetTableName().GetValidFileName();
28+
29+
string path = string.IsNullOrEmpty(_location)
30+
? Path.Combine(AppContext.BaseDirectory, "appdata", databasename)
31+
: _location;
32+
33+
Directory.CreateDirectory(path);
34+
35+
return Path.Combine(path, name + "." + filetype);
36+
}
37+
38+
public string LoadContent()
39+
{
40+
lock (thisLock)
41+
{
42+
string path = GetFileName();
43+
44+
if (File.Exists(path))
45+
{
46+
return File.ReadAllText(path);
47+
}
48+
49+
return "";
50+
}
51+
}
52+
53+
public void SaveContent(string content)
54+
{
55+
lock (thisLock)
56+
{
57+
string path = GetFileName();
58+
File.WriteAllText(path, content);
59+
}
60+
}
61+
62+
public bool Clear()
63+
{
64+
lock (thisLock)
65+
{
66+
FileInfo fi = new FileInfo(GetFileName());
67+
68+
if (fi.Exists)
69+
{
70+
fi.Delete();
71+
return true;
72+
}
73+
else
74+
{
75+
return false;
76+
}
77+
}
78+
}
79+
80+
public bool FileExists()
81+
{
82+
lock (thisLock)
83+
{
84+
FileInfo fi = new FileInfo(GetFileName());
85+
86+
return fi.Exists;
87+
}
88+
}
89+
}
90+
}

0 commit comments

Comments
 (0)