Skip to content

Commit

Permalink
added working Interface repository compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
joshika39 committed Oct 9, 2023
1 parent 726025d commit 791d6d3
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 38 deletions.
4 changes: 2 additions & 2 deletions _src/Implementation/Configuration/JsonConfigurationQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public async Task SetObjectAsync<T>(string path, T value)
{
var content = await File.ReadAllTextAsync(_filePath);
var jsonObject = SetJsonObject(JObject.Parse(content), path, value);
await File.WriteAllTextAsync(_filePath, jsonObject.ToString(Formatting.Indented));
await File.WriteAllTextAsync(_filePath, jsonObject.ToString());
}
#endregion

Expand All @@ -120,7 +120,7 @@ public void SetObject<T>(string path, T value)
{
var content = File.ReadAllText(_filePath);
var jsonObject = SetJsonObject(JObject.Parse(content), path, value);
File.WriteAllText(_filePath, jsonObject.ToString(Formatting.Indented));
File.WriteAllText(_filePath, jsonObject.ToString());
}
#endregion
#endregion
Expand Down
23 changes: 23 additions & 0 deletions _src/Implementation/Converters/GenericJsonConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using Newtonsoft.Json;

namespace Implementation.Converters
{
public class GenericJsonConverter<TInterface, TConcreteType> : JsonConverter
where TInterface : class
where TConcreteType : class, TInterface
{
public override bool CanConvert(Type objectType)
{
return (objectType == typeof(TInterface));
}
public override object? ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer)
{
return serializer.Deserialize(reader, typeof(TConcreteType));
}
public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer)
{
serializer.Serialize(writer, value, typeof(TConcreteType));
}
}
}
3 changes: 3 additions & 0 deletions _src/Implementation/Implementation.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@






</ItemGroup>


Expand Down
61 changes: 36 additions & 25 deletions _src/Implementation/Repositories/AJsonRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,70 +3,81 @@
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Implementation.Converters;
using Infrastructure.Application;
using Infrastructure.Repositories;
using Newtonsoft.Json;

namespace Implementation.Repositories
{
public abstract class AJsonRepository<T> : IRepository<T> where T : IEntity
public abstract class AJsonRepository<TInterface, TClass> : IRepository<TInterface>
where TInterface : class, IEntity
where TClass : class, TInterface
{
private readonly string _dataPath;
private readonly string _filePath;
private readonly IList<T> _updatedEntities;
private readonly IList<T> _addedEntities;
private readonly IList<TInterface> _updatedEntities;
private readonly IList<TInterface> _addedEntities;
private readonly IList<Guid> _removedEntities;
private bool _isLocked;
private readonly JsonSerializerSettings _settings;

protected AJsonRepository(IApplicationSettings applicationSettings, string repositoryKey)
{
applicationSettings = applicationSettings ?? throw new ArgumentNullException(nameof(applicationSettings));
repositoryKey = repositoryKey ?? throw new ArgumentNullException(nameof(repositoryKey));

_updatedEntities = new List<T>();
_addedEntities = new List<T>();
_updatedEntities = new List<TInterface>();
_addedEntities = new List<TInterface>();
_removedEntities = new List<Guid>();
_dataPath = applicationSettings.RepositoryPath!;
_filePath = Path.Join(_dataPath, $"{repositoryKey}.json");
_settings = new JsonSerializerSettings()
{
Converters = new List<JsonConverter>
{
new GenericJsonConverter<TInterface, TClass>()
}
};
}

public async Task<IEnumerable<T>> GetAllEntitiesAsync()
public async Task<IEnumerable<TInterface>> GetAllEntitiesAsync()
{
return await GetAllContentAsync();
}
public IEnumerable<T> GetAllEntities()
public IEnumerable<TInterface> GetAllEntities()
{
return GetAllContent();
}
public async Task<T?> GetEntityAsync(Guid id)
public async Task<TInterface?> GetEntityAsync(Guid id)
{
var allContent = await GetAllContentAsync();
return allContent.FirstOrDefault(e => e.Id.Equals(id));

}

public T? GetEntity(Guid id)
public TInterface? GetEntity(Guid id)
{
var allContent = GetAllContent();
return allContent.FirstOrDefault(e => e.Id.Equals(id));
}
public IRepository<T> Create(T entity)
public IRepository<TInterface> Create(TInterface entity)
{
_addedEntities.Add(entity);
return this;
}
public IRepository<T> Delete(Guid id)
public IRepository<TInterface> Delete(Guid id)
{
_removedEntities.Add(id);
return this;
}
public IRepository<T> Update(T entity)
public IRepository<TInterface> Update(TInterface entity)
{
_updatedEntities.Add(entity);
return this;
}

public IRepository<T> SaveChanges()
public IRepository<TInterface> SaveChanges()
{
if (_isLocked)
{
Expand Down Expand Up @@ -94,8 +105,8 @@ public IRepository<T> SaveChanges()
currentContent.Remove(currentContent.First(e => e.Id.Equals(id)));
}
}

var newContent = JsonConvert.SerializeObject(currentContent);
var newContent = JsonConvert.SerializeObject(currentContent, _settings);
File.WriteAllText(_filePath, newContent);
_isLocked = false;
return this;
Expand Down Expand Up @@ -129,27 +140,27 @@ private void CreateRepository()
}
}

