SteeltoeOSS / Connectors

Connectors simplify connecting to standalone and CloudFoundry services
https://steeltoe.io/docs/steeltoe-connectors/
Apache License 2.0
27 stars 13 forks source link

Make RedisServiceConnectorFactory retrieval reusable #9

Closed tillig closed 7 years ago

tillig commented 7 years ago

In RedisCacheServiceCollectionExtensions all of the extension methods repeat this same little block of code to get a populated RedisServiceConnectorFactory:

RedisCacheConnectorOptions redisConfig = new RedisCacheConnectorOptions(config);
RedisServiceInfo info = config.GetSingletonServiceInfo<RedisServiceInfo>();
RedisServiceConnectorFactory factory = new RedisServiceConnectorFactory(info, redisConfig);

In a recent project I was trying to use Redis as a backing store for configuration and wanted to use the nicely built connection parsing to set that up. Unfortunately, all of that happens before DI is started, so just adding an IDistributedCache or whatever to the service collection doesn't help.

Could there be a new reusable extension like this added? (I can PR it if so...)

public static RedisServiceConnectorFactory CreateRedisServiceConnectorFactory(this IConfiguration config)
{
  if (config == null)
  {
    throw new ArgumentNullException(nameof(config));
  }

  RedisCacheConnectorOptions redisConfig = new RedisCacheConnectorOptions(config);
  RedisServiceInfo info = config.GetSingletonServiceInfo<RedisServiceInfo>();
  return new RedisServiceConnectorFactory(info, redisConfig);
}
dtillman commented 7 years ago

@tillig

A PR or other contributions are always welcome!

Dave

dtillman commented 7 years ago

Thanks @tillig, I'll start looking at and merging your contributions into the 2.0 development line that I'm about to start on... ... I want to refactor several of the Steeltoe frameworks in that time frame... and many of your contributions (i.e in other repos as well) are spot on....

Dave