Better implementation for the serialisation of a List


A

aine_canby

Hi,

I have the following simple class:

/// <summary>
/// Representative of a lab
/// </summary>
public class Lab
{
string name;
public string Name { set { name = value; } get { return
name; } }

string directory;
public string Directory { set { directory = value; } get
{ return directory; } }

/// <summary>
/// Constructor
/// </summary>
public Lab()
{
}

/// <summary>
/// Constructor
/// </summary>
/// <param name="name">Lab Name</param>
/// <param name="directory">Directory Path</param>
public Lab(string name, string directory)
{
this.name = name;
this.directory = directory;
}

public bool Exists()
{
return new DirectoryInfo(directory).Exists;
}
}

I then have another class which stores objects of type Lab, as
follows:

public List<Lab> labs = new List<Lab>();

which I populates during its construction with:

XmlSerializer xmlSerializer = new XmlSerializer(typeof(List<Lab>));

using (StreamReader streamReader = new
StreamReader(xmlLabDirectoriesFileName))
{
labs = (List<Lab>)xmlSerializer.Deserialize(streamReader);
}

// remove labs from the list if they don't exist locally
for (int i = labs.Count - 1; i >= 0; i--)
{
if (!labs.Exists())
labs.Remove(labs);
}

The code works but it feels like it could be done better. Perhaps it
would be smarter with a LabCollection class that would block the
addition of a lab, if it doesn't exist, during serialization - rather
than doing it afterwards as I am doing. What do you think?

Always interested in learning better ways to do things,

Thanks,

Aine.
 
Ad

Advertisements

S

sloan

Its more of a personal preference, but since you asked:

You can actually do this for a collection:


public LabCollection : List<Lab>
{
//yes ,that's all there is.
}


I don't like to mix the objects, and the code that creates the object, so I
either create a
LabController
or
LabManager


public class LabController
{

private LabCollection _labs = new LabCollection ();

public LabCollection GetLabs( LabCollection preexistingLabs )

{

XmlSerializer xmlSerializer = new XmlSerializer(typeof(LabCollection ));

using (StreamReader streamReader = new
StreamReader(xmlLabDirectoriesFileName))
{
_labs = ( LabCollection )xmlSerializer.Deserialize(streamReader);
}

//DOes the preexisting labs take priority? Which one is a subset of the
other?



}

}


or someting like that..... Maybe you just need a static reference to
LabCollection somewhere??

Are you in winforms or web? Are you just "keeping a LabCollection around"?




If you go here:
http://sholliday.spaces.live.com/Blog/cns!A68482B9628A842A!140.entry

and get the source code, you can find a "CustomerFilter", which will show
you how to write a reusable "lab matcher", when you're trying to remove the
non wanted labs.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top