I want to be able to look values up by a key in same cases, and in other cases iterate through the collection.
Previously I was doing this:
public class Company
{
public string Name { get; set; }
public ICollection<Department> Departments { get; set; }
}
public class Department
{
public string Name { get; set; } //display name
public string UniqueName { get; set; } //like a code value
public bool IsSelected { get; set; }
}
foreach(var d in someCompany.Departments) ...
var departments = someCompany.Departments.Select( d => some projection stuff ...
Now I have several places I also need to iterate through some other collection like checkbox lists with matching strings for the UniqueName, so for it to be efficient I changed the declaration of Departments to an IDictionary, but this makes the other use cases more cumbersome, always requiring me to drill into a Value or Values. So now Departments property is no longer a collection of Department classes, but instead a collection of KeyValue pairs.
foreach(ListItem item in someCheckBoxes.Items)
{
someCompany.Departments[item.Value].Selected = true;
}
foreach(var d in someCompany.Departments.Values) ...
var departments = someCompany.Departments.Values.Select( d => some projection stuff ...
I also don’t care for having to convert Lists to IDictionary or add a KeyValue whenever I initialize these lists.
Ideally I would have a collection that behaves like an ICollection but also has an index operator and Contains a function which internally access a dictionary.
OR I would have two properties like so, that stay in sync:
public Company(string uniqueName, string name, ICollection<Department> departments)
{
Name = name;
UniqueName = uniqueName;
DepartmentsByUniqueName = departments.ToDictionary<Department, string>(p => p.UniqueName);
}
public IDictionary<string,Department> DepartmentsByUniqueName { get; set;}
public ICollection<Department> Departments { get { return DepartmentsByUniqueName.Values; } }
public void AddDepartment(Department department)
{
DepartmentsByUniqueName.Add(department,department.UniqueName)
}
The problem here is that someone could get the Values collection via the Departments property and add/remove items to it, not reallizing they really need to be added to the dictionary instead.(implementing set doesn’t solve this because they get the cllection via the get and then can add items to it.)
Trying not to reinvent too much code, but inheriting from Dictionary automatically makes it inherit IEnumerable. Even if I do an explicit implementation of the interface, problems could occur if the user has cast it to that base interface.
So essentially I want a class that implements IEnumerable but also has a Contains and [] operators which utilize a dictionary internally for efficiency.
OR be able to create addition DepartmentByUniqueName dictionary property which stays in sync with the Departments collection.
What you may want to do is to subclass KeyedCollection and define it thus:
And use it in your company class as the Departments property:
The KeyedCollection can then be used either by name or by index: