Skip to content

Commit 366cae5

Browse files
committed
csv fix, excel storage
1 parent ae1a0ba commit 366cae5

File tree

10 files changed

+244
-33
lines changed

10 files changed

+244
-33
lines changed

src/Example/Data/Context.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,20 @@ public class Context : DbContext
1818

1919
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
2020
{
21-
optionsBuilder.UseFileContext(new FileContextCore.Serializer.XMLSerializer());
21+
//Default: JSON-Serialize
22+
//optionsBuilder.UseFileContext();
23+
24+
//JSON-Serialize + simple Encryption
25+
//optionsBuilder.UseFileContext(new FileContextCore.Serializer.JSONSerializer(), new FileContextCore.FileManager.EncryptedFileManager());
26+
27+
//XML
28+
//optionsBuilder.UseFileContext(new FileContextCore.Serializer.XMLSerializer());
29+
30+
//CSV
31+
//optionsBuilder.UseFileContext(new FileContextCore.Serializer.CSVSerializer());
32+
33+
//Excel with password
34+
optionsBuilder.UseFileContext(new FileContextCore.CombinedManager.ExcelManager());
2235
}
2336

2437
protected override void OnModelCreating(ModelBuilder modelBuilder)

src/Example/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public static void Main(string[] args)
1818

1919
//db.Database.EnsureDeleted();
2020

21-
List<User> users = db.Users.Include(x => x.Contents)/*.Include(x => x.Settings)*/.ToList();
21+
List<User> users = db.Users.Include(x => x.Contents).Include(x => x.Settings).ToList();
2222

2323
List<Content> contents = db.Contents.Include(x => x.User).ToList();
2424

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
using OfficeOpenXml;
2+
using System;
3+
using System.Collections;
4+
using System.Collections.Generic;
5+
using System.IO;
6+
using System.Linq;
7+
using System.Reflection;
8+
using System.Text;
9+
10+
namespace FileContextCore.CombinedManager
11+
{
12+
public class ExcelManager : ICombinedManager
13+
{
14+
private ExcelPackage package;
15+
16+
public ExcelManager(string password = "")
17+
{
18+
if (password != "")
19+
{
20+
package = new ExcelPackage(GetFilePath("data.xlsx"), password);
21+
}
22+
else
23+
{
24+
package = new ExcelPackage(GetFilePath("data.xlsx"));
25+
}
26+
}
27+
28+
FileInfo GetFilePath(string fileName)
29+
{
30+
string path = Path.Combine(AppContext.BaseDirectory, "appdata");
31+
Directory.CreateDirectory(path);
32+
33+
return new FileInfo(Path.Combine(path, fileName));
34+
}
35+
36+
public IList GetItems(Type t)
37+
{
38+
ExcelWorksheet ws = package.Workbook.Worksheets[t.Name];
39+
40+
if(ws != null)
41+
{
42+
List<PropertyInfo> props = t.GetRuntimeProperties().Where(x => !x.SetMethod.IsVirtual).ToList();
43+
Dictionary<int, PropertyInfo> properties = new Dictionary<int, PropertyInfo>();
44+
45+
for(int i = 0; i < ws.Dimension.Columns; i++)
46+
{
47+
properties.Add(i + 1, props.FirstOrDefault(x => x.Name == (string)ws.Cells[1, i + 1].Value));
48+
}
49+
50+
IList result = (IList)Activator.CreateInstance(typeof(List<>).MakeGenericType(t));
51+
52+
for (int i = 1; i < ws.Dimension.Rows; i++)
53+
{
54+
object item = Activator.CreateInstance(t);
55+
56+
foreach(KeyValuePair<int, PropertyInfo> prop in properties)
57+
{
58+
prop.Value.SetValue(item, Convert.ChangeType(ws.Cells[i + 1, prop.Key].Value, prop.Value.PropertyType));
59+
}
60+
61+
result.Add(item);
62+
}
63+
64+
return result;
65+
}
66+
else
67+
{
68+
ws = package.Workbook.Worksheets.Add(t.Name);
69+
70+
PropertyInfo[] props = t.GetRuntimeProperties().Where(x => !x.SetMethod.IsVirtual).ToArray();
71+
72+
for (int i = 0; i < props.Count(); i++)
73+
{
74+
PropertyInfo pi = props[i];
75+
ws.Cells[1, i + 1].Value = pi.Name;
76+
ws.Column(i + 1).AutoFit();
77+
}
78+
79+
ws.View.FreezePanes(2, 1);
80+
81+
package.Save();
82+
return (IList)Activator.CreateInstance(typeof(List<>).MakeGenericType(t));
83+
}
84+
}
85+
86+
public void SaveItems(IList list)
87+
{
88+
Type t = list.GetType().GenericTypeArguments[0];
89+
PropertyInfo[] props = t.GetRuntimeProperties().Where(x => !x.SetMethod.IsVirtual).ToArray();
90+
91+
ExcelWorksheet ws = package.Workbook.Worksheets[t.Name];
92+
93+
for (int i = 0; i < list.Count; i++)
94+
{
95+
object item = list[i];
96+
97+
for (int x = 0; x < props.Count(); x++)
98+
{
99+
PropertyInfo pi = props[x];
100+
101+
ws.SetValue(i + 2, x + 1, pi.GetValue(item).ToString());
102+
}
103+
}
104+
105+
for (int i = 0; i < ws.Dimension.Columns; i++)
106+
{
107+
ws.Column(i + 1).AutoFit();
108+
}
109+
110+
package.Save();
111+
}
112+
}
113+
}
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;
3+
using System.Collections.Generic;
4+
using System.Text;
5+
6+
namespace FileContextCore.CombinedManager
7+
{
8+
public interface ICombinedManager
9+
{
10+
IList GetItems(Type t);
11+
12+
void SaveItems(IList list);
13+
}
14+
}

