0

I Want to convert from Autofac to Simple Injector... My problem is set Named instance and set WithParameter in Simple Injector.. this is the autofac code :

//cache managers
if (config.RedisCachingEnabled)
{
    builder.RegisterType<RedisConnectionWrapper>().As<IRedisConnectionWrapper>()
        .SingleInstance();

    builder.RegisterType<RedisCacheManager>().As<ICacheManager>()
        .Named<ICacheManager>("snt_cache_static")
        .InstancePerLifetimeScope();
}
else
{
    builder.RegisterType<MemoryCacheManager>().As<ICacheManager>()
        .Named<ICacheManager>("snt_cache_static")
        .SingleInstance();
}

builder.RegisterType<PerRequestCacheManager>().As<ICacheManager>()
    .Named<ICacheManager>("snt_cache_per_request")
    .InstancePerLifetimeScope();

builder.RegisterType<SettingService>().As<ISettingService>()
    .WithParameter(ResolvedParameter.ForNamed<ICacheManager>("snt_cache_static"))
    .InstancePerLifetimeScope();
Steven
  • 166,672
  • 24
  • 332
  • 435
  • What's the function of the `PerRequestCacheManager` registration with the `snt_cache_per_request` name. It doesn't seem relevant to the question. Is this keyed registration used somewhere else? – Steven Feb 28 '17 at 16:24
  • function of "PerRequestCacheManager": in class RedisCacheManager, "PerRequestCacheManager" is used to cache and retrieve a loaded object in memory for the current HTTP request... Is used only in "RedisCacheManager" class – Ssocial.it Web Agency Mar 01 '17 at 14:35
  • Where and how is this "snt_cache_per_request" registration used? Is it important for you or can it be removed? What other registratin is using it? – Steven Mar 01 '17 at 15:48
  • Then show where that keyed registration is used. – Steven Mar 01 '17 at 16:01
  • Yes,it is important...in RedisCacheManager class there is a global variable `private readonly ICacheManager _perRequestCacheManager` which is instantiated in constructor:`this._perRequestCacheManager = EngineContext.Current.ContainerManager.Resolve("snt_cache_per_request");`RedisCacheManager class contains public methods for set and get `_perRequestCacheManager`.The purpose is to use "PerRequestCacheManager" to cache a loaded object in memory for current HTTP request.This way don't connect to Redis server 500 times per HTTP request (e.g. each time to load a locale or setting)... – Ssocial.it Web Agency Mar 01 '17 at 16:07
  • Great. Would you mind updating your question and showing the relevant classes and their constructors, like the `RedisCacheMan‌​ager` and `PerRequestCacheManager`. Also, could you show an example of how you would construct this set of objects if you didn't have a DI container (but without changing the classes itself). In other words, how would you construct these objects by hand. – Steven Mar 01 '17 at 16:53

0 Answers0