lundi 6 avril 2015

Looking for a generic search for a composite object

Consider the below



private static EmployeeDepartment GetEmpDeptCollection()
{
var departmentRecords = LoadDepartments();

//Search Employees by Id
var filteredEmployeeRecords = SearchEmployeesByDeptId(1);

//Search Departmnent by DepartmentID
var filteredDepartmentRecords = (
from empRecs in filteredEmployeeRecords
join deptRecs in departmentRecords
on empRecs.DepartmentId equals deptRecs.DepartmentId
select deptRecs
).ToList();

return new EmployeeDepartment()
{
EmployeeSet = filteredEmployeeRecords
, DepartmentSet = filteredDepartmentRecords
};
}


I am basically performing an EmployeeSearch first and then doing a Department Search based on the matching DepartmentID's. Finally bundling them in the EmployeeDepartment


The program works fine.


Now consider, I have Product class which is a composition of Finance, Inventory,Marketing


I may have to do a search in the Finance first, then on Inventory followed by Marketing (consider I have matching ID's). Or it may be some other way... I can do that by the approach I am currently proceeding with.


But just out of curiosity, any other elegant approach is there - say Visitor Pattern or something of that sort that may help?


A generic approach that will do it.. may be recursively... something interesting that you people have come across and have already done (:


The entire current implementation:



class Program
{
static void Main(string[] args)
{
var EmpDeptRecords = GetEmpDeptCollection();

// Wait for user
Console.ReadKey();
}

private static EmployeeDepartment GetEmpDeptCollection()
{
var departmentRecords = LoadDepartments();

//Search Employees by Id
var filteredEmployeeRecords = SearchEmployeesByDeptId(1);

//Search Departmnent by DepartmentID
var filteredDepartmentRecords = (
from empRecs in filteredEmployeeRecords
join deptRecs in departmentRecords
on empRecs.DepartmentId equals deptRecs.DepartmentId
select deptRecs
).ToList();

return new EmployeeDepartment()
{
EmployeeSet = filteredEmployeeRecords
, DepartmentSet = filteredDepartmentRecords
};
}

private static List<Employees> SearchEmployeesByDeptId(int deptID)
{
return LoadEmployees().FindAll(i => i.DepartmentId == deptID);
}

private static List<Employees> LoadEmployees()
{
return new List<Employees>()
{
new Employees { EmployeeId = 1, EmployeeName = "John", DepartmentId = 1 } ,
new Employees { EmployeeId = 2, EmployeeName = "Abraham", DepartmentId = 2 },
new Employees { EmployeeId = 3, EmployeeName = "Adam", DepartmentId = 1 },
new Employees { EmployeeId = 4, EmployeeName = "Marshal", DepartmentId = 3 },
new Employees { EmployeeId = 5, EmployeeName = "Girijan", DepartmentId = 4 },
new Employees { EmployeeId = 6, EmployeeName = "Clary", DepartmentId = 4 }
};
}

private static List<Department> LoadDepartments()
{
return new List<Department>()
{
new Department { DepartmentId = 1, DepartmentName = "Utility Plant" } ,
new Department { DepartmentId = 2, DepartmentName = "Scandinavian Club" },
new Department { DepartmentId = 3, DepartmentName = "Video Game Society" },
new Department { DepartmentId = 4, DepartmentName = "Sympoh"}
};
}
}



class Employees
{
public int EmployeeId { get; set; }
public string EmployeeName { get; set; }
public int DepartmentId { get; set; }
}

class Department
{
public int DepartmentId { get; set; }
public string DepartmentName { get; set; }
}

class EmployeeDepartment
{
public List<Employees> EmployeeSet { get; set; }
public List<Department> DepartmentSet { get; set; }
}

Aucun commentaire:

Enregistrer un commentaire