Skip to content

Commit 4955e1a

Browse files
committed
added web example, bug fixes
1 parent b8f01e3 commit 4955e1a

File tree

20 files changed

+417
-124
lines changed

20 files changed

+417
-124
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@
22
/src/FileContextCore/bin
33
/src/FileContextCore/obj
44
/src/Example/bin
5-
/src/Example/obj
5+
/src/Example/obj
6+
/src/WebExample/bin
7+
/src/WebExample/obj

FileContextCore.sln

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio 15
4-
VisualStudioVersion = 15.0.26228.4
4+
VisualStudioVersion = 15.0.26228.9
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{2ED3EC6D-B2EF-4643-9002-0E9FD879E92D}"
77
EndProject
@@ -11,6 +11,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FileContextCore", "src\File
1111
EndProject
1212
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Example", "src\Example\Example.csproj", "{51494293-60AC-4DE1-889C-B7F41CBF6D96}"
1313
EndProject
14+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebExample", "src\WebExample\WebExample.csproj", "{5CFD6E40-85DA-46CB-8DD8-5DE5C89622A8}"
15+
EndProject
1416
Global
1517
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1618
Debug|Any CPU = Debug|Any CPU
@@ -25,12 +27,17 @@ Global
2527
{51494293-60AC-4DE1-889C-B7F41CBF6D96}.Debug|Any CPU.Build.0 = Debug|Any CPU
2628
{51494293-60AC-4DE1-889C-B7F41CBF6D96}.Release|Any CPU.ActiveCfg = Release|Any CPU
2729
{51494293-60AC-4DE1-889C-B7F41CBF6D96}.Release|Any CPU.Build.0 = Release|Any CPU
30+
{5CFD6E40-85DA-46CB-8DD8-5DE5C89622A8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
31+
{5CFD6E40-85DA-46CB-8DD8-5DE5C89622A8}.Debug|Any CPU.Build.0 = Debug|Any CPU
32+
{5CFD6E40-85DA-46CB-8DD8-5DE5C89622A8}.Release|Any CPU.ActiveCfg = Release|Any CPU
33+
{5CFD6E40-85DA-46CB-8DD8-5DE5C89622A8}.Release|Any CPU.Build.0 = Release|Any CPU
2834
EndGlobalSection
2935
GlobalSection(SolutionProperties) = preSolution
3036
HideSolutionNode = FALSE
3137
EndGlobalSection
3238
GlobalSection(NestedProjects) = preSolution
3339
{571D089B-52FF-466F-B666-5AA64644D333} = {2ED3EC6D-B2EF-4643-9002-0E9FD879E92D}
3440
{51494293-60AC-4DE1-889C-B7F41CBF6D96} = {2ED3EC6D-B2EF-4643-9002-0E9FD879E92D}
41+
{5CFD6E40-85DA-46CB-8DD8-5DE5C89622A8} = {2ED3EC6D-B2EF-4643-9002-0E9FD879E92D}
3542
EndGlobalSection
3643
EndGlobal

src/Example/Program.cs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public static void Main(string[] args)
2222

2323
watch.Start();
2424

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

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

@@ -41,6 +41,10 @@ public static void Main(string[] args)
4141

4242
db.Users.Add(us);
4343

44+
//db.Users.Remove(db.Users.FirstOrDefault());
45+
46+
db.SaveChanges();
47+
4448
db.Contents.Add(new Content()
4549
{
4650
Text = "Test",
@@ -73,6 +77,26 @@ public static void Main(string[] args)
7377

7478
db.Messurements.Add(current);
7579
db.SaveChanges();
80+
81+
while (true)
82+
{
83+
string result = Console.ReadLine();
84+
85+
if(result != "q")
86+
{
87+
db.Users.Add(new User()
88+
{
89+
Username = "testuser",
90+
Name = "Test User"
91+
});
92+
93+
db.SaveChanges();
94+
95+
continue;
96+
}
97+
98+
break;
99+
}
76100
}
77101
}
78102
}

src/FileContextCore/CombinedManager/ExcelManager.cs

