By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,161 Members | 1,071 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,161 IT Pros & Developers. It's quick & easy.

Better implementation for the serialisation of a List

P: n/a
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<Lablabs = 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[i].Exists())
labs.Remove(labs[i]);
}

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.

Oct 24 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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.

<ai********@yahoo.comwrote in message
news:11*********************@t8g2000prg.googlegrou ps.com...
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<Lablabs = 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[i].Exists())
labs.Remove(labs[i]);
}

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.

Oct 24 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.