Application Design

  • Thread starter Thread starter archway
  • Start date Start date
A

archway

Hi,

I am writing an application whereby there is an object model that holds the
data for the application, which is then saved to file. Now, ideally I would
like to open this file and deserialize the object model when the application
loads and then keep this object structure open throughout the duration of
the application.

A couple of questions:

* Is this a good architecture?
* How would you make the object model available to the entire WinForm app?
* How would you instantiate it when the application loads?

Thanks in advance for any advice!
 
Hi, this can easily be done with a DataSet. You declare a static DataSet
(C#) or declare it in a module (VB). When the application starts, call
MyDataSet.ReadXml(). This will fill the dataset with XML data. It would be
easier to have the data in XML format than serializing an object structure.

archway said:
Hi,

I am writing an application whereby there is an object model that holds the
data for the application, which is then saved to file. Now, ideally I would
like to open this file and deserialize the object model when the application
loads and then keep this object structure open throughout the duration of
the application.

A couple of questions:

* Is this a good architecture?
If that is what you need, then it's ok.
* How would you make the object model available to the entire WinForm app?
As I said, you can declare the DataSet as static (C#) or declare it in a
module (VB).
* How would you instantiate it when the application loads?
public static DataSet MyDataSet = new DataSet();
void static Main() {
MyDataSet.ReadXml("C:\\file.xml");
Application.Run(new frmMain());
}
Thanks in advance for any advice!

You could also use a typed dataset, so you would have a strongly-typed data
architecture.

I hope this helps.

Etienne.
 
Hi,

I would create a separate "application logic" layer containing the object
model. All the domain objects would be usual classes, and there would be a
kind of "Advanced Factory" class responsible for creating an empty object
structure, for loading the structure from a disk file (probably with usage
of the Builder design pattern if standard binary serialization is not
enough) and for saving the structure to a disk file.
 
Yes, what you've proposed makes sense. I've had success using this
approach. Also, note:

- If the data you're saving/restoring is user-specific, store it in
"isolated storage" as opposed to a traditional file in the application
folder.
- Of course, all of the objects that you want to serialize should be
marked with the [Serializable()] attribute or implement the ISerializable
interface.
- I typically load the persistent data at startup in Main() and save the
data at shutdown, also in Main().
- I typically call my "parent" (or "root") object which contains all of
the child serializable objects something like "Settings." For example:

internal class Settings{
public static readonly Settings Instance=new Settings();
public void Load(Stream s){
....
}
public void Save(Stream s){
...
}
...various properties...
private Settings(){}
}

Notice the static readonly Instance variable and private constructor.
This ensures that only one instance of the Settings class can exist at any
given time. You can then access your Settings object from anywhere:

Settings.Instance.Username="david";
Settings.Instance.Password="password";

David
 
Back
Top