Skip to content

Commit e8b9d25

Browse files
Merge pull request #31 from johelvisguzman/feature/issue19
Feature/issue19
2 parents 2495008 + cd68ef8 commit e8b9d25

File tree

16 files changed

+1188
-313
lines changed

16 files changed

+1188
-313
lines changed

DotNetToolkit.Repository.sln

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DotNetToolkit.Repository.In
1919
EndProject
2020
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DotNetToolkit.Repository.EntityFrameworkCore", "src\DotNetToolkit.Repository.EntityFrameworkCore\DotNetToolkit.Repository.EntityFrameworkCore.csproj", "{0A9D9AA4-F01C-470F-AD5B-5A81EA7A398D}"
2121
EndProject
22-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DotNetToolkit.Repository.Json", "src\DotNetToolkit.Repository.Json\DotNetToolkit.Repository.Json.csproj", "{B71EA207-390A-4AB0-BFC8-44BA124FC35B}"
22+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DotNetToolkit.Repository.Json", "src\DotNetToolkit.Repository.Json\DotNetToolkit.Repository.Json.csproj", "{B71EA207-390A-4AB0-BFC8-44BA124FC35B}"
23+
EndProject
24+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DotNetToolkit.Repository.Xml", "src\DotNetToolkit.Repository.Xml\DotNetToolkit.Repository.Xml.csproj", "{BB858B77-5CE8-4301-9FB6-343FFF5B7E63}"
2325
EndProject
2426
Global
2527
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -55,6 +57,10 @@ Global
5557
{B71EA207-390A-4AB0-BFC8-44BA124FC35B}.Debug|Any CPU.Build.0 = Debug|Any CPU
5658
{B71EA207-390A-4AB0-BFC8-44BA124FC35B}.Release|Any CPU.ActiveCfg = Release|Any CPU
5759
{B71EA207-390A-4AB0-BFC8-44BA124FC35B}.Release|Any CPU.Build.0 = Release|Any CPU
60+
{BB858B77-5CE8-4301-9FB6-343FFF5B7E63}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
61+
{BB858B77-5CE8-4301-9FB6-343FFF5B7E63}.Debug|Any CPU.Build.0 = Debug|Any CPU
62+
{BB858B77-5CE8-4301-9FB6-343FFF5B7E63}.Release|Any CPU.ActiveCfg = Release|Any CPU
63+
{BB858B77-5CE8-4301-9FB6-343FFF5B7E63}.Release|Any CPU.Build.0 = Release|Any CPU
5864
EndGlobalSection
5965
GlobalSection(SolutionProperties) = preSolution
6066
HideSolutionNode = FALSE
@@ -67,6 +73,7 @@ Global
6773
{715D2F11-3AAF-476E-9A6A-DCA6DEBD377E} = {DD273D5E-6D6C-41FA-A0C8-646CC53C4DC3}
6874
{0A9D9AA4-F01C-470F-AD5B-5A81EA7A398D} = {DD273D5E-6D6C-41FA-A0C8-646CC53C4DC3}
6975
{B71EA207-390A-4AB0-BFC8-44BA124FC35B} = {DD273D5E-6D6C-41FA-A0C8-646CC53C4DC3}
76+
{BB858B77-5CE8-4301-9FB6-343FFF5B7E63} = {DD273D5E-6D6C-41FA-A0C8-646CC53C4DC3}
7077
EndGlobalSection
7178
GlobalSection(ExtensibilityGlobals) = postSolution
7279
SolutionGuid = {96973E0C-81D1-42DE-9F78-7103241B4E07}

appveyor.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ after_build:
4949
- dotnet pack .\src\DotNetToolkit.Repository.EntityFramework\DotNetToolkit.Repository.EntityFramework.csproj --configuration Release
5050
- dotnet pack .\src\DotNetToolkit.Repository.EntityFrameworkCore\DotNetToolkit.Repository.EntityFrameworkCore.csproj --configuration Release
5151
- dotnet pack .\src\DotNetToolkit.Repository.Json\DotNetToolkit.Repository.Json.csproj --configuration Release
52+
- dotnet pack .\src\DotNetToolkit.Repository.Xml\DotNetToolkit.Repository.Xml.csproj --configuration Release
5253

5354
#---------------------------------#
5455
# tests configuration #
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
namespace DotNetToolkit.Repository.InMemory
2+
{
3+
using Properties;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Globalization;
7+
using System.IO;
8+
using System.Linq;
9+
10+
/// <summary>
11+
/// Represents a file based repository for in-memory operations (for testing purposes).
12+
/// </summary>
13+
public abstract class InMemoryFileBasedRepositoryBase<TEntity, TKey> : InMemoryRepositoryBase<TEntity, TKey> where TEntity : class
14+
{
15+
#region Properties
16+
17+
/// <summary>
18+
/// Gets the file extension.
19+
/// </summary>
20+
protected abstract string FileExtension { get; }
21+
22+
#endregion
23+
24+
#region Constructors
25+
26+
/// <summary>
27+
/// Initializes a new instance of the <see cref="InMemoryFileBasedRepositoryBase{TEntity, TKey}"/> class.
28+
/// </summary>
29+
/// <param name="filePath">The file path.</param>
30+
protected InMemoryFileBasedRepositoryBase(string filePath)
31+
{
32+
OnInitialize(filePath);
33+
}
34+
35+
#endregion
36+
37+
#region Protected Methods
38+
39+
/// <summary>
40+
/// A protected overridable method for loading the entities from the specified stream reader.
41+
/// </summary>
42+
protected abstract IEnumerable<TEntity> OnLoaded(StreamReader reader);
43+
44+
/// <summary>
45+
/// A protected overridable method for saving the entities to the specified stream writer.
46+
/// </summary>
47+
protected abstract void OnSaved(StreamWriter writer, IEnumerable<TEntity> entities);
48+
49+
#endregion
50+
51+
#region Private Methods
52+
53+
private string ValidateFile(string filePath)
54+
{
55+
if (string.IsNullOrEmpty(filePath))
56+
throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, nameof(filePath));
57+
58+
// Ensures we have a valid file
59+
var fileName = filePath;
60+
61+
if (Directory.Exists(filePath))
62+
{
63+
if (!fileName.EndsWith(@"\"))
64+
fileName += @"\";
65+
66+
fileName += $"{GetType().Name}{FileExtension}";
67+
}
68+
else
69+
{
70+
if (string.IsNullOrEmpty(Path.GetExtension(fileName)))
71+
fileName += FileExtension;
72+
73+
if (!Path.GetExtension(fileName).Equals(FileExtension))
74+
throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resources.InvalidFileExtension, fileName, FileExtension));
75+
76+
if (fileName.IndexOfAny(Path.GetInvalidFileNameChars()) < 0)
77+
throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resources.InvalidFilePath, fileName));
78+
}
79+
80+
return fileName;
81+
}
82+
83+
private void OnInitialize(string filePath)
84+
{
85+
DatabaseName = ValidateFile(filePath);
86+
87+
// Creates the file if does not exist
88+
if (!File.Exists(DatabaseName))
89+
{
90+
File.Create(DatabaseName).Dispose();
91+
}
92+
// Otherwise, try to get the data from the file
93+
else
94+
{
95+
// Adds the data from the file into memory
96+
using (var stream = new FileStream(DatabaseName, FileMode.Open, FileAccess.Read))
97+
using (var reader = new StreamReader(stream))
98+
{
99+
var entities = OnLoaded(reader);
100+
101+
EnsureDeleted();
102+
103+
foreach (var entity in entities)
104+
{
105+
AddItem(entity);
106+
}
107+
108+
base.SaveChanges();
109+
}
110+
}
111+
}
112+
113+
#endregion
114+
115+
#region Overrides of InMemoryRepositoryBase<TEntity,TKey>
116+
117+
/// <summary>
118+
/// A protected overridable method for saving changes made in the current unit of work in the repository.
119+
/// </summary>
120+
protected override void SaveChanges()
121+
{
122+
using (var stream = new FileStream(DatabaseName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Delete))
123+
{
124+
// Saves the data into memory
125+
base.SaveChanges();
126+
127+
// Puts from memory into the file
128+
using (var writer = new StreamWriter(stream))
129+
{
130+
var entities = GetQuery().ToList();
131+
132+
OnSaved(writer, entities);
133+
}
134+
}
135+
}
136+
137+
#endregion
138+
}
139+
}

