Skip to content

Commit cd68ef8

Browse files
Introduced a new xml repository
1 parent 271cabd commit cd68ef8

File tree

9 files changed

+986
-2
lines changed

9 files changed

+986
-2
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 #

src/DotNetToolkit.Repository.InMemory/InMemoryFileBasedRepositoryBase.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Collections.Generic;
66
using System.Globalization;
77
using System.IO;
8+
using System.Linq;
89

910
/// <summary>
1011
/// Represents a file based repository for in-memory operations (for testing purposes).
@@ -126,7 +127,7 @@ protected override void SaveChanges()
126127
// Puts from memory into the file
127128
using (var writer = new StreamWriter(stream))
128129
{
129-
var entities = GetQuery();
130+
var entities = GetQuery().ToList();
130131

131132
OnSaved(writer, entities);
132133
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<Import Project="..\..\build\common.props" />
4+
5+
<PropertyGroup>
6+
<TargetFrameworks>net451;netstandard1.3</TargetFrameworks>
7+
<AssemblyName>DotNetToolkit.Repository.Xml</AssemblyName>
8+
<RootNamespace>DotNetToolkit.Repository.Xml</RootNamespace>
9+
<Description>A toolkit that provides a lightweight starter kit for using the Repository pattern for storing entities in an xml formatted file.</Description>
10+
</PropertyGroup>
11+
12+
<ItemGroup>
13+
<PackageReference Include="System.Xml.XmlSerializer" Version="4.3.0" />
14+
</ItemGroup>
15+
16+
<ItemGroup>
17+
<ProjectReference Include="..\DotNetToolkit.Repository.InMemory\DotNetToolkit.Repository.InMemory.csproj" />
18+
<ProjectReference Include="..\DotNetToolkit.Repository\DotNetToolkit.Repository.csproj" />
19+
</ItemGroup>
20+
21+
</Project>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
namespace DotNetToolkit.Repository.Xml
2+
{
3+
/// <summary>
4+
/// Represents a repository for storing entities as an xml formatted file.
5+
/// </summary>
6+
public class XmlRepository<TEntity, TKey> : XmlRepositoryBase<TEntity, TKey> where TEntity : class
7+
{
8+
#region Constructors
9+
10+
/// <summary>
11+
/// Initializes a new instance of the <see cref="XmlRepository{TEntity,TKey}"/> class.
12+
/// </summary>
13+
/// <param name="filePath">The file path.</param>
14+
public XmlRepository(string filePath = null) : base(filePath)
15+
{
16+
}
17+
18+
#endregion
19+
}
20+
21+
/// <summary>
22+
/// Represents a repository for storing entities as an xml formatted file with a default primary key value of type integer (for testing purposes).
23+
/// </summary>
24+
public class XmlRepository<TEntity> : XmlRepositoryBase<TEntity, int>, IRepository<TEntity> where TEntity : class
25+
{
26+
#region Constructors
27+
28+
/// <summary>
29+
/// Initializes a new instance of the <see cref="XmlRepository{TEntity}"/> class.
30+
/// </summary>
31+
/// <param name="filePath">The file path.</param>
32+
public XmlRepository(string filePath = null) : base(filePath)
33+
{
34+
}
35+
36+
#endregion
37+
}
38+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
namespace DotNetToolkit.Repository.Xml
2+
{
3+
using InMemory;
4+
using System.Collections.Generic;
5+
using System.IO;
6+
using System.Xml.Serialization;
7+
8+
/// <summary>
9+
/// Represents a repository for storing entities as an xml formatted file.
10+
/// </summary>
11+
public abstract class XmlRepositoryBase<TEntity, TKey> : InMemoryFileBasedRepositoryBase<TEntity, TKey> where TEntity : class
12+
{
13+
#region Constructors
14+
15+
/// <summary>
16+
/// Initializes a new instance of the <see cref="XmlRepositoryBase{TEntity, TKey}"/> class.
17+
/// </summary>
18+
/// <param name="filePath">The file path.</param>
19+
protected XmlRepositoryBase(string filePath) : base(filePath)
20+
{
21+
}
22+
23+
#endregion
24+
25+
#region Overrides of InMemoryFileBasedRepositoryBase<TEntity,TKey>
26+
27+
/// <summary>
28+
/// Gets the file extension.
29+
/// </summary>
30+
protected override string FileExtension { get; } = ".xml";
31+
32+
/// <summary>
33+
/// A protected overridable method for loading the entities from the specified stream reader.
34+
/// </summary>
35+
protected override IEnumerable<TEntity> OnLoaded(StreamReader reader)
36+
{
37+
var serializer = new XmlSerializer(typeof(List<TEntity>));
38+
var entities = (List<TEntity>)serializer.Deserialize(reader);
39+
40+
return entities;
41+
}
42+
43+
/// <summary>
44+
/// A protected overridable method for saving the entities to the specified stream writer.
45+
/// </summary>
46+
protected override void OnSaved(StreamWriter writer, IEnumerable<TEntity> entities)
47+
{
48+
var serializer = new XmlSerializer(typeof(List<TEntity>));
49+
50+
serializer.Serialize(writer, entities);
51+
}
52+
53+
#endregion
54+
}
55+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
namespace DotNetToolkit.Repository.Xml
2+
{
3+
using System;
4+
using System.Linq;
5+
6+
/// <summary>
7+
/// An implementation of <see cref="IRepositoryFactory" />.
8+
/// </summary>
9+
public class XmlRepositoryFactory : IRepositoryFactory
10+
{
11+
#region Private Methods
12+
13+
private string GetFilePath(IRepositoryOptions options)
14+
{
15+
var arg = options.DbContextArgs.FirstOrDefault();
16+
var databaseName = arg as string;
17+
18+
if (arg != null && databaseName == null)
19+
throw new ArgumentException($"The provided {nameof(options.DbContextArgs)} must be a valid string argument.");
20+
21+
return databaseName;
22+
}
23+
24+
#endregion
25+
26+
#region Implementation of IRepositoryFactory
27+
28+
/// <summary>
29+
/// Creates a new repository for the specified entity type.
30+
/// </summary>
31+
/// <typeparam name="TEntity">The type of the entity.</typeparam>
32+
/// <param name="options">The options.</param>
33+
/// <returns>The new repository.</returns>
34+
public IRepository<TEntity> Create<TEntity>(IRepositoryOptions options) where TEntity : class
35+
{
36+
return new XmlRepository<TEntity>(GetFilePath(options));
37+
}
38+
39+
/// <summary>
40+
/// Creates a new repository for the specified entity and primary key type.
41+
/// </summary>
42+
/// <typeparam name="TEntity">The type of the entity.</typeparam>
43+
/// <typeparam name="TKey">The type of the key primary key value.</typeparam>
44+
/// <param name="options">The options.</param>
45+
/// <returns>The new repository.</returns>
46+
public IRepository<TEntity, TKey> Create<TEntity, TKey>(IRepositoryOptions options) where TEntity : class
47+
{
48+
return new XmlRepository<TEntity, TKey>(GetFilePath(options));
49+
}
50+
51+
#endregion
52+
}
53+
}

test/DotNetToolkit.Repository.Integration.Test/DotNetToolkit.Repository.Integration.Test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
<ProjectReference Include="..\..\src\DotNetToolkit.Repository.EntityFramework\DotNetToolkit.Repository.EntityFramework.csproj" />
3131
<ProjectReference Include="..\..\src\DotNetToolkit.Repository.InMemory\DotNetToolkit.Repository.InMemory.csproj" />
3232
<ProjectReference Include="..\..\src\DotNetToolkit.Repository.Json\DotNetToolkit.Repository.Json.csproj" />
33+
<ProjectReference Include="..\..\src\DotNetToolkit.Repository.Xml\DotNetToolkit.Repository.Xml.csproj" />
3334
</ItemGroup>
3435

3536
</Project>

0 commit comments

Comments
 (0)