private async Task<IEnumerable<T>> GetAllContentAsync()
private async Task<IEnumerable<TInterface>> GetAllContentAsync()
{
await SaveChangesAsync();
var allContent = await File.ReadAllTextAsync(_filePath);
var list = await Task.Factory.StartNew(() => JsonConvert.DeserializeObject<IEnumerable<T>>(allContent));
return list ?? new List<T>();
var list = await Task.Factory.StartNew(() => JsonConvert.DeserializeObject<IEnumerable<TInterface>>(allContent, _settings));
return list ?? new List<TInterface>();
}

private IEnumerable<T> GetAllContent()
private IEnumerable<TInterface> GetAllContent()
{
SaveChanges();
var allContent = File.ReadAllText(_filePath);
var list = JsonConvert.DeserializeObject<IEnumerable<T>>(allContent);
return list ?? new List<T>();
var list = JsonConvert.DeserializeObject<IEnumerable<TInterface>>(allContent, _settings);
return list ?? new List<TInterface>();
}

public async Task<IRepository<T>> SaveChangesAsync()
public async Task<IRepository<TInterface>> SaveChangesAsync()
{
if (_isLocked)
{
return await Task.FromResult<IRepository<T>>(this);
return await Task.FromResult<IRepository<TInterface>>(this);
}
await CreateRepositoryAsync();
_isLocked = true;
Expand All @@ -174,10 +185,10 @@ public async Task<IRepository<T>> SaveChangesAsync()
}
}

var newContent = await Task.Factory.StartNew(() => JsonConvert.SerializeObject(currentContent));
var newContent = await Task.Factory.StartNew(() => JsonConvert.SerializeObject(currentContent, _settings));
await File.WriteAllTextAsync(_filePath, newContent);
_isLocked = false;
return await Task.FromResult<IRepository<T>>(this);
return await Task.FromResult<IRepository<TInterface>>(this);
}

public async ValueTask DisposeAsync()
Expand Down
4 changes: 3 additions & 1 deletion _src/Implementation/Repositories/DefaultJsonRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

namespace Implementation.Repositories
{
internal class DefaultJsonRepository<T> : AJsonRepository<T> where T: IEntity
internal class DefaultJsonRepository<TInterface, TClass> : AJsonRepository<TInterface, TClass>
where TInterface: class, IEntity
where TClass: class, TInterface
{
public DefaultJsonRepository(IApplicationSettings applicationSettings, string repositoryKey) : base(applicationSettings, repositoryKey)
{ }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ public RepositoryFactory(IApplicationSettings applicationSettings)
_applicationSettings = applicationSettings ?? throw new ArgumentNullException(nameof(applicationSettings));
}

public IRepository<T> CreateJsonRepository<T>(string repositoryName) where T : IEntity
public IRepository<TInterface> CreateJsonRepository<TInterface, T>(string repositoryName)
where TInterface : class, IEntity
where T : class, TInterface
{
return new DefaultJsonRepository<T>(_applicationSettings, repositoryName);
return new DefaultJsonRepository<TInterface, T>(_applicationSettings, repositoryName);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ namespace Infrastructure.Repositories.Factories
{
public interface IRepositoryFactory
{
IRepository<T> CreateJsonRepository<T>(string repositoryName) where T: IEntity;
IRepository<TInterface> CreateJsonRepository<TInterface, T>(string repositoryName)
where TInterface : class, IEntity
where T : class, TInterface;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public async Task JRT_0001_Given_InExistentJsonRepository_When_ConstructorCalled
var fileName = $@".\data\users-{id}.json";
try
{
await using var repository = CreateRepositoryFactory(@".\data").CreateJsonRepository<User>($"users-{id}");
await using var repository = CreateRepositoryFactory(@".\data").CreateJsonRepository<IUser, User>($"users-{id}");
await repository.DisposeAsync();
Assert.True(File.Exists(fileName));
var text = await File.ReadAllTextAsync(fileName);
Expand All @@ -46,7 +46,7 @@ public async Task JRT_0011_Given_EmptyRepository_When_CreateEntityCalled_Then_En

try
{
await using var repository = CreateRepositoryFactory(@".\data").CreateJsonRepository<User>($"users-{id}");
await using var repository = CreateRepositoryFactory(@".\data").CreateJsonRepository<IUser, User>($"users-{id}");
var user = new User
{
Name = "Peter",
Expand Down Expand Up @@ -75,7 +75,7 @@ public void JRT_0011_Given_EmptyRepository_When_CreateEntityCalledSynchronously_

try
{
using var repository = CreateRepositoryFactory(@".\data").CreateJsonRepository<User>($"users-{id}");
using var repository = CreateRepositoryFactory(@".\data").CreateJsonRepository<IUser, User>($"users-{id}");
var user = new User
{
Name = "Peter",
Expand All @@ -84,9 +84,9 @@ public void JRT_0011_Given_EmptyRepository_When_CreateEntityCalledSynchronously_
repository.Create(user);
repository.SaveChanges();
var jsonString = $"[{{\"Id\":\"{user.Id}\",\"Name\":\"{user.Name}\",\"Age\":{user.Age}}}]";

var text = File.ReadAllText(fileName);
Assert.True(File.Exists(fileName));
Assert.Equal(jsonString, File.ReadAllText(fileName));
Assert.Equal(jsonString, text);
}
finally
{
Expand All @@ -101,7 +101,7 @@ public void JRT_0011_Given_EmptyRepository_When_CreateEntityCalledSynchronously_
public async Task JRT_0021_Given_JsonFile_When_GetAllEntitiesCalled_Then_AllEntitiesReturns()
{
var fileName = @".\Resources\JRT\0021-users.json";
await using var repository = CreateRepositoryFactory(@".\Resources\JRT").CreateJsonRepository<User>("0021-users");
await using var repository = CreateRepositoryFactory(@".\Resources\JRT").CreateJsonRepository<IUser, User>("0021-users");
var allUsers = await repository.GetAllEntitiesAsync();
Assert.True(File.Exists(fileName));
Assert.Equal(4, allUsers.Count());
Expand Down
11 changes: 11 additions & 0 deletions _src/_Tests/ImplementationTest/RepositoryTests/Model/IUser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;
using Infrastructure.Repositories;

namespace ImplementationTest.RepositoryTests.Model
{
public interface IUser : IEntity
{
public string Name { get; set; }
public int Age { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace ImplementationTest.RepositoryTests.Model
{
public class User : IEntity
public class User : IUser
{
public Guid Id { get; } = Guid.NewGuid();
public string Name { get; set; }
Expand Down

0 comments on commit 791d6d3

Please sign in to comment.