Ultimately, this should come down to the time-space trade off. Your suggestion of implementing an equality comparer and iteration through the entire repository maximizes runtime but uses little space by using a List<T>
as the data structure used by the repository. In the worst case, where you update the last element of the list, you will need to iterate through the entire thing and run the equality operation on each element until it matches the last one. This is feasible for smaller repositories.
Another very common solution would be to override the GetHashCode
of your T
types in the repository, and using a HashSet<T>
or Dictionary<T, V>
as the data structure in the repository. The latter would minimize time to O(1) but take more space for the data structure. This is probably a better solution for much larger repositories, especially so if each of the type T
objects has one property, like a GUID
or database identifier associated with it that is unique because then you have a very easy hash value.
There are other data structures you can consider for your repository based on the exact use-case of your repository. For example, if you are trying to maintain an ordering of elements in the repository where only the highest or lowest element is fetched at a time, a PriorityQueue or Heap might be for you. If you spend time thinking about the data structure that backs your repository, the rest of the implementation should solve itself.