Re: OOP object collections




I started out 2.0 code with a bunch of

List<SomeObject>

but since..have highly favored going to

public class SomeObjectCollection : List<SomeObject>
{}

and I'm future proofed, in case I ever need to write special methods for
just the collection.

..............




"sloan" <sloan@xxxxxxxxx> wrote in message
news:Ow8WBRaaIHA.3828@xxxxxxxxxxxxxxxxxxxxxxx

I do this.

1 interface for the business object.
1 interface for the collection.
1 concrete business object : interface
1 concrete collection : interface

// object interface
namespace MeasInc.Applications.ZooropaManager.Interfaces.Zebra
{

public interface IZebra : IComparable<IZebra>
{
Guid ZebraUUID { get; set; }
System.DateTime CreateDate { get; set; }
string ZebraName { get; set; }
int CompareTo(IZebra other, Comparers.ZebraType comparisonType);

}



}


// collection interface

namespace MeasInc.Applications.ZooropaManager.Interfaces.Zebra
{


public interface IZebraCollection : IList< IZebra >
{

void Sort();
void Sort(IComparer<IZebra> icomp);

}


}




// object concrete

using MeasInc.Applications.ZooropaManager.Interfaces.Zebra;
using MeasInc.Applications.ZooropaManager.Interfaces.Comparers ;


namespace
MeasInc.Applications.ZooropaManager.BusinessLogic.BusinessObjects
{

[Serializable]
[DataContract] // WCF specific, you can remove
public class Zebra : IZebra
{
[DataMember] // WCF specific, you can remove

private Guid _zebraUUID;

[DataMember] // WCF specific, you can remove

private System.DateTime _createDate;

[DataMember] // WCF specific, you can remove

private string _zebraName;





public Zebra() { } //unnecessary, but a placeholder

#region IZebra Members

public Zebra(Guid zebraUUID)
{
this.ZebraUUID = zebraUUID;
}

public Zebra(Guid zebraUUID, string zebraName, System.DateTime
createDate)
{
this.ZebraUUID = zebraUUID;
this.CreateDate = createDate;
this.ZebraName = zebraName;

}


#endregion

#region IZebra Members

public Guid ZebraUUID
{
get { return _zebraUUID; }
set { _zebraUUID = value; }
}

public System.DateTime CreateDate
{
get { return _createDate; }
set { _createDate = value; }
}
public string ZebraName
{
get { return _zebraName; }
set { _zebraName = value; }
}


#endregion



#region IComparable<IZebra> Members

public int CompareTo(IZebra other)
{
return this.ZebraUUID .CompareTo(other.ZebraUUID );
}

#endregion






}

}




//concrete collection

using MeasInc.Applications.ZooropaManager.Interfaces.Zebra;

namespace MeasInc.Applications.ZooropaManager.BusinessLogic.Collections
{
[Serializable]
[CollectionDataContract] // WCF specific, you can remove

public class ZebraCollection : List<IZebra> , IZebraCollection
{
//yeah, that's it
}
}





...............// stop cs code

You can actually get these code samples at:
http://sholliday.spaces.live.com/Blog/cns!A68482B9628A842A!158.entry

and just kinda ignore the wcf stuff.



.....

As far as creation, I highly recommend seperating the objects/collections
away from the code that creates them.
Use a Controller or Manager class for object / collection creation.

Which is also seen at the code sample above.



Try out my code... I think your EmployeeCollection may be more verbose
than you need.

Good luck.





"RSH" <way_beyond_oops@xxxxxxxxx> wrote in message
news:ebNPNAZaIHA.4284@xxxxxxxxxxxxxxxxxxxxxxx
Hi,

I have a basic question around how to handle collections of objects when
using object composition.

In my sample below I have a typical arrangement, I have a Company class
and a Employee class. My question is whether I should create a collection
IN the company class to store the employees, or as in my sample below,
create a EmployeeCollection class which is responsible for managing the
collection of employees?

It seems like it is a better construct to have a seperate class to manage
the collection...am I wrong there?

Thanks,
Ron

public class Company
{
private int m_Id;
private string m_Name;
private EmployeeCollection m_EmployeeCollection = null;

public Company(int Id, string Name)
{
m_Id = Id;
m_Name = Name;
LoadEmployees();
}

public int Id { get { return m_Id;}}
public string Name { get { return m_Name; } set { m_Name = value; } }

protected void LoadEmployees()
{
Employee employee = null;
m_EmployeeCollection = new EmployeeCollection(this);
// Load employees from datasource into EmployeeCollection object
employee = new Employee(this, 0, "Smith", "Doug");
m_EmployeeCollection.Add(employee);
}
}

public class EmployeeCollection
{
private List<Employee> m_EmployeeList = new List<Employee>();
private Company m_Parent = null;

public EmployeeCollection(Company Parent)
{
m_Parent = Parent;
}

public IEnumerable<Employee> GetEmployees()
{
foreach (Employee employee in m_EmployeeList)
{
yield return employee;
}
}

public void Add(Employee employee)
{
m_EmployeeList.Add(employee);
}

public void Remove(int Id)
{
foreach (Employee employee in m_EmployeeList)
{
if (employee.Id == Id)
{
m_EmployeeList.Remove(employee);
}
}
}
}

public class Employee
{
private int m_Id;
private string m_LastName;
private string m_FirstName;
private Company m_Parent = null;

public Employee(Company Parent,int Id, string LastName, string
FirstName)
{
m_Id = Id;
m_LastName = LastName;
m_FirstName = FirstName;
m_Parent = Parent;
}

public int Id { get { return m_Id;}}
public string LastName { get { return m_LastName; } set { m_LastName =
value; } }
public string FirstName { get { return m_FirstName; } set { m_FirstName
= value; } }
}





.



Relevant Pages

  • Re: Using Httpwebrequest to Submit multipart/form-data
    ... > End Sub ... > Private Sub btnSignOn_Click(ByVal sender As System.Object, ... > Private Function PostRequest(ByVal strURL As String, ... > Public Class Parameters ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Using Httpwebrequest to Submit multipart/form-data
    ... > End Sub ... > Private Sub btnSignOn_Click(ByVal sender As System.Object, ... > Private Function PostRequest(ByVal strURL As String, ... > Public Class Parameters ...
    (microsoft.public.dotnet.framework)
  • Re: Using Httpwebrequest to Submit multipart/form-data
    ... > End Sub ... > Private Sub btnSignOn_Click(ByVal sender As System.Object, ... > Private Function PostRequest(ByVal strURL As String, ... > Public Class Parameters ...
    (microsoft.public.dotnet.general)
  • Re: OOP object collections
    ... ZebraUUID OR Name OR CreateDate. ... string ZebraName ... private System.DateTime _createDate; ... and a Employee class. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: OOP object collections
    ... string FirstName ... public sealed class Employee: Workshop.IEmployee ... private string m_LastName; ... public interface IEmployeeCollection: IList ...
    (microsoft.public.dotnet.languages.csharp)