src/FileContextCore/Extensions/FileContextDbContextOptionsExtensions.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using FileContextCore.FileManager;
1+
using FileContextCore.CombinedManager;
2+
using FileContextCore.FileManager;
23
using FileContextCore.Helper;
34
using FileContextCore.Infrastructure;
45
using FileContextCore.Serializer;
@@ -30,11 +31,29 @@ public static DbContextOptionsBuilder UseFileContext(this DbContextOptionsBuilde
3031
{
3132
OptionsHelper.serializer = serializer;
3233
}
34+
else
35+
{
36+
OptionsHelper.serializer = new JSONSerializer();
37+
}
3338

3439
if (fileManager != null)
3540
{
3641
OptionsHelper.fileManager = fileManager;
3742
}
43+
else
44+
{
45+
OptionsHelper.fileManager = new DefaultFileManager();
46+
}
47+
48+
return optionsBuilder;
49+
}
50+
51+
public static DbContextOptionsBuilder UseFileContext(this DbContextOptionsBuilder optionsBuilder, ICombinedManager manager)
52+
{
53+
((IDbContextOptionsBuilderInfrastructure)optionsBuilder).AddOrUpdateExtension(
54+
new FileContextOptionsExtension());
55+
56+
OptionsHelper.manager = manager;
3857

3958
return optionsBuilder;
4059
}

src/FileContextCore/FileContextCore.csproj

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,28 @@
44
<TargetFramework>netstandard1.6</TargetFramework>
55
<AssemblyName>FileContextCore</AssemblyName>
66
<PackageId>FileContextCore</PackageId>
7-
<NetStandardImplicitPackageVersion>1.6.0</NetStandardImplicitPackageVersion>
7+
<NetStandardImplicitPackageVersion>1.6.1</NetStandardImplicitPackageVersion>
88
<PackageTargetFallback>$(PackageTargetFallback);dnxcore50</PackageTargetFallback>
99
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
1010
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
1111
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
1212
<SignAssembly>False</SignAssembly>
1313
<DelaySign>True</DelaySign>
14-
<Description>FileContextCore is a "Database"-Provider for Entity Framework Core and adds the ability to store information in files instead of being limited to databases.</Description>
14+
<Description>FileContextCore is a "Database"-Provider for Entity Framework Core and adds the ability to store information in files instead of being limited to databases. It enables fast developments because of the advantage of just copy, edit and delete files.</Description>
1515
<Authors>morrisjdev</Authors>
1616
<PackageLicenseUrl>https://github.com/morrisjdev/FileContextCore/blob/master/LICENSE</PackageLicenseUrl>
1717
<Copyright>Copyright (c) 2017 Morris Janatzek</Copyright>
1818
<PackageProjectUrl>https://github.com/morrisjdev/FileContextCore</PackageProjectUrl>
1919
<RepositoryUrl>https://github.com/morrisjdev/FileContextCore</RepositoryUrl>
2020
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
2121
<PackageRequireLicenseAcceptance>False</PackageRequireLicenseAcceptance>
22+
<Version>1.0.1</Version>
23+
<PackageTags>ef, database, file, db, filedb, json, xml, csv, xlsx, excel, dbcontext, entity framework</PackageTags>
2224
</PropertyGroup>
2325

