Closed ThisNoName closed 5 years ago
Right. the .As<X>
auto-implementation only expects to pass a connection to the constructor.
I can think of 2 ways to approach it.
services.AddScoped(x => {
var myQuery = new SqlConnectionStringBuilder(conn).As<MyQuery>();
myQuery.cacheService = context.Get<ICacheService>();
return myQuery;
})
class MyQuery : IQuery {
constructor (ICacheService, IQuery inner) { /* inject both */ }
public foo TheQuery(params) {
return CacheService.get(x, () => inner.TheQuery(params));
}
}
Thanks. I'll take injection. Layered approach is apparently more elegant, but this whole thing is out of pure laziness pampered by auto interface ^_^
hahaha :)
I would look for a caching layer that does proxying. if there isn't one, someone should write it.
I would love to do:
interface CachedQueries : IQuery {
// leave most definitions alone
[CachePolicy (blah)]
// repeat method signature
}
or even something that would cache with an external policy:
var iquery = conn.As<IQuery>();
var cached = CachePolicy.Wrap(iquery)
Thanks. I usually handle memory cache and db cache (for remote db) similarly, using attribute or injecting caching service via constructor. This particular one is only reserved for a handful of frequently used and always cached calls.
Some common queries are always cached, so having something like this could save quite a bit of hassle else where.
I couldn't figure out if this is possible since auto interface IoC seems expecting default constructor with no parameters.
Any suggestions? Thanks.