Made repository methods asynchronous. Simplified playlist repository. Use playlist manager in the app, and left playlist repository to only be used by the playlist manager.
This commit is contained in:
@@ -8,10 +8,10 @@ public abstract class FileRepository<TObject> : IRepository<TObject> where TObje
|
||||
protected abstract string Extension { get; }
|
||||
|
||||
protected abstract string GetNewFileName();
|
||||
protected abstract string Serialize(TObject playlist);
|
||||
protected abstract TObject Deserialize(Stream stream);
|
||||
protected abstract Task<string> SerializeAsync(TObject playlist);
|
||||
protected abstract Task<TObject> DeserializeAsync(Stream stream);
|
||||
|
||||
public FileRepository()
|
||||
public async Task InitializeAsync()
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(DirectoryName))
|
||||
return;
|
||||
@@ -23,7 +23,7 @@ public abstract class FileRepository<TObject> : IRepository<TObject> where TObje
|
||||
return;
|
||||
|
||||
List<string> fileNames = GetAllFileNames();
|
||||
LoadFileNamesIntoMap(fileNames);
|
||||
await LoadFileNamesIntoMapAsync(fileNames);
|
||||
}
|
||||
|
||||
private List<string> GetAllFileNames()
|
||||
@@ -36,7 +36,7 @@ public abstract class FileRepository<TObject> : IRepository<TObject> where TObje
|
||||
return [.. fileInfoList.Select(fileInfo => fileInfo.FullName)];
|
||||
}
|
||||
|
||||
private void LoadFileNamesIntoMap(List<string> fileNames)
|
||||
private async Task LoadFileNamesIntoMapAsync(List<string> fileNames)
|
||||
{
|
||||
foreach (var fileName in fileNames)
|
||||
{
|
||||
@@ -44,7 +44,7 @@ public abstract class FileRepository<TObject> : IRepository<TObject> where TObje
|
||||
|
||||
try
|
||||
{
|
||||
TObject obj = Deserialize(textReader.BaseStream);
|
||||
TObject obj = await DeserializeAsync(textReader.BaseStream);
|
||||
_fileNameMap.Add(obj, fileName);
|
||||
}
|
||||
catch (Exception)
|
||||
@@ -59,9 +59,9 @@ public abstract class FileRepository<TObject> : IRepository<TObject> where TObje
|
||||
return [.. _fileNameMap.Keys];
|
||||
}
|
||||
|
||||
public void Save(TObject obj)
|
||||
public async Task SaveAsync(TObject obj)
|
||||
{
|
||||
string serializedObject = Serialize(obj);
|
||||
string serializedObject = await SerializeAsync(obj);
|
||||
|
||||
string fileName = Path.Combine(DirectoryName, GetFileName(obj));
|
||||
string? path = Path.GetDirectoryName(fileName);
|
||||
@@ -72,8 +72,10 @@ public abstract class FileRepository<TObject> : IRepository<TObject> where TObje
|
||||
if (Directory.Exists(path) == false)
|
||||
Directory.CreateDirectory(path);
|
||||
|
||||
using TextWriter textWriter = new StreamWriter(fileName);
|
||||
textWriter.Write(serializedObject);
|
||||
//using TextWriter textWriter = new StreamWriter(fileName);
|
||||
//await textWriter.WriteAsync(serializedObject);
|
||||
|
||||
await File.WriteAllTextAsync(fileName, serializedObject);
|
||||
}
|
||||
|
||||
private string GetFileName(TObject obj)
|
||||
@@ -91,6 +93,8 @@ public abstract class FileRepository<TObject> : IRepository<TObject> where TObje
|
||||
{
|
||||
string fileName = Path.Combine(DirectoryName, GetFileName(obj));
|
||||
|
||||
_fileNameMap.Remove(obj);
|
||||
|
||||
if (File.Exists(fileName))
|
||||
File.Delete(fileName);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user