Caching Application Block :: Strongly Typed cache objects

Topics: Caching Application Block
Jul 16, 2008 at 7:22 PM

 Hi all,

Looking at the application block it appears that items in the cache are always returned as Object when retrieved.  This then requires a cast to the required type. ie. Product myproduct = (Product)cache.GetData(productId); 

Is there a CacheManager that supports strongly typed objects to remove the need for the cast?  Does the block not support generics to allow the cacheManger to be initialised with a type for the objects?

Any help appreciated.

Zube.

Jul 16, 2008 at 7:54 PM
Hi Zube,

Cache managers are not strongly typed, and making them so would require changing not only the runtime objects but also the factories and probably configuration.

If what concerns you the most is the repeated casting code, and you're using .NET 3.5, you could add an extension method on the ICacheManager interface to do the casting for you (but you would still need to specify the type in some cases).

Hope this helps,
Fernando


zube wrote:

 Hi all,

Looking at the application block it appears that items in the cache are always returned as Object when retrieved.  This then requires a cast to the required type. ie. Product myproduct = (Product)cache.GetData(productId); 

Is there a CacheManager that supports strongly typed objects to remove the need for the cast?  Does the block not support generics to allow the cacheManger to be initialised with a type for the objects?

Any help appreciated.

Zube.




Jul 16, 2008 at 8:01 PM
Thanks Fernando,

I guess the casting just worries me as it seems less clean than using Generics to specify the types. 

You mention the level of change required to the Ent Lib to implement, and it seems a bit strange that MS have not implemented this more cleanly.  Have you used this block? If so, does it represent an efficient cast given the amount of casting that could be involved?

Thanks for your time.
Zube
Jul 16, 2008 at 8:31 PM

Hi Zube,

I agree that casting involves some overhead but that is an issue only if you expect the elements in your cache to be homogeneous, and I don't think homogeneous caches are the primary target for the block.

It could be argued that the caching block was designed before generics and it wasn't updated since then, but other caches like ASP.NET's and the bleeding-edge Velocity also follow the approach and don't have a generics-based API.

This doesn't mean that a cache that supports generics isn't valuable; I suggest you log a feature request for this.

Regards,
Fernando


zube wrote:
Thanks Fernando,

I guess the casting just worries me as it seems less clean than using Generics to specify the types. 

You mention the level of change required to the Ent Lib to implement, and it seems a bit strange that MS have not implemented this more cleanly.  Have you used this block? If so, does it represent an efficient cast given the amount of casting that could be involved?

Thanks for your time.
Zube