Personally, I'd stick with the Contains
approach, for two reasons:
- The Locator should be responsible for knowing which services have been registered - not the services (Single Responsibility Principle).
- The only way to make the service unable to set the property, is to use inheritance with
internal
properties which only the locator can set. Again, forcing the service to inherit your base class is very restrictive (since c# doesn't support multiple inheritance).
If you're worried about the Contains
performance, you could use a data structure with better performance than List<T>
. "Skip lists" are a kind of list with properties similar to Binary Search Trees. It allows you to search for a specific item in O(log n) time, whereas List<T>
takes O(n) time (i.e., much slower).
NGenerics provides implementations for Skip lists and several other useful data structures: https://github.com/ngenerics/ngenerics
Here's a cheat sheet for data structures performance: http://bigocheatsheet.com/#data-structures