I'm using StructureMap
at the moment, generally with convention-based (Scan()
) auto-configuration, and I'm looking to add decorator-based caching into the pipeline.
If I configure it manually that is fine, but Scan()
is just so convenient when you get lots of dependencies... I'm toying with noting cache suggestions on the interface(s), for example:
public interface IFoo {
[CacheDuration(20)] // cache for 20 minutes
string[] DoSomethingReusable();
SomeType DoSomethingNonReusable(int key); // not cached
}
with the idea being that by adding a custom "convention" to StructureMap
's scanning (pretty easy) it can spot that one-or-more methods are decorated for caching, and automatically inject a generated caching decorator into that type's pipeline (generating a cache key from the interface/method name and parameter values).
On the plus side it makes adding caching very painless - just decorate the interface a little; but is is a code smell? And/or am I duplicating something that is already solved?
Attributes are OK if you're aware of two major factors:
inherently decentralized structure, instead of in one place you have your metadata sprinkled on each and every component it touches
rigid compiler constant structure. If you suddenly decide that your cache duration should be 10, not 20, you have to recompile all your code. You could route attributes to things like config, but that's working around the attributes, and at this point you should really reconsider whether using them was the best idea in the first place.
As long as you're aware of these issues, and OK with them, than go ahead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With