pp/1/Item.cs 3.5 KB (3542b); Sat, 04 Jun 2011 17:42
´╗┐using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Xml;
using System.Xml.Serialization;

[XmlType("i")]
public class Item
{
	[XmlAttribute("i")]
	public int Id { get; set; }

	[XmlAttribute("n")]
	public string Name { get; set; }

	[XmlIgnore]
	public List<Category> Categories { get; set; }
}

[XmlType("c")]
public class Category
{
	[XmlAttribute("i")]
	public int Id { get; set; }

	[XmlAttribute("n")]
	public string Name { get; set; }

	[XmlIgnore]
	public List<Item> Items { get; set; }
}

[XmlType("l")]
public class IFC
{
	public IFC() { }

	public IFC(int item, int cat)
	{
		Item = item;
		Category = cat;
	}

	[XmlAttribute("i")]
	public int Item { get; set; }

	[XmlAttribute("c")]
	public int Category { get; set; }
}

[XmlRoot("store")]
public class ItemStore
{
	[XmlArray("cs")]
	public List<Category> Categories { get; set; }

	[XmlArray("is")]
	public List<Item> Items { get; set; }

	[XmlArray("ls")]
	public List<IFC> Links { get; set; }
}

public static class ItemMgr
{
	private static readonly ItemStore s;
	private static readonly XmlHelper h = new XmlHelper();

	static ItemMgr()
	{
		s = h.Read() ?? new ItemStore
		{
			Categories = new List<Category>(),
			Items = new List<Item>(),
			Links = new List<IFC>()
		};
		s.Links.ForEach(x => AddPvt(x.Item, x.Category));
	}

	#region Public

	public static Item[] AllItems { get { return s.Items.ToArray(); } }

	public static Category[] AllCategories { get { return s.Categories.ToArray(); } }

	public static void Add(int item, int cat)
	{
		AddPvt(item, cat);
		Save();
	}

	public static void Remove(int item, int cat)
	{
		var i = s.Items.Find(x => x.Id == item);
		var c = s.Categories.Find(x => x.Id == cat);
		s.Links.Remove(s.Links.Find(x => x.Item == item && x.Category == cat));
		i.Categories.Remove(c);
		c.Items.Remove(i);
		Save();
	}

	public static void NewItem(string name)
	{
		var id = s.Items.Count == 0 ? 1 : s.Items.Max(x => x.Id) + 1;
		s.Items.Add(new Item
		{
			Id = id,
			Name = name,
			Categories = new List<Category>()
		});
		Save();
	}

	public static void NewCat(string name)
	{
		var id = s.Categories.Count == 0 ? 1 : s.Categories.Max(x => x.Id) + 1;
		s.Categories.Add(new Category
		{
			Id = id,
			Name = name,
			Items = new List<Item>()
		});
		Save();
	}

	#endregion

	private static void AddPvt(int item, int cat)
	{
		var i = s.Items.Find(x => x.Id == item);
		var c = s.Categories.Find(x => x.Id == cat);
		s.Links.Add(new IFC(item, cat));
		i.Categories.Add(c);
		c.Items.Add(i);
	}

	private static void Save()
	{
		h.Save(s);
	}

	private class XmlHelper
	{
		private readonly string XmlFile = HttpContext.Current.Server.MapPath("~/App_Data/ItemStore.xml");
		private readonly XmlSerializer xs = new XmlSerializer(typeof(ItemStore));

		public void Save(ItemStore s)
		{
			var w = new StringWriter();
			var xw = new XmlTextWriter(w);
			xw.Formatting = Formatting.Indented;
			xs.Serialize(xw, s);
			xw.Flush();
			xw.Close();

			var content = w.ToString();
			if (File.Exists(XmlFile)) File.Delete(XmlFile);
			File.WriteAllText(XmlFile, content);
		}

		public ItemStore Read()
		{
			if (File.Exists(XmlFile) == false) return null;
			var r = new StringReader(File.ReadAllText(XmlFile));
			var xr = new XmlTextReader(r);
			var obj = xs.Deserialize(xr);
			return (ItemStore)obj;
		}
	}
}