patterncsharpMinor
Passing parameter to singleton
Viewed 0 times
passingsingletonparameter
Problem
I wrote this factory class in order to pass parameter to Singlton class, is it a good design in term of design for multithreading environment?
public static class LoggingServiceFactory
{
private static string _connectionstring;
private static readonly Lazy _INSTANCE = new Lazy(() => new LoggingService(_connectionstring));
public static ILoggingService GetService(string connectionString)
{
_connectionstring = connectionString;
return _INSTANCE.Value;
}
private class LoggingService : ILoggingService
{
private string _connectionstring;
internal LoggingService(string connectionString)
{
_connectionstring = connectionString;
}
public void LogMessage(string msg)
{
// do the logging work
}
}
}
public interface ILoggingService
{
void LogMessage(string msg);
}Solution
I would not personally put LoggingService implementation inside factory, as it reduces testability and usually needed to play some tricks with generics type parameters only.
To have a predictable behavior according to your design it might look like:
Anyway, it makes sense to think about delegating it to IoC container...
I don't think that it is a responsibility of this class to verify if connection string is the same. This interface delivers different perception.
To have a predictable behavior according to your design it might look like:
public static class LoggingServiceFactory
{
static ConcurrentDictionary Services { get; } =
new ConcurrentDictionary();
public static ILoggingService GetService(string connectionString) =>
Services.GetOrAdd(connectionString, cs => new LoggingService(cs));
}Anyway, it makes sense to think about delegating it to IoC container...
I don't think that it is a responsibility of this class to verify if connection string is the same. This interface delivers different perception.
Code Snippets
public static class LoggingServiceFactory
{
static ConcurrentDictionary<string, ILoggingService> Services { get; } =
new ConcurrentDictionary<string, ILoggingService>();
public static ILoggingService GetService(string connectionString) =>
Services.GetOrAdd(connectionString, cs => new LoggingService(cs));
}Context
StackExchange Code Review Q#117959, answer score: 2
Revisions (0)
No revisions yet.