I try to register a communication service with a custom parameter "server". But i want automatic resolve the ILogger dependency and not set this dependency twice.
Whats the best way to do this?
SimpleInjector Register
var diContainer = new Container();
diContainer.Register<ILogger, DefaultLogger>();
//Good
diContainer.Register<ICommunicationService>(
() => new CommunicationService("server1"),
Lifestyle.Singleton);
//Bad
diContainer.Register<ICommunicationService>(
() => new CommunicationService(new DefaultLogger(), "server1"),
Lifestyle.Singleton);
CommunicationService Class
public class CommunicationService : ICommunicationService
{
public CommunicationService(ILogger logger, string server)
{
}
}
Update 2017-11-27
@Steven I want run two instances of CommunicationService with different configuration. In my example server1 and server2.
var container = new Container();
container.Register<ILogger, DefaultLogger>();
container.RegisterSingleton(new CommunicationServiceConfig { Server = "server1" });
container.RegisterSingleton(new CommunicationServiceConfig { Server = "server2" });
diContainer.Register<ICommunicationService, CommunicationService>(Lifestyle.Singleton);
diContainer.Register<ICommunicationService, CommunicationService>(Lifestyle.Singleton);