Lines changed: 120 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,11 @@ namespace FileContextCore.CombinedManager
1212
{
1313
public class ExcelManager : ICombinedManager
1414
{
15-
private ExcelPackage package;
15+
private string password = "";
1616

17-
public ExcelManager(string password = "")
17+
public ExcelManager(string _password = "")
1818
{
19-
if (password != "")
20-
{
21-
package = new ExcelPackage(GetFilePath("data.xlsx"), password);
22-
}
23-
else
24-
{
25-
package = new ExcelPackage(GetFilePath("data.xlsx"));
26-
}
19+
password = _password;
2720
}
2821

2922
FileInfo GetFilePath(string fileName)
@@ -36,14 +29,25 @@ FileInfo GetFilePath(string fileName)
3629

3730
public IList GetItems(Type t)
3831
{
32+
ExcelPackage package;
33+
34+
if (password != "")
35+
{
36+
package = new ExcelPackage(GetFilePath("data.xlsx"), password);
37+
}
38+
else
39+
{
40+
package = new ExcelPackage(GetFilePath("data.xlsx"));
41+
}
42+
3943
ExcelWorksheet ws = package.Workbook.Worksheets[t.Name];
4044

41-
if(ws != null)
45+
if (ws != null)
4246
{
4347
List<PropertyInfo> props = t.GetRuntimeProperties().Where(x => !x.SetMethod.IsVirtual).ToList();
4448
Dictionary<int, PropertyInfo> properties = new Dictionary<int, PropertyInfo>();
45-
46-
for(int i = 0; i < ws.Dimension.Columns; i++)
49+
50+
for (int i = 0; i < ws.Dimension.Columns; i++)
4751
{
4852
properties.Add(i + 1, props.FirstOrDefault(x => x.Name == (string)ws.Cells[1, i + 1].Value));
4953
}
@@ -53,13 +57,17 @@ public IList GetItems(Type t)
5357
for (int i = 1; i < ws.Dimension.Rows; i++)
5458
{
5559
object item = Activator.CreateInstance(t);
56-
57-
foreach(KeyValuePair<int, PropertyInfo> prop in properties)
60+
61+
foreach (KeyValuePair<int, PropertyInfo> prop in properties)
5862
{
59-
if(prop.Value.PropertyType == typeof(TimeSpan))
63+
if (prop.Value.PropertyType == typeof(TimeSpan))
6064
{
6165
prop.Value.SetValue(item, TimeSpan.Parse((string)ws.Cells[i + 1, prop.Key].Value));
6266
}
67+
else if (prop.Value.PropertyType == typeof(Guid))
68+
{
69+
prop.Value.SetValue(item, Guid.Parse((string)ws.Cells[i + 1, prop.Key].Value));
70+
}
6371
else
6472
{
6573
prop.Value.SetValue(item, Convert.ChangeType(ws.Cells[i + 1, prop.Key].Value, prop.Value.PropertyType));
@@ -69,6 +77,8 @@ public IList GetItems(Type t)
6977
result.Add(item);
7078
}
7179

80+
package.Dispose();
81+
7282
return result;
7383
}
7484
else
@@ -86,13 +96,34 @@ public IList GetItems(Type t)
8696

8797
ws.View.FreezePanes(2, 1);
8898

89-
package.Save();
99+
if (password != "")
100+
{
101+
package.Save(password);
102+
}
103+
else
104+
{
105+
package.Save();
106+
}
107+
108+
package.Dispose();
109+
90110
return (IList)Activator.CreateInstance(typeof(List<>).MakeGenericType(t));
91-
}
111+
}
92112
}
93113

94114
public void SaveItems(IList list)
95115
{
116+
ExcelPackage package;
117+
118+
if (password != "")
119+
{
120+
package = new ExcelPackage(GetFilePath("data.xlsx"), password);
121+
}
122+
else
123+
{
124+
package = new ExcelPackage(GetFilePath("data.xlsx"));
125+
}
126+
96127
Type t = list.GetType().GenericTypeArguments[0];
97128
PropertyInfo[] props = t.GetRuntimeProperties().Where(x => !x.SetMethod.IsVirtual).ToArray();
98129

@@ -110,12 +141,80 @@ public void SaveItems(IList list)
110141
}
111142
}
112143

113-
for (int i = 0; i < ws.Dimension.Columns; i++)
144+
for (int y = 0; y < ws.Dimension.Columns; y++)
145+
{
146+
ws.Column(y + 1).AutoFit();
147+
}
148+
149+
if (password != "")
150+
{
151+
package.Save(password);
152+
}
153+
else
154+
{
155+
package.Save();
156+
}
157+
158+
package.Dispose();
159+
}
160+
161+
public bool Clear()
162+
{
163+
bool result = false;
164+
ExcelPackage package;
165+
166+
if (password != "")
167+
{
168+
package = new ExcelPackage(GetFilePath("data.xlsx"), password);
169+
}
170+
else
171+
{
172+
package = new ExcelPackage(GetFilePath("data.xlsx"));
173+
}
174+
175+
if(package.Workbook.Worksheets.Count > 0)
176+
{
177+
for (int i = 0; i < package.Workbook.Worksheets.Count; i++)
178+
{
179+
package.Workbook.Worksheets.Delete(i + 1);
180+
}
181+
182+
if (password != "")
183+
{
184+
package.Save(password);
185+
}
186+
else
187+
{
188+
package.Save();
189+
}
190+
191+
result = true;
192+
}
193+
194+
package.Dispose();
195+
196+
return result;
197+
}
198+
199+
public bool Exists()
200+
{
201+
bool result = false;
202+
ExcelPackage package;
203+
204+
if (password != "")
114205
{
115-
ws.Column(i + 1).AutoFit();
206+
package = new ExcelPackage(GetFilePath("data.xlsx"), password);
116207
}
208+
else
209+
{
210+
package = new ExcelPackage(GetFilePath("data.xlsx"));
211+
}
212+
213+
result = package.Workbook.Worksheets.Count > 0;
214+
215+
package.Dispose();
117216

118-
package.Save();
217+
return result;
119218
}
120219
}
121220
}

