Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / October 2007

Tip: Looking for answers? Try searching our database.

Better implementation for the serialisation of a List

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
aine_canby@yahoo.com - 24 Oct 2007 10:10 GMT
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[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.
sloan - 24 Oct 2007 14:15 GMT
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.

> Hi,
>
[quoted text clipped - 69 lines]
>
> Aine.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.