- geographicrepresentation.zip
- GeographicRepresentation
- bin
- Release
- GeographicRepresentation.Interfaces.dll
- GeographicRepresentation.Interfaces.pdb
- GeographicRepresentation.Lib.dll
- GeographicRepresentation.Lib.pdb
- GeographicRepresentation.TestLib.dll
- GeographicRepresentation.TestLib.pdb
- GeographicRepresentation.UILib.exe
- GeographicRepresentation.UILib.pdb
- GeographicRepresentation.UILib.vshost.exe
- GeographicRepresentation.UILib.vshost.exe.manifest
- Resources
- GeographicRepresentation.sln
- GeographicRepresentation.suo
- GeographicRepresentation.TestLib
- GeographicRepresentation.vsmdi
- Interfaces
- Lib
- LocalTestRun.testrunconfig
- TestResults
- UILib
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Serialization;
using GeographicRepresentation.Lib.Model;
namespace GeographicRepresentation.Lib
{
/// <summary>
/// This class uses as the DAL for this application
/// </summary>
public class DataManager
{
#region Members
public const string DATA_FILE_NAME = "Resources\\data.xml";
/// <summary>
/// A hash table for quickly retrieving data over continents
/// </summary>
Dictionary<Guid, Continent> m_ContinentsHash;
/// <summary>
/// A hash table for quickly retrieving data over countries
/// </summary>
Dictionary<Guid, Country> m_CountriesHash;
/// <summary>
/// A hash table for quickly retrieving data over cities
/// </summary>
Dictionary<Guid, City> m_CitiesHash;
#endregion
#region Singleton
static DataManager m_Instance = null;
public static DataManager Instance
{
get
{
if (m_Instance == null)
{
m_Instance = new DataManager();
}
return m_Instance;
}
}
private DataManager()
{
m_ContinentsHash = new Dictionary<Guid, Continent>();
m_CountriesHash = new Dictionary<Guid, Country>();
m_CitiesHash = new Dictionary<Guid, City>();
LoadDataFromStorage();
}
#endregion
#region Methods
/// <summary>
/// Get the data from the storage
/// </summary>
public void LoadDataFromStorage()
{
using (FileStream f = new FileStream(DATA_FILE_NAME, FileMode.Open))
{
try
{
XmlSerializer deserializer = new XmlSerializer(typeof(List<Continent>));
List<Continent> list = (List<Continent>)deserializer.Deserialize(f);
m_ContinentsHash.Clear();
m_CountriesHash.Clear();
m_CitiesHash.Clear();
foreach (Continent continent in list)
{
m_ContinentsHash.Add(continent.Id, continent);
foreach (Country country in continent.Countries)
{
m_CountriesHash.Add(country.Id, country);
foreach (City city in country.Cities)
{
m_CitiesHash.Add(city.Id, city);
}
}
}
}
catch
{
}
finally
{
if (f != null)
{
f.Close();
}
}
}
}
/// <summary>
/// Rewrite the data into the storage
/// </summary>
public void SaveDataIntoStorage()
{
List<Continent> list = m_ContinentsHash.Values.ToList();
using (FileStream f = new FileStream(DATA_FILE_NAME, FileMode.Create))
{
try
{
XmlSerializer serializer = new XmlSerializer(typeof(List<Continent>));
serializer.Serialize(f, list);
}
catch
{
}
finally
{
if (f != null)
{
f.Close();
}
}
}
LoadDataFromStorage();
}
/// <summary>
/// Return a list of all the data which the data manager holds
/// </summary>
/// <returns></returns>
public List<Continent> GetAllContinents()
{
return m_ContinentsHash.Values.ToList();
}
/// <summary>
/// Return data of a specific continent.
/// </summary>
/// <param name="continentId"></param>
/// <returns></returns>
public Continent GetContinent(Guid continentId)
{
Continent continent = null;
m_ContinentsHash.TryGetValue(continentId, out continent);
return continent;
}
/// <summary>
/// Return data of a specific country.
/// </summary>
/// <param name="continentId"></param>
/// <returns></returns>
public Country GetCountry(Guid countryId)
{
Country country = null;
m_CountriesHash.TryGetValue(countryId, out country);
return country;
}
/// <summary>
/// Returns data of a specific city
/// </summary>
/// <param name="cityId"></param>
/// <returns></returns>
public City GetCity(Guid cityId)
{
City city = null;
m_CitiesHash.TryGetValue(cityId, out city);
return city;
}
/// <summary>
/// Add a new continent
/// </summary>
/// <param name="continent"></param>
public void AddContinent(Continent continent)
{
Continent temp;
if (!m_ContinentsHash.TryGetValue(continent.Id, out temp))
{
m_ContinentsHash.Add(continent.Id, continent);
}
}
/// <summary>
/// Remove a continent from the list
/// </summary>
/// <param name="continentId"></param>
public void RemoveContinent(Guid continentId)
{
Continent continent;
if (m_ContinentsHash.TryGetValue(continentId, out continent))
{
// Remove all the countries which belong to the continent
for (int iter = continent.Countries.Count - 1; iter > -1; iter--)
{
RemoveCountry(continent.Countries[iter].Id);
}
m_ContinentsHash.Remove(continentId);
SaveDataIntoStorage();
}
}
/// <summary>
/// Remove a country from the list
/// </summary>
/// <param name="countryId"></param>
public void RemoveCountry(Guid countryId)
{
Country country;
if (m_CountriesHash.TryGetValue(countryId, out country))
{
// Remove all the cities which belong to the country
for (int iter = country.Cities.Count - 1; iter > -1; iter--)
{
RemoveCity(country.Cities[iter].Id);
}
m_CountriesHash.Remove(countryId);
Continent continent = m_ContinentsHash.Values.FirstOrDefault(c => c.Countries.FirstOrDefault(d => d.Id == country.Id) != null);
if (continent != null)
{
continent.Countries.Remove(continent.Countries.First(c => c.Id == country.Id));
}
SaveDataIntoStorage();
}
}
/// <summary>
/// Remove a city from the list
/// </summary>
/// <param name="cityId"></param>
public void RemoveCity(Guid cityId)
{
City city;
if (m_CitiesHash.TryGetValue(cityId, out city))
{
m_CitiesHash.Remove(cityId);
Country country = m_CountriesHash.Values.FirstOrDefault(c => c.Cities.FirstOrDefault(d => d.Id == city.Id) != null);
if (country != null)
{
country.Cities.Remove(country.Cities.First(c => c.Id == city.Id));
}
}
SaveDataIntoStorage();
}
#endregion
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.