views:

49

answers:

4

Hey all,

Let me start with the definitions of the objects with which I am working and then I'll do my best to explain what I'm after:

class CacheItem<T> where T : EntityBase

class CacheCollection : List<CacheItem<EntityBase>>

OK, so my generic collection, CacheCollection should be a list of CacheItems which I can manipulate according to various business rules. One thing I'd like to ensure, howerver, is that each CacheCollection I instanciate only works for a single type of EntityBase. For instance, I have two classes inheriting from EntityBase; Case and Client. I'd like each CacheCollection to handle only one of those types and not mix them.

What changes can/should I make to accomodate this design requirement?

Thanks in advance, Sonny

+3  A: 

You could always make CacheCollection a generic type itself and then pass that up the inheritance tree to CacheItem:

class CacheCollection<T> : List<CacheItem<T>> where T : EntityBase
Justin Niessner
A: 

Make CacheCollection generic, so you explicitly state the type that it can contain:

class CacheCollection<T> : List<CacheItem<T>> where T : EntityBase {}
driis
+1  A: 

It sounds like you want to get rid of CacheCollection (which serves no purpose) and have e.g.

List<CacheCollection<Case>> cases;
List<CacheCollection<Client>> clients;

no?

Brian
Beaten to the punch.
Kilanash
CacheCollection has a number of method within it that deal with how many items are held in cache, which items get "bumped" when the number is exceed, etc. Would there be a better place to put this type of logic?
Sonny Boy
In that case, I like @Justin Niessner's answer.
Brian
A: 

What's wrong with

class CacheCollection<T> : List<CacheItem<T>> where T:EntityBase
MartinStettner