Nikita Romanenko
6 years ago
11 changed files with 189 additions and 116 deletions
@ -1,8 +0,0 @@ |
|||
namespace Pores.Controllers |
|||
{ |
|||
public abstract class BaseSaveController<TData> |
|||
{ |
|||
public abstract void Save(TData data); |
|||
public abstract TData Load(); |
|||
} |
|||
} |
@ -0,0 +1,8 @@ |
|||
namespace Pores.Controllers |
|||
{ |
|||
public abstract class BaseSaveLoadController |
|||
{ |
|||
public abstract void Save<DataType>(DataType data, string fileName); |
|||
public abstract DataType Load<DataType>(); |
|||
} |
|||
} |
@ -1,47 +0,0 @@ |
|||
using Newtonsoft.Json; |
|||
using Pores.Models; |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.IO; |
|||
using System.Windows; |
|||
|
|||
namespace Pores.Controllers |
|||
{ |
|||
public class PoresToFileSaver : BaseSaveController<IEnumerable<Pore>> |
|||
{ |
|||
public string FileName { get; set; } |
|||
|
|||
public override IEnumerable<Pore> Load() |
|||
{ |
|||
try |
|||
{ |
|||
var data = File.ReadAllText(FileName); |
|||
var output = JsonConvert.DeserializeObject<IList<Pore>>(data); |
|||
return output; |
|||
} |
|||
catch (Exception ex) |
|||
{ |
|||
MessageBox.Show(ex.Message); |
|||
return null; |
|||
} |
|||
} |
|||
|
|||
public override void Save(IEnumerable<Pore> data) |
|||
{ |
|||
var output = JsonConvert.SerializeObject(data); |
|||
try |
|||
{ |
|||
File.WriteAllText(FileName, output); |
|||
} |
|||
catch (Exception ex) |
|||
{ |
|||
MessageBox.Show(ex.Message); |
|||
} |
|||
} |
|||
|
|||
public PoresToFileSaver(string fileName = "not_defined") |
|||
{ |
|||
FileName = fileName; |
|||
} |
|||
} |
|||
} |
@ -0,0 +1,56 @@ |
|||
using Microsoft.Win32; |
|||
using Newtonsoft.Json; |
|||
using Pores.Interfaces; |
|||
using Pores.Models; |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.IO; |
|||
using System.Linq; |
|||
using System.Windows; |
|||
|
|||
namespace Pores.Controllers |
|||
{ |
|||
public class ReportGenerator : BaseSaveLoadController |
|||
{ |
|||
public string FileNamer(string fileName) |
|||
{ |
|||
return |
|||
".\\results\\" |
|||
+ fileName + "_" |
|||
+ DateTime.Now.ToString("yyyyMMdd_HHmmss") |
|||
+ ".mdl"; |
|||
} |
|||
|
|||
public override DataType Load<DataType>() |
|||
{ |
|||
try |
|||
{ |
|||
var dialog = new OpenFileDialog(); |
|||
if (dialog.ShowDialog() != true) |
|||
return default(DataType); |
|||
var data = File.ReadAllText(dialog.FileName); |
|||
var output = JsonConvert.DeserializeObject<DataType>(data); |
|||
return output; |
|||
} |
|||
catch (Exception ex) |
|||
{ |
|||
MessageBox.Show(ex.Message); |
|||
return default(DataType); |
|||
} |
|||
} |
|||
|
|||
public override void Save<DataType>(DataType data, string fileName) |
|||
{ |
|||
var output = JsonConvert.SerializeObject(data); |
|||
try |
|||
{ |
|||
if (!Directory.Exists(".\\results\\")) Directory.CreateDirectory(".\\results\\"); |
|||
File.WriteAllText(fileName, output); |
|||
} |
|||
catch (Exception ex) |
|||
{ |
|||
MessageBox.Show(ex.Message); |
|||
} |
|||
} |
|||
} |
|||
} |
Loading…
Reference in new issue