src/FileContextCore/CombinedManager/ICombinedManager.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,9 @@ public interface ICombinedManager
1010
IList GetItems(Type t);
1111

1212
void SaveItems(IList list);
13+
14+
bool Clear();
15+
16+
bool Exists();
1317
}
1418
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using FileContextCore.FileManager;
2+
using FileContextCore.Serializer;
3+
using System;
4+
using System.Collections;
5+
using System.Collections.Generic;
6+
using System.Text;
7+
8+
namespace FileContextCore.CombinedManager
9+
{
10+
public class SerializerManager : ICombinedManager
11+
{
12+
private ISerializer serializer;
13+
private IFileManager fileManager;
14+
15+
public SerializerManager(ISerializer _serializer, IFileManager _fileManager)
16+
{
17+
serializer = _serializer;
18+
fileManager = _fileManager;
19+
}
20+
21+
public bool Clear()
22+
{
23+
return fileManager.Clear();
24+
}
25+
26+
public bool Exists()
27+
{
28+
return fileManager.DatabaseExists();
29+
}
30+
31+
public IList GetItems(Type t)
32+
{
33+
return serializer.DeserializeList(fileManager.LoadContent(t, serializer.FileType), t);
34+
}
35+
36+
public void SaveItems(IList list)
37+
{
38+
Type t = list.GetType().GenericTypeArguments[0];
39+
40+
fileManager.SaveContent(t, serializer.FileType, serializer.SerializeList(list));
41+
}
42+
}
43+
}

src/FileContextCore/Extensions/FileContextDbContextOptionsExtensions.cs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,17 @@ public static DbContextOptionsBuilder UseFileContext(this DbContextOptionsBuilde
1919
((IDbContextOptionsBuilderInfrastructure)optionsBuilder).AddOrUpdateExtension(
2020
new FileContextOptionsExtension());
2121

22-
if(serializer != null)
22+
if(serializer != null && fileManager != null)
2323
{
24-
OptionsHelper.serializer = serializer;
24+
OptionsHelper.manager = new SerializerManager(serializer, fileManager);
2525
}
26-
else
26+
else if(serializer != null)
2727
{
28-
OptionsHelper.serializer = new JSONSerializer();
28+
OptionsHelper.manager = new SerializerManager(serializer, new DefaultFileManager());
2929
}
30-
31-
if (fileManager != null)
32-
{
33-
OptionsHelper.fileManager = fileManager;
34-
}
35-
else
30+
else if(fileManager != null)
3631
{
37-
OptionsHelper.fileManager = new DefaultFileManager();
32+
OptionsHelper.manager = new SerializerManager(new JSONSerializer(), fileManager);
3833
}
3934

4035
return optionsBuilder;

src/FileContextCore/FileContextCore.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<RepositoryUrl>https://github.com/morrisjdev/FileContextCore</RepositoryUrl>
2020
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
2121
<PackageRequireLicenseAcceptance>False</PackageRequireLicenseAcceptance>
22-
<Version>1.0.2</Version>
22+
<Version>1.0.6</Version>
2323
<PackageTags>ef, database, file, db, filedb, json, xml, csv, xlsx, excel, dbcontext, entity framework</PackageTags>
2424
<PackageIconUrl>https://morrisj.net/img/morrisjdev_logo.png</PackageIconUrl>
2525
</PropertyGroup>

src/FileContextCore/FileManager/DefaultFileManager.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ public void SaveContent(string fileName, string content)
5353
lock (thisLock)
5454
{
5555
string path = GetFilePath(fileName);
56-
5756
File.WriteAllText(path, content);
5857
}
5958
}

0 commit comments

Comments
 (0)