I have done some terrible, terrible things over the past few months in my dealings with RavenDB. This one isn’t so bad.
I noticed early on that I was probably going to have to write a few Transformers. I’m lazy and didn’t want to have to actually do anything to maintain them or make sure they’re up-to-date if I change the underlying codebase, so I came up with a way to have it take care of itself for me.
This chunk of code is intended to be run just after you’ve initialized your static IDocumentStore, preferably in a web service or something that isn’t going to be starting and stopping a whole lot. The point is that this ought to happen infrequently, hopefully only once.
1 2 3 4 5 6 7 |
// this uses reflection to find all the transformers and register them on the database; saves us from maintaining a manual list Assembly.GetExecutingAssembly().GetTypes() .Where(t => t.BaseType.IsGenericType && (t.BaseType.GetGenericTypeDefinition() == typeof(AbstractTransformerCreationTask<>))) .Select(t => Activator.CreateInstance(t)) .ToList() .ForEach(t => (t as AbstractTransformerCreationTask).Execute(documentStore)); |
Pretty simple stuff. Get all the classes you’ve created which inherit from AbstractTransformerCreationTask, initialize one of each, convert all those instances to a list, and then dump each one into the IDocumentStore. Now the Transformers in the database are guaranteed to match their counterparts from your codebase.