We've added the method 'IsCachable
The complete signature for the method is shown below:
bool IsCacheable<T>(T value, ref IEnumerable<Type> failingTypes) where T : class;
The suitability for caching will be returned as the boolean result and any failing types are returned via the ref parameter 'faillingTypes'. Obviously the internal implementation is dependent on the cache provider being used. The method will always return true for the In Memory & Null Cache Providers because neither of these implementations have to manipulate the data being stored in the cache provider.
The implementation was initially created for was the Isolated Storage Cache Provider. This uses the SilverlightSerializer internally and historically this gave rather confusing exception messages, but since Mike has released v2 these have greatly improved (see my previous post). We still went ahead with adding this functionality to aid testing and we completed it before Mike released the new version :)
Basically the implementation re-curses the object graph extracting the object properties which are reference types and marked for serialisation (i.e. they don't have the 'DoNotSerialize' attribute). Common types which are known to be serializable are ignored - string, List
So lets see how this works with a simple succeeding unit test:
[TestMethod]
public void ShouldBeAbleToCacheComplexModel()
{
// Given we have an isolated storage cache provider...
var cacheProvider = new IsolatedStorageCacheProvider("IsolatedStorageCacheProviderTests", Enumerable.Empty<Assembly>());
// Given we have a model we want to check for cacheability...
var model = new SuccessfulComplexModel
{
FirstName = "ollie",
LastName = "riches",
Location = new GeoCoordinate(51.554111, -0.072784)
};
model.Area = new LocationRect(model.Location, 10, 10);
model.Stuff.Add("Stuff1");
model.Stuff.Add("Stuff2");
model.Stuff.Add("Stuff3");
// When we test for cacheability...
IEnumerable<Type> failingTypes = null;
var cacheable = cacheProvider.IsCacheable(model, ref failingTypes);
//Then we expect it to be cacheable...
Assert.IsTrue(cacheable);
Assert.IsFalse(failingTypes.Any());
}
The type being checked here is a relatively simple complex type! See the class diagram below, it contains 2 String properties, 2 generic collections as well as a GeoCoordinate & LocationRect property.
When we run the tests in debug when get the following returned in the output window in visual studio. The highlighted area shows the types that have been checked for compatibility with the SilverlightSerializer and since the <ROOT>
.
A failing test produces the following output in visual studio, what you can see is two MissingMethodException messages being generated. These are coming from SilverlightSerializer, the actual (exception) message is not being displayed here but it will have detailed information about the failing type (since v2 of SilverlightSerializer).
What you can see below the exception messages is the types we've attempted to serialize and you can see the <ROOT>
That pretty much covers it, hope this helps anyone using the Isolated Cache Provider and writing tests for entities you wish to cache.
Comments
Post a Comment