src/DotNetToolkit.Repository.InMemory/InMemoryRepositoryBase.cs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public abstract class InMemoryRepositoryBase<TEntity, TKey> : RepositoryBase<TEn
2929
/// <summary>
3030
/// Gets or sets the name of the database.
3131
/// </summary>
32-
protected string DatabaseName { get; set; }
32+
internal string DatabaseName { get; set; }
3333

3434
#endregion
3535

@@ -49,15 +49,6 @@ protected InMemoryRepositoryBase(string databaseName = null)
4949

5050
#region Protected Methods
5151

52-
/// <summary>
53-
/// Ensures the in-memory store is completely deleted.
54-
/// </summary>
55-
public void EnsureDeleted()
56-
{
57-
_context.Clear();
58-
InMemoryCache<TEntity, TKey>.Instance.GetContext(DatabaseName).Clear();
59-
}
60-
6152
/// <summary>
6253
/// Releases unmanaged and - optionally - managed resources.
6354
/// </summary>
@@ -77,6 +68,19 @@ protected virtual void Dispose(bool disposing)
7768

7869
#endregion
7970

71+
#region Internal Methods
72+
73+
/// <summary>
74+
/// Ensures the in-memory store is completely deleted.
75+
/// </summary>
76+
internal void EnsureDeleted()
77+
{
78+
_context.Clear();
79+
InMemoryCache<TEntity, TKey>.Instance.GetContext(DatabaseName).Clear();
80+
}
81+
82+
#endregion
83+
8084
#region Private Methods
8185

8286
/// <summary>

src/DotNetToolkit.Repository.InMemory/Properties/Resources.Designer.cs

Lines changed: 20 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/DotNetToolkit.Repository.InMemory/Properties/Resources.resx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,4 +130,10 @@
130130
<data name="EntityNotFoundInStore" xml:space="preserve">
131131
<value>Attempted to update or delete an entity that does not exist in the in-memory store.</value>
132132
</data>
133+
<data name="InvalidFileExtension" xml:space="preserve">
134+
<value>The specified '{0}' file has an invalid extension. Please consider using '{1}'.</value>
135+
</data>
136+
<data name="InvalidFilePath" xml:space="preserve">
137+
<value>The specified '{0}' file is not a valid path.</value>
138+
</data>
133139
</root>

src/DotNetToolkit.Repository.Json/DotNetToolkit.Repository.Json.csproj

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,4 @@
1818
<ProjectReference Include="..\DotNetToolkit.Repository\DotNetToolkit.Repository.csproj" />
1919
</ItemGroup>
2020

21-
<ItemGroup>
22-
<Compile Update="Properties\Resources.Designer.cs">
23-
<DesignTime>True</DesignTime>
24-
<AutoGen>True</AutoGen>
25-
<DependentUpon>Resources.resx</DependentUpon>
26-
</Compile>
27-
</ItemGroup>
28-
29-
<ItemGroup>
30-
<EmbeddedResource Update="Properties\Resources.resx">
31-
<Generator>ResXFileCodeGenerator</Generator>
32-
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
33-
</EmbeddedResource>
34-
</ItemGroup>
35-
3621
</Project>

0 commit comments

Comments
 (0)