2426
<ItemGroup>
25-
<PackageReference Include="CsvHelper" Version="3.0.0-beta7" />
27+
<PackageReference Include="CsvHelper" Version="2.16.3" />
28+
<PackageReference Include="EPPlus.Core" Version="1.3.1" />
2629
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="1.1.1" />
2730
<PackageReference Include="Newtonsoft.Json" Version="10.0.1" />
2831
<PackageReference Include="System.Xml.XmlSerializer" Version="4.3.0" />
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using FileContextCore.FileManager;
1+
using FileContextCore.CombinedManager;
2+
using FileContextCore.FileManager;
23
using FileContextCore.Serializer;
34
using System;
45
using System.Collections.Generic;
@@ -8,8 +9,10 @@ namespace FileContextCore.Helper
89
{
910
static class OptionsHelper
1011
{
11-
public static IFileManager fileManager = new DefaultFileManager();
12+
public static IFileManager fileManager = null;
1213

13-
public static ISerializer serializer = new JSONSerializer();
14+
public static ISerializer serializer = null;
15+
16+
public static ICombinedManager manager = null;
1417
}
1518
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
This file is used by the publish/package process of your Web project. You can customize the behavior of this process
4+
by editing this MSBuild file. In order to learn more about this please visit https://go.microsoft.com/fwlink/?LinkID=208121.
5+
-->
6+
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
7+
<PropertyGroup>
8+
<PublishProtocol>FileSystem</PublishProtocol>
9+
<Configuration>Release</Configuration>
10+
<TargetFramework>netstandard1.6</TargetFramework>
11+
<PublishDir>bin\Release\PublishOutput</PublishDir>
12+
</PropertyGroup>
13+
</Project>

src/FileContextCore/Serializer/CSVSerializer.cs

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,31 +27,36 @@ public CSVSerializer(string _delimiter = ",")
2727

2828
public IList DeserializeList(string list, Type t)
2929
{
30-
PropertyInfo[] properties = t.GetRuntimeProperties().Where(x => !x.SetMethod.IsVirtual).ToArray();
30+
if(list != "")
31+
{
32+
PropertyInfo[] properties = t.GetRuntimeProperties().Where(x => !x.SetMethod.IsVirtual).ToArray();
3133

32-
CsvClassMap map = Activator.CreateInstance(typeof(DefaultCsvClassMap<>).MakeGenericType(t)) as CsvClassMap;
34+
CsvClassMap map = Activator.CreateInstance(typeof(DefaultCsvClassMap<>).MakeGenericType(t)) as CsvClassMap;
3335

34-
foreach (PropertyInfo pi in properties)
35-
{
36-
CsvPropertyMap propMap = new CsvPropertyMap(pi);
37-
propMap.Name(pi.Name);
38-
propMap.Index(0);
39-
map.PropertyMaps.Add(propMap);
40-
}
36+
foreach (PropertyInfo pi in properties)
37+
{
38+
CsvPropertyMap propMap = new CsvPropertyMap(pi);
39+
propMap.Name(pi.Name);
40+
propMap.Index(0);
41+
map.PropertyMaps.Add(propMap);
42+
}
4143

42-
TextReader tr = new StringReader(list);
43-
CsvReader reader = new CsvReader(tr);
44-
reader.Configuration.Delimiter = delimiter;
45-
reader.Configuration.RegisterClassMap(map);
44+
TextReader tr = new StringReader(list);
45+
CsvReader reader = new CsvReader(tr);
46+
reader.Configuration.Delimiter = delimiter;
47+
reader.Configuration.RegisterClassMap(map);
4648

47-
IList result = (IList)Activator.CreateInstance(typeof(List<>).MakeGenericType(t));
49+
IList result = (IList)Activator.CreateInstance(typeof(List<>).MakeGenericType(t));
4850

49-
foreach (object record in reader.GetRecords(t))
50-
{
51-
result.Add(record);
51+
foreach (object record in reader.GetRecords(t))
52+
{
53+
result.Add(record);
54+
}
55+
56+
return result;
5257
}
5358

54-
return result;
59+
return (IList)Activator.CreateInstance(typeof(List<>).MakeGenericType(t));
5560
}
5661

5762
public string SerializeList(IList list)

0 commit comments

Comments
 (0)