I am experimenting with loading views from a database, and as suggested in the article one might want to add some caching to prevent hitting the database every time.
ConfigureServices
:
services.AddHttpContextAccessor();
services.AddMemoryCache();
services.AddRazorPages()
.AddRazorRuntimeCompilation(opt =>
{
opt.FileProviders.Add(new DatabaseFileProvider(Configuration["AppSettings:SQLConnectionString"]));
});
DatabaseFileProvider
constructor:
private string _connection;
public DatabaseFileProvider(string connection)
{
_connection = connection;
}
How do I dependency inject an instance of IMemoryCache
to the DatabaseFileProvider
class?, as one can do with e.g. a singleton:
ConfigureServices
:
services.AddSingleton<AppUtils>();
AppUtils
constructor:
private static IMemoryCache _cache;
public AppUtils(IMemoryCache cache)
{
_cache = cache;
}
Use DI services to configure
MvcRazorRuntimeCompilationOptions
directlyAssuming a target provider like
Creating the provider with the aid of the DI services will allow for any registered dependencies to be resolved and explicitly injected using the deferred configuration delegate.
Reference Use DI services to configure options
Configure
allows the use of up to five services to configure options, but if aIServiceProvider
is injected, the provider can be used in resolve more dependencies if needed.If that service locator approach is not preferred, the setup can be rearranged to follow a more pure DI design.