When using Redis, we create a ConnectionMultiplexer and from that GetDatabase() to get access to a given cache.
I want to configure Unity to do this as follows:
ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("localhost");
container.RegisterInstance(redis);
container.RegisterType<IDatabase, redis.GetDatabase()>();
But not surprisingly that wont compile.
The only other way I can think of at the moment is to create a database factory class and pass that into my controllers instead of an IDatabase but that seems like overkill as I would just essentially be wrapping a wrapper:
public static void RegisterComponents() {
ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("localhost");
container.RegisterInstance(redis);
RedisDatabaseFactory redisDbf = new RedisDatabaseFactory(redis);
container.RegisterInstance(redisDbf);
container.RegisterType<IRedisDatabaseFactory, RedisDatabaseFactory>();
}
MyController.cs
public MyController(IRedisDatabaseFactory rdbf){
_db = rdbf.GetDatabase();
}
So short of using this overly verbose factory method, how can I use the methods of registered instances to create injectable dependencies for other objects?