diff --git a/LangLang/Core/Repository/Repository.cs b/LangLang/Core/Repository/Repository.cs new file mode 100644 index 00000000..86882f9c --- /dev/null +++ b/LangLang/Core/Repository/Repository.cs @@ -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 where T : ISerializable, new() + { + private readonly string _fileName = @"../../../../LangLang/Data/{0}"; + private readonly Serializer _serializer = new Serializer(); + + public Repository(string fileName) + { + _fileName = string.Format(_fileName, fileName); + } + public List Load() + { + if (!File.Exists(_fileName)) + { + FileStream fs = File.Create(_fileName); + fs.Close(); + } + + IEnumerable lines = File.ReadLines(_fileName); + List objects = _serializer.FromCSV(lines); + + return objects; + } + + public void Save(List objects) + { + string serializedObjects = _serializer.ToCSV(objects); + using (StreamWriter streamWriter = new StreamWriter(_fileName)) + { + streamWriter.Write(serializedObjects); + } + } + } +}