Skip to content

Commit

Permalink
[Add] Implementation of Repository class
Browse files Browse the repository at this point in the history
  • Loading branch information
natasakasikovic committed Mar 28, 2024
1 parent c699bc1 commit 9e4dee1
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions LangLang/Core/Repository/Repository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using LangLang.Core.Repository.Serialization;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LangLang.Core.Repository
{
public class Repository<T> where T : ISerializable, new()
{
private readonly string _fileName = @"../../../../LangLang/Data/{0}";
private readonly Serializer<T> _serializer = new Serializer<T>();

public Repository(string fileName)
{
_fileName = string.Format(_fileName, fileName);
}
public List<T> Load()
{
if (!File.Exists(_fileName))
{
FileStream fs = File.Create(_fileName);
fs.Close();
}

IEnumerable<string> lines = File.ReadLines(_fileName);
List<T> objects = _serializer.FromCSV(lines);

return objects;
}

public void Save(List<T> objects)
{
string serializedObjects = _serializer.ToCSV(objects);
using (StreamWriter streamWriter = new StreamWriter(_fileName))
{
streamWriter.Write(serializedObjects);
}
}
}
}

0 comments on commit 9e4dee1

Please sign in to comment.