From 2b2f31371a655696c4824a9e07b52b62f8b5d561 Mon Sep 17 00:00:00 2001
From: mdn <1stdragon@gmail.com>
Date: Sat, 5 Dec 2015 13:30:47 +0100
Subject: [PATCH] Xml-Speicherung fertig gestellt
---
.../TimeScheduler/Common/NotifyableObject.cs | 2 +-
.../TimeScheduler/Domain/Impl/FileDomain.cs | 54 +++++++++++--------
.../TimeScheduler/Domain/Impl/TimeItem.cs | 26 ++++++++-
.../TimeScheduler/Model/ATimeItem.cs | 13 -----
.../TimeScheduler/TimeScheduler.csproj | 1 -
5 files changed, 58 insertions(+), 38 deletions(-)
delete mode 100644 TimeScheduler/TimeScheduler/Model/ATimeItem.cs
diff --git a/TimeScheduler/TimeScheduler/Common/NotifyableObject.cs b/TimeScheduler/TimeScheduler/Common/NotifyableObject.cs
index 20a673f..3cfe8e3 100644
--- a/TimeScheduler/TimeScheduler/Common/NotifyableObject.cs
+++ b/TimeScheduler/TimeScheduler/Common/NotifyableObject.cs
@@ -5,7 +5,7 @@ using System.Runtime.CompilerServices;
namespace TimeScheduler.Common
{
/// Basis-Klasse für Objekte, die sich der Oberfläche mitteilen möchten
- abstract class NotifyableObject : INotifyPropertyChanged
+ public abstract class NotifyableObject : INotifyPropertyChanged
{
/// Setzt den neuen Wert auf das Feld, wenn sie unterschiedlich sind und wird das -Ereignis
/// Typ des Feldes
diff --git a/TimeScheduler/TimeScheduler/Domain/Impl/FileDomain.cs b/TimeScheduler/TimeScheduler/Domain/Impl/FileDomain.cs
index 4139e68..d3bf89f 100644
--- a/TimeScheduler/TimeScheduler/Domain/Impl/FileDomain.cs
+++ b/TimeScheduler/TimeScheduler/Domain/Impl/FileDomain.cs
@@ -4,13 +4,15 @@ using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Xml.Serialization;
using System.Linq;
+using System.Reflection;
+
namespace TimeScheduler.Model.Impl
{
class FileDomain : IDomain
{
#region Konstruktion
- private List allItems_ = new List();
+ private List allItems_ = new List();
private string filename_ = string.Empty;
public FileDomain()
@@ -19,11 +21,11 @@ namespace TimeScheduler.Model.Impl
filename_ = Path.Combine(fi.DirectoryName, fi.Name + ".xmldata");
if (File.Exists(filename_))
- ReadList(filename_);
+ Load();
else
{
// Wenn nichts vorhanden, dann einen Spielsatz anlegen
- allItems_ = new List {
+ allItems_ = new List {
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 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
private void ReadList(string filePath)
{
- var xs = new XmlSerializer(typeof(List));
var bin = new BinaryFormatter();
- using (var rd = new StreamReader(filePath))
- {
- var elems = xs.Deserialize(rd) as List;
- allItems_ = elems;
- //allItems.Clear();
- //foreach (var elem in elems)
- // allItems.Add(elem);
- }
+ using (var rd = new FileStream(filePath, FileMode.Open))
+ allItems_ = bin.Deserialize(rd) as List;
}
private void WriteList(string path)
{
- XmlSerializer xs = new XmlSerializer(typeof(List));
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));
+ using (var rd = new StreamReader(filePath))
+ allItems_ = xs.Deserialize(rd) as List;
+ }
+ private void WriteListXml(string path)
+ {
+ var xs = new XmlSerializer(typeof(List));
using (StreamWriter wr = new StreamWriter(path))
- {
- xs.Serialize(wr, (List)allItems_);
- }
+ xs.Serialize(wr, allItems_);
+ }
+
+ private void Load()
+ {
+ ReadListXml(filename_);
}
private void Save()
{
var newFilename = filename_ + ".new";
- WriteList(newFilename);
+ WriteListXml(newFilename);
File.Delete(filename_);
File.Move(newFilename, filename_);
}
@@ -72,7 +82,7 @@ namespace TimeScheduler.Model.Impl
public List GetItems(DateTime date)
{
var founded = allItems_.Where(x => x.From.Date == date);
- return founded.ToList();
+ return founded.Cast().ToList();
}
public Dictionary GetCostUnits()
@@ -91,18 +101,18 @@ namespace TimeScheduler.Model.Impl
public void ElementAdded(ITimeItem timeItem)
{
- allItems_.Add(timeItem);
+ allItems_.Add((TimeItem)timeItem);
Save();
}
public void ElementChanged(ITimeItem timeItem)
{
- allItems_.Remove(timeItem);
- allItems_.Add(timeItem);
+ allItems_.Remove((TimeItem)timeItem);
+ allItems_.Add((TimeItem)timeItem);
Save();
}
public void ElementRemoved(ITimeItem timeItem)
{
- allItems_.Remove(timeItem);
+ allItems_.Remove((TimeItem)timeItem);
Save();
}
#endregion
diff --git a/TimeScheduler/TimeScheduler/Domain/Impl/TimeItem.cs b/TimeScheduler/TimeScheduler/Domain/Impl/TimeItem.cs
index 634cfb2..55effe6 100644
--- a/TimeScheduler/TimeScheduler/Domain/Impl/TimeItem.cs
+++ b/TimeScheduler/TimeScheduler/Domain/Impl/TimeItem.cs
@@ -1,10 +1,33 @@
using System;
+using System.Runtime.Serialization;
using TimeScheduler.Common;
namespace TimeScheduler.Model.Impl
{
- public class TimeItem : NotifyableObject, ITimeItem
+ ///
+ ///
+ ///
+ ///
+ /// 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
+ ///
+ [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_;
public string Description { get { return description_; } set { SetField(ref description_, value); } }
@@ -19,5 +42,6 @@ namespace TimeScheduler.Model.Impl
private TimeItemType itemType_;
public TimeItemType ItemType { get { return itemType_; } set { SetField(ref itemType_, value); } }
+ #endregion
}
}
diff --git a/TimeScheduler/TimeScheduler/Model/ATimeItem.cs b/TimeScheduler/TimeScheduler/Model/ATimeItem.cs
deleted file mode 100644
index 953de83..0000000
--- a/TimeScheduler/TimeScheduler/Model/ATimeItem.cs
+++ /dev/null
@@ -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
- {
- }
-}
diff --git a/TimeScheduler/TimeScheduler/TimeScheduler.csproj b/TimeScheduler/TimeScheduler/TimeScheduler.csproj
index 8b16cd0..072d782 100644
--- a/TimeScheduler/TimeScheduler/TimeScheduler.csproj
+++ b/TimeScheduler/TimeScheduler/TimeScheduler.csproj
@@ -61,7 +61,6 @@
-