generated from kzi-nastava/dotnet-wpf-starter-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Add] Implementation of Repository class
- Loading branch information
1 parent
c699bc1
commit 9e4dee1
Showing
1 changed file
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} |