Xml-Speicherung fertig gestellt

This commit is contained in:
mdn 2015-12-05 13:30:47 +01:00
parent cc28b87b9f
commit 2b2f31371a
5 changed files with 58 additions and 38 deletions

View file

@ -5,7 +5,7 @@ using System.Runtime.CompilerServices;
namespace TimeScheduler.Common namespace TimeScheduler.Common
{ {
/// <summary>Basis-Klasse für Objekte, die sich der Oberfläche mitteilen möchten</summary> /// <summary>Basis-Klasse für Objekte, die sich der Oberfläche mitteilen möchten</summary>
abstract class NotifyableObject : INotifyPropertyChanged public abstract class NotifyableObject : INotifyPropertyChanged
{ {
/// <summary>Setzt den neuen Wert auf das Feld, wenn sie unterschiedlich sind und wird das <see cref="PropertyChanged"/>-Ereignis</summary> /// <summary>Setzt den neuen Wert auf das Feld, wenn sie unterschiedlich sind und wird das <see cref="PropertyChanged"/>-Ereignis</summary>
/// <typeparam name="T">Typ des Feldes</typeparam> /// <typeparam name="T">Typ des Feldes</typeparam>

View file

@ -4,13 +4,15 @@ using System.IO;
using System.Runtime.Serialization.Formatters.Binary; using System.Runtime.Serialization.Formatters.Binary;
using System.Xml.Serialization; using System.Xml.Serialization;
using System.Linq; using System.Linq;
using System.Reflection;
namespace TimeScheduler.Model.Impl namespace TimeScheduler.Model.Impl
{ {
class FileDomain : IDomain class FileDomain : IDomain
{ {
#region Konstruktion #region Konstruktion
private List<ITimeItem> allItems_ = new List<ITimeItem>(); private List<TimeItem> allItems_ = new List<TimeItem>();
private string filename_ = string.Empty; private string filename_ = string.Empty;
public FileDomain() public FileDomain()
@ -19,11 +21,11 @@ namespace TimeScheduler.Model.Impl
filename_ = Path.Combine(fi.DirectoryName, fi.Name + ".xmldata"); filename_ = Path.Combine(fi.DirectoryName, fi.Name + ".xmldata");
if (File.Exists(filename_)) if (File.Exists(filename_))
ReadList(filename_); Load();
else else
{ {
// Wenn nichts vorhanden, dann einen Spielsatz anlegen // Wenn nichts vorhanden, dann einen Spielsatz anlegen
allItems_ = new List<ITimeItem> { allItems_ = new List<TimeItem> {
new TimeItem { Description = "Desc 01", From = DateTime.Now.Subtract(TimeSpan.FromHours(2)), Till = DateTime.Now, CostUnit = 14, ItemType = TimeItemType.Normal }, new TimeItem { Description = "Desc 01", From = DateTime.Now.Subtract(TimeSpan.FromHours(2)), Till = DateTime.Now, CostUnit = 14, ItemType = TimeItemType.Normal },
new TimeItem { Description = "Desc 02", From = DateTime.Now.Subtract(TimeSpan.FromHours(2)), Till = DateTime.Now, CostUnit = 13, ItemType = TimeItemType.Break }, new TimeItem { Description = "Desc 02", From = DateTime.Now.Subtract(TimeSpan.FromHours(2)), Till = DateTime.Now, CostUnit = 13, ItemType = TimeItemType.Break },
new TimeItem { Description = "Desc 03", From = DateTime.Now.Subtract(TimeSpan.FromHours(2)), Till = DateTime.Now, CostUnit = 12, ItemType = TimeItemType.Holiday }, new TimeItem { Description = "Desc 03", From = DateTime.Now.Subtract(TimeSpan.FromHours(2)), Till = DateTime.Now, CostUnit = 12, ItemType = TimeItemType.Holiday },
@ -37,32 +39,40 @@ namespace TimeScheduler.Model.Impl
#region Hilfsfunktionen #region Hilfsfunktionen
private void ReadList(string filePath) private void ReadList(string filePath)
{ {
var xs = new XmlSerializer(typeof(List<ITimeItem>));
var bin = new BinaryFormatter(); var bin = new BinaryFormatter();
using (var rd = new StreamReader(filePath)) using (var rd = new FileStream(filePath, FileMode.Open))
{ allItems_ = bin.Deserialize(rd) as List<TimeItem>;
var elems = xs.Deserialize(rd) as List<ITimeItem>;
allItems_ = elems;
//allItems.Clear();
//foreach (var elem in elems)
// allItems.Add(elem);
}
} }
private void WriteList(string path) private void WriteList(string path)
{ {
XmlSerializer xs = new XmlSerializer(typeof(List<TimeItem>));
var bin = new BinaryFormatter(); var bin = new BinaryFormatter();
using (var wr = new FileStream(path, FileMode.OpenOrCreate))
bin.Serialize(wr, allItems_);
}
private void ReadListXml(string filePath)
{
var xs = new XmlSerializer(typeof(List<TimeItem>));
using (var rd = new StreamReader(filePath))
allItems_ = xs.Deserialize(rd) as List<TimeItem>;
}
private void WriteListXml(string path)
{
var xs = new XmlSerializer(typeof(List<TimeItem>));
using (StreamWriter wr = new StreamWriter(path)) using (StreamWriter wr = new StreamWriter(path))
{ xs.Serialize(wr, allItems_);
xs.Serialize(wr, (List<ITimeItem>)allItems_); }
}
private void Load()
{
ReadListXml(filename_);
} }
private void Save() private void Save()
{ {
var newFilename = filename_ + ".new"; var newFilename = filename_ + ".new";
WriteList(newFilename); WriteListXml(newFilename);
File.Delete(filename_); File.Delete(filename_);
File.Move(newFilename, filename_); File.Move(newFilename, filename_);
} }
@ -72,7 +82,7 @@ namespace TimeScheduler.Model.Impl
public List<ITimeItem> GetItems(DateTime date) public List<ITimeItem> GetItems(DateTime date)
{ {
var founded = allItems_.Where(x => x.From.Date == date); var founded = allItems_.Where(x => x.From.Date == date);
return founded.ToList(); return founded.Cast<ITimeItem>().ToList();
} }
public Dictionary<int, string> GetCostUnits() public Dictionary<int, string> GetCostUnits()
@ -91,18 +101,18 @@ namespace TimeScheduler.Model.Impl
public void ElementAdded(ITimeItem timeItem) public void ElementAdded(ITimeItem timeItem)
{ {
allItems_.Add(timeItem); allItems_.Add((TimeItem)timeItem);
Save(); Save();
} }
public void ElementChanged(ITimeItem timeItem) public void ElementChanged(ITimeItem timeItem)
{ {
allItems_.Remove(timeItem); allItems_.Remove((TimeItem)timeItem);
allItems_.Add(timeItem); allItems_.Add((TimeItem)timeItem);
Save(); Save();
} }
public void ElementRemoved(ITimeItem timeItem) public void ElementRemoved(ITimeItem timeItem)
{ {
allItems_.Remove(timeItem); allItems_.Remove((TimeItem)timeItem);
Save(); Save();
} }
#endregion #endregion

View file

@ -1,10 +1,33 @@
using System; using System;
using System.Runtime.Serialization;
using TimeScheduler.Common; using TimeScheduler.Common;
namespace TimeScheduler.Model.Impl namespace TimeScheduler.Model.Impl
{ {
public class TimeItem : NotifyableObject, ITimeItem /// <summary>
///
/// </summary>
/// <remarks>
/// <see cref="ISerializable"/> muss implementiert werden, damit die Daten über den BinarFormater serialisiert werden können.
/// Für den XmlSerializer müsste die Klasse auf Public gestellt werden, was nicht immer gewünscht ist
/// </remarks>
[Serializable]
public class TimeItem : NotifyableObject, ITimeItem//, ISerializable
{ {
public TimeItem() { }
//#region ISerializable
//public TimeItem(SerializationInfo info, StreamingContext context)
//{
// Description = (string)info.GetValue("Description", typeof(string));
//}
//public void GetObjectData(SerializationInfo info, StreamingContext context)
//{
// info.AddValue("Description", Description, typeof(string));
//}
//#endregion
#region ITimeItem
private string description_; private string description_;
public string Description { get { return description_; } set { SetField(ref description_, value); } } public string Description { get { return description_; } set { SetField(ref description_, value); } }
@ -19,5 +42,6 @@ namespace TimeScheduler.Model.Impl
private TimeItemType itemType_; private TimeItemType itemType_;
public TimeItemType ItemType { get { return itemType_; } set { SetField(ref itemType_, value); } } public TimeItemType ItemType { get { return itemType_; } set { SetField(ref itemType_, value); } }
#endregion
} }
} }

View file

@ -1,13 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TimeScheduler.Common;
namespace TimeScheduler.Model
{
abstract class ATimeItem : NotifyableObject, ITimeItem
{
}
}

View file

@ -61,7 +61,6 @@
<Compile Include="Domain\Impl\FileDomain.cs" /> <Compile Include="Domain\Impl\FileDomain.cs" />
<Compile Include="Domain\Impl\TimeItem.cs" /> <Compile Include="Domain\Impl\TimeItem.cs" />
<Compile Include="Domain\TimeItemType.cs" /> <Compile Include="Domain\TimeItemType.cs" />
<Compile Include="Model\ATimeItem.cs" />
<Compile Include="Model\ITimeItem.cs" /> <Compile Include="Model\ITimeItem.cs" />
<Compile Include="ViewModel\MainViewModel.cs" /> <Compile Include="ViewModel\MainViewModel.cs" />
<Page Include="MainWindow.xaml"> <Page Include="MainWindow.xaml">