Is this a sensible way to implement a model?
I've created the following model for an academic project, and I'm wondering if this is a sensible way to manage a model:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using System.Collections.ObjectModel;
using System.Windows.Forms;
namespace IceCreamShop
{
[Serializable]
public class Customer : ISerializable
{
private const string SAVE_FILE_NAME = @".\Customers.bin";
#region static model accessors
private static List<Customer> customers = new List<Customer>();
public static ReadOnlyCollection<Customer> Customers
{
get
{
return customers.AsReadOnly();
}
}
public static int Cardinality { get; private set; }
public static void saveCustomers(List<Customer> customers)
{
IFormatter formatter = new BinaryFormatter();
Stream stream = File.Open(SAVE_FILE_NAME, FileMode.Create);
formatter.Serialize(stream, customers);
formatter.Serialize(stream, Customer.Cardinality);
stream.Close();
}
public static void loadAllCustomers()
{
IFormatter formatter = new BinaryFormatter();
List<Customer> customers = new List<Customer>();
try
{
Stream stream = File.Open(SAVE_FILE_NAME, FileMode.OpenOrCreate);
if (stream.Length > 0)
{
customers = (List<Customer>)formatter.Deserialize(stream);
Customer.Cardinality = (int)formatter.Deserialize(stream);
}
stream.Close();
}
// Shouldn't happen during normal operation, pending any permissions/file write issues
catch (Exception ex)
{
throw ex;
}
if(customers.Count == 0)
{
// Dummy customer for patrons who don't want their data tracked
customers.Add(new Customer("nobody", "nobody"));
Customer.Cardinality = 1;
}
Customer.customers = customers;
}
#endregion
#region individual model data
private string firstName;
public string FirstName { get { return firstName; } }
private string lastName;
public string LastName { get { return lastName; } }
private int id;
public int ID { get { return id; } }
//Used externally to generate a new customer
public Customer(string firstName, string lastName)
{
this.id = Customer.Cardinality++;
this.firstName = firstName;
this.lastName = lastName;
}
#endregion
#region serialization
Customer(SerializationInfo info, StreamingContext context)
{
this.id = info.GetInt32("i");
this.firstName = info.GetString("j");
this.lastName = info.GetString("k");
I've created the following model for an academic project, and I'm wondering if this is a sensible way to manage a model:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using System.Collections.ObjectModel;
using System.Windows.Forms;
namespace IceCreamShop
{
[Serializable]
public class Customer : ISerializable
{
private const string SAVE_FILE_NAME = @".\Customers.bin";
#region static model accessors
private static List<Customer> customers = new List<Customer>();
public static ReadOnlyCollection<Customer> Customers
{
get
{
return customers.AsReadOnly();
}
}
public static int Cardinality { get; private set; }
public static void saveCustomers(List<Customer> customers)
{
IFormatter formatter = new BinaryFormatter();
Stream stream = File.Open(SAVE_FILE_NAME, FileMode.Create);
formatter.Serialize(stream, customers);
formatter.Serialize(stream, Customer.Cardinality);
stream.Close();
}
public static void loadAllCustomers()
{
IFormatter formatter = new BinaryFormatter();
List<Customer> customers = new List<Customer>();
try
{
Stream stream = File.Open(SAVE_FILE_NAME, FileMode.OpenOrCreate);
if (stream.Length > 0)
{
customers = (List<Customer>)formatter.Deserialize(stream);
Customer.Cardinality = (int)formatter.Deserialize(stream);
}
stream.Close();
}
// Shouldn't happen during normal operation, pending any permissions/file write issues
catch (Exception ex)
{
throw ex;
}
if(customers.Count == 0)
{
// Dummy customer for patrons who don't want their data tracked
customers.Add(new Customer("nobody", "nobody"));
Customer.Cardinality = 1;
}
Customer.customers = customers;
}
#endregion
#region individual model data
private string firstName;
public string FirstName { get { return firstName; } }
private string lastName;
public string LastName { get { return lastName; } }
private int id;
public int ID { get { return id; } }
//Used externally to generate a new customer
public Customer(string firstName, string lastName)
{
this.id = Customer.Cardinality++;
this.firstName = firstName;
this.lastName = lastName;
}
#endregion
#region serialization
Customer(SerializationInfo info, StreamingContext context)
{
this.id = info.GetInt32("i");
this.firstName = info.GetString("j");
this.lastName = info.GetString("k");
No comments:
Post a Comment