OOP object collections

S

sloan

Just a final few things.


IEmployeeCollection ec = new EmployeeCollection();

Notice I have the interface here.

Try your intellisense...and you'll see:

ec.Find( .......... ); //single find
ec.FindAll(............);//multiple find

"magically" included in the definition.

You'll have to learn how to write a Filter and Predicate....but the methods
are ready to go.

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

I asked about whether an employee is ONLY IN A SINGLE company.

If so, or the majority of time...then you good.

If you have ALOT of employees, and they criss cross companies...you'll want
to investigate this design pattern
http://www.dofactory.com/Patterns/PatternFlyweight.aspx
FlyWeight.


Good luck, I think you're on the right track (what else would I say??)
 
M

Marc Gravell

Just a minor aside; remember that in .NET 3.5 with C# 3 you can get
these things for free anyway courtesy of LINQ and the extension
methods on IEnumerable<T> etc.

Marc
 
R

RSH

Sloan,

Im struggling a bit with trying to add a custom sort method. I want to be
able to sort by LastName, FirstName or ID.
....and Im having a little trouble figuring out where the different
components go.

here is kind of where I am heading:

Employee Classes:
public interface IEmployee : IComparable<IEmployee>

{

string FirstName { get; set; }

int Id { get; set; }

string LastName { get; set; }

}

public sealed class Employee : Workshop.IEmployee

{

private int m_Id;

private string m_LastName;

private string m_FirstName;

private ICompany m_Parent = null;

public Employee(ICompany 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; } set { m_Id = value; } }

public string LastName { get { return m_LastName; } set { m_LastName =
value; } }

public string FirstName { get { return m_FirstName; } set { m_FirstName =
value; } }

public int CompareTo(IEmployee other)

{

switch (m_SortBy)

{

case EmployeeSortType.Id:

return this.m_Id.CompareTo(other.Id);

case EmployeeSortType.FirstName:

return this.m_FirstName.CompareTo(other.FirstName);

case EmployeeSortType.LastName:

return this.m_LastName.CompareTo(other.LastName);

default:

return 0;

}

}

}

public enum EmployeeSortType

{

Id, FirstName, LastName

}





Company Class



public interface ICompany

{

int Id { get;}

string Name { get;set;}

IEmployeeCollection Employees { get;}

void PrintEmployees();

void LoadEmployees();

}

public interface IEmployeeCollection : IList<IEmployee>

{

EmployeeSortType SortBy { get;set;}

void Sort();

}

public sealed class Company : ICompany

{

private int m_Id;

private string m_Name;

private IEmployeeCollection m_EmployeeCollection = null;

public Company(int Id, string Name)

{

m_Id = Id;

m_Name = Name;

}

public int Id { get { return m_Id; } }

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

public IEmployeeCollection Employees

{

get

{

return m_EmployeeCollection;

}

}

public void LoadEmployees()

{

m_EmployeeCollection = new EmployeeCollection();

CompanyController.GetEmployees(this);

}

public void PrintEmployees()

{

foreach (Employee employee in m_EmployeeCollection)

{

Console.WriteLine(string.Format("{0}\t{1}\t{2}", employee.Id,
employee.LastName, employee.FirstName));

}

}

}

public class EmployeeCollection : List<IEmployee>, IEmployeeCollection

{

private EmployeeSortType m_SortBy = EmployeeSortType.LastName;

public EmployeeSortType SortBy { get { return m_SortBy; } set { m_SortBy =
value; } }

}



Company Controller

public static class CompanyController

{

public static ICompany GetCompany(int companyId)

{

return new Company(1001, "Bills House of Bugs");

}

public static void GetEmployees(ICompany company)

{

IEmployee employee = null;

employee = new Employee(company, 1000, "House", "William");

company.Employees.Add(employee);

employee = new Employee(company, 1001, "Appleyard", "Katie");

company.Employees.Add(employee);

employee = new Employee(company, 1002, "Costen", "Rosa");

company.Employees.Add(employee);

employee = new Employee(company, 1003, "Herhuth", "Ron");

company.Employees.Add(employee);

}

}
 
R

RSH

Oops...I hadnt finished the CompareTo method...here is what I came up
with...
public int CompareTo(IEmployee other)

{

switch (m_Parent.Employees.SortBy)

{

case EmployeeSortType.Id:

return this.m_Id.CompareTo(other.Id);

case EmployeeSortType.FirstName:

return this.m_FirstName.CompareTo(other.FirstName);

case EmployeeSortType.LastName:

return this.m_LastName.CompareTo(other.LastName);

default:

return 0;

}

}

Everything seems to work as desired and I have all or the different sort
methods working.
 

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