Skip to content
This repository has been archived by the owner on Nov 10, 2022. It is now read-only.

Bug: Id is reserved string in converted objects #22

Merged
merged 11 commits into from
May 20, 2016
5 changes: 1 addition & 4 deletions LazyStorage/InMemory/InMemoryRepositoryWithConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,12 @@ public void Set(T item)
if (matchingItemsInStore.Any())
{
// Update
var obj = matchingItemsInStore;
m_Repository.Remove(obj.First());
m_Repository.Remove(matchingItemsInStore.First());
m_Repository.Add(storableItem);
}
else
{
// Insert
var nextId = m_Repository.Any() ? m_Repository.Max(x => x.Id) + 1 : 1;
storableItem.Id = nextId;
m_Repository.Add(storableItem);
}
}
Expand Down
9 changes: 5 additions & 4 deletions LazyStorage/Json/JsonRepositoryWithConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@ public ICollection<T> Get(Func<T, bool> exp = null)

public void Set(T item)
{
if (m_Repository.Contains(item))
var storableObject = m_Converter.GetStorableObject(item);
var matchingItemsInStore = m_Repository.Where(x => m_Converter.IsEqual(storableObject, x));

if (matchingItemsInStore.Any())
{
// Update
var storableObject = m_Converter.GetStorableObject(item);
var obj = m_Repository.Where(x => m_Converter.IsEqual(storableObject, x));
m_Repository.Remove(obj.First());
m_Repository.Remove(matchingItemsInStore.First());
m_Repository.Add(item);
}
else
Expand Down
8 changes: 1 addition & 7 deletions LazyStorage/StorableObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,13 @@

namespace LazyStorage
{
public class StorableObject : IEquatable<StorableObject>
public class StorableObject
{
public int Id { get; set; }
public Dictionary<string, string> Info { get; }

public StorableObject()
{
Info = new Dictionary<string, string>();
}

public bool Equals(StorableObject other)
{
return (other.Id == Id);
}
}
}
142 changes: 73 additions & 69 deletions LazyStorage/Xml/XmlRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,132 +2,136 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
using LazyStorage.Interfaces;

namespace LazyStorage.Xml
{
internal class XmlRepository<T> : IRepository<T> where T : IStorable<T>, new()
{
private readonly string m_StorageFolder;
private XDocument m_File;
private readonly string m_Uri;
private List<T> m_Repository = new List<T>();

public XmlRepository(string storageFolder)
{
m_StorageFolder = storageFolder;
m_Uri = $"{storageFolder}{typeof(T)}.xml";
Load();
}

public ICollection<T> Get(Func<T, bool> exp = null)
{
ICollection<T> found = new List<T>();

foreach (var node in m_File.Element("Root").Elements())
{
var temp = new T();
var info = new Dictionary<string, string>();

foreach (var element in node.Descendants())
{
info.Add(element.Name.ToString(), element.Value);
}

temp.InitialiseWithStorageInfo(info);

found.Add(temp);
}

var query = found.AsQueryable<T>();
return exp != null ? query.Where(exp).ToList() : found;
return exp != null ? m_Repository.Where(exp).ToList() : m_Repository.ToList();
}

public void Set(T item)
{
var matchingItem = Get(x => x.Equals(item));

if (matchingItem.Any())
if (m_Repository.Contains(item))
{
Update(item);
// Update
var obj = m_Repository.Where(x => x.Equals(item));
m_Repository.Remove(obj.First());
m_Repository.Add(item);
}
else
{
Insert(item);
// Insert
var nextId = m_Repository.Any() ? m_Repository.Max(x => x.Id) + 1 : 1;
item.Id = nextId;
m_Repository.Add(item);
}
}

private void Update(T item)
public void Delete(T item)
{
var info = item.GetStorageInfo();

var rootElement = m_File.Element("Root");
var idXElements = rootElement.Descendants("Id");
var node = idXElements.SingleOrDefault(x => x.Value == item.Id.ToString());

foreach (var data in info)
{
node.Parent.Element(data.Key).Value = data.Value;
}
var obj = m_Repository.SingleOrDefault(x => x.Id == item.Id);
m_Repository.Remove(obj);
}

private void Insert(T item)

public object Clone()
{
var typeAsString = typeof (T).ToString();
var newRepo = new XmlRepository<T>(m_Uri);

foreach (var item in Get())
{
var temp = new T();

var rootElement = m_File.Element("Root");
var idXElements = rootElement.Descendants("Id");
var info = item.GetStorageInfo();

item.Id = idXElements.Any() ? idXElements.Max(x => (int) x) + 1 : 1;
temp.InitialiseWithStorageInfo(info);

var info = item.GetStorageInfo();
newRepo.Set(temp);
}

var newElement = new XElement(typeAsString);
return newRepo;
}

foreach (var data in info)
public void Load()
{
if (File.Exists(m_Uri))
{
newElement.Add(new XElement(data.Key, data.Value));
m_Repository = GetObjectsFromXml(m_Uri);
}
else
{
m_Repository = new List<T>();
}

rootElement.Add(newElement);
}

public void Delete(T item)
public void Save()
{
var rootElement = m_File.Element("Root");
var idXElements = rootElement.Descendants("Id");
var node = idXElements.SingleOrDefault(x => x.Value == item.Id.ToString());

node = node.Parent;
node.Remove();
GetXmlOuput(m_Repository).Save(m_Uri);
}

public object Clone()
private XDocument GetXmlOuput(List<T> objects)
{
var newRepo = new XmlRepository<T>(m_StorageFolder);
var file = new XDocument(new XElement("Root"));

foreach (var item in Get())
{
var temp = new T();
var typeAsString = typeof(T).ToString();

var rootElement = file.Element("Root");

foreach (var item in objects)
{
var info = item.GetStorageInfo();

temp.InitialiseWithStorageInfo(info);
var newElement = new XElement(typeAsString);

foreach (var data in info)
{
newElement.Add(new XElement(data.Key, data.Value));
}

newRepo.Set(temp);
rootElement.Add(newElement);
}

return newRepo;
return file;
}

public void Save()
private List<T> GetObjectsFromXml(string uri)
{
m_File.Save(m_Uri);
}
var file = XDocument.Load(uri);

public void Load()
{
m_File = !File.Exists(m_Uri) ? new XDocument(new XElement("Root")) : XDocument.Load(m_Uri);
var found = new List<T>();

foreach (var node in file.Element("Root").Elements())
{
var storageInfo = new Dictionary<string, string>();

foreach (var element in node.Descendants())
{
storageInfo.Add(element.Name.ToString(), element.Value);
}

var item = new T();
item.InitialiseWithStorageInfo(storageInfo);

found.Add(item);
}

return found;
}
}
}
Loading