Geeks With Blogs

News This is the *old* blog. The new one is at
Elton Stoneman
This is the *old* blog. The new one is at

In the REST world, ETags are your gateway to performance boosts by letting clients cache responses. In the non-REST world, you may also want to add an ETag to an entity definition inside a traditional service contract – think of a scenario where a consumer persists its own representation of your entity, and wants to keep it in sync. Rather than load every entity by ID and check for changes, the consumer can send in a set of linked IDs and ETags, and you can return only the entities where the current ETag is different from the consumer’s version. 

If your entity is a projection from various sources, you may not have a persistent ETag, so you need an efficient way to generate an ETag which is deterministic, so an entity with the same state always generates the same ETag.

I have an implementation for a generic ETag generator on GitHub here: EntityTagger code sample. The essence is simple - we get the entity, serialize it and build a hash from the serialized value. Any changes to either the state or the structure of the entity will result in a different hash. To use it, just call SetETag, passing your populated object and a Func<> which acts as an accessor to the ETag property:

EntityTagger.SetETag(user, x => x.ETag);

The implementation is all in at 80 lines of code, which is all pretty straightforward:

var eTagProperty = AsPropertyInfo(eTagPropertyAccessor);	
var originalETag = eTagProperty.GetValue(entity, null);
	ResetETag(entity, eTagPropertyAccessor);	
	string json;
	var serializer = new DataContractJsonSerializer(entity.GetType());
	using (var stream = new MemoryStream())
		serializer.WriteObject(stream, entity);
		json = Encoding.UTF8.GetString(stream.GetBuffer(), 0, (int)stream.Length);
	var guid = GetDeterministicGuid(json);
	eTagProperty.SetValue(entity, guid.ToString(), null);

There are a couple of helper methods to check if the object has changed since the ETag value was last set, and to reset the ETag.

This implementation uses JSON to do the serializing rather than XML. Benefit - should be marginally more efficient as your hashing a much smaller serialized string; downside, JSON doesn't include namespaces or class names at the root level, so if you have two classes with the exact same structure but different names, then instances which have the same content will have the same ETag. You may want that behaviour, but change to use the XML DataContractSerializer if you think that will be an issue.

If you can persist the ETag somewhere, it will save you server processing to load up the entity, but that will only apply to scenarios where you can reliably invalidate your ETag (e.g. if you control all the entry points where entity contents can be updated, then you can calculate and persist the new ETag with each update).

Posted on Wednesday, November 21, 2012 10:54 PM Caching , github | Back to top

Comments on this post: A Simple Entity Tagger

# re: A Simple Entity Tagger
Requesting Gravatar...
I will test this over the next days and see how it works. Thanks and merry christmas.
Left by Jana on Dec 24, 2012 9:03 PM

# re: A Simple Entity Tagger
Requesting Gravatar...
Good post. Most of the niche marketing gurus often outsource their work to a team of artists, writers, designers and so forth..
Left by Orange County attorneys bankrupt on Jan 05, 2013 3:09 PM

# re: A Simple Entity Tagger
Requesting Gravatar...
Thanks for the Tutorial.Helpful.
Left by Mihai Alexandru on Mar 06, 2013 2:47 AM

# re: A Simple Entity Tagger
Requesting Gravatar...
Thanks, this helped to get a deeper understanding about the Entities.
Left by Sandra on Mar 30, 2013 12:54 PM

Your comment:
 (will show your gravatar)

Copyright © Elton Stoneman | Powered by: | Join free