I wonder what would be correct way to inject services into singleton class (by using NInject framework, for example.
What singleton code is doing actually - it run windows form in application context
public class FrontController
{
private static volatile FrontController _instance;
private static readonly object syncRoot = new Object();
private ControlsContainer _controlsContainer;
private FrontController() { }
public static FrontController Instance
{
get
{
if (_instance == null)
{
lock (syncRoot)
{
if (_instance == null)
{
_instance = new FrontController();
}
}
}
return _instance;
}
}
public void StartApplication()
{
_controlsContainer = new ControlsContainer();
Application.Run(_controlsContainer);
}
public void EndApplication()
{
//throw new NotImplementedException();
}
internal void Synchronize()
{
ISymantecService<ClientModel> service =
new SymantecService<ClientModel>(new CustomerRepository<ClientModel>());
service.Synchronize();
}
}
We have
public void StartApplication()
{
_controlsContainer = new ControlsContainer();
Application.Run(_controlsContainer);
}
as well as
internal void Synchronize()
{
ISymantecService<ClientModel> service =
new SymantecService<ClientModel>(new CustomerRepository<ClientModel>());
quickBookservice.Synchronize();
}
Is there any way to inject ISymantecService and ICustomerRepository in this class in a thread safe manner.
Related
An application I work with has a static class that loads some configuration from XML files in its constructor.
But when we make changes to one of these XML's, this class is not reloaded (as it should be, since it is static).
What can I do for this static class to be instantiated again, reloading the configuration?
Would I need to restart the IIS server? Are there some other ways?
Probably better to use a Singleton pattern with locks and with data invalidation
(typed in here so sorry in any syntax is wrong)
public class MySingleton
{
private static MySingleton _instance;
private static object _lock = new object();
private static MySingleton
{
// initialize here
}
public static MySingleton Instance
{
get
{
var singl = _instance;
if (singl != null)
return singl;
lock(_lock)
{
if (singl != null)
return singl;
_instance = new MySingleton();
return _instance;
}
}
}
public static void Invalidate()
{
lock(_lock)
{
_instance = null;
}
}
// -- your non-static methods
public bool CheckSomething(){ return true; }
}
use
// thread one
if (MySingleton.Instance.CheckSomething())
// my code
// thread two
MySingleton.Invalidate();
currently I'm working on UWP project, using Prism. I'm facing some problem with DI & IUnityContainer. I want to call my service in IUnityContainer anywhere in my UILogic project without declaring it on ViewModel constructor. Should I use singleton class which holds the container and calls it, instead of declaring in the constructor?
Here's my singleton class
public class WebServiceHandler
{
private static WebServiceHandler _instance;
private static object syncRoot = new Object();
private IUnityContainer myContainer;
public IErrorMessageService ErrorMessageService;
public IEventAggregator EventAggregator;
public IRunInformationService RunInformationService;
public static WebServiceHandler Instance
{
get
{
if (_instance == null)
{
lock (syncRoot)
{
if (_instance == null)
{
_instance = new WebServiceHandler();
}
}
}
return _instance;
}
}
private WebServiceHandler()
{
try
{
myContainer = (UnityContainer)Application.Current.Resources["MyContainer"];
ErrorMessageService = myContainer.Resolve<IErrorMessageService>();
EventAggregator = myContainer.Resolve<IEventAggregator>();
RunInformationService = myContainer.Resolve<IRunInformationService>();
}
catch { }
}
}
Is using lock(this) instead of lock(lockObject) good implementation in a singleton lazy multi thread implementation?
example:
public class MedicineHelper
{
private static MedicineHelper medicineHelper;
private MedicineHelper()
{
}
public static MedicineHelper GetInstance()
{
if (medicineHelper == null)
{
lock (this)
{
if (medicineHelper == null) medicineHelper = new MedicineHelper();
}
}
return medicineHelper;
}
}
The classical pattern require a lock dedicated object like this:
public class MedicineHelper
{
private static MedicineHelper medicineHelper;
private static Object lockObject = new Object();
private MedicineHelper()
{
}
public static MedicineHelper GetInstance()
{
if (medicineHelper == null)
{
lock (LockObject)
{
if (medicineHelper == null) medicineHelper = new MedicineHelper();
}
}
return medicineHelper;
}
}
It is not a good implementation because you can't do it. "this" is not available from a static method.
I want to de-allocate the memory from the original singleton object and create a new one with another method.
public sealed class ObjectZ {
static readonly ObjectZ _instance = new ObjectZ();
private ObjectZ() {}
public static ObjectZ Instance{
get { return _instance; }
}
}
What would this method look like?
Singletons are usually created once and exist for the lifetime of the domain, recreating a singleton is dodgy business and by definition the code I've provided isn't truly a singleton.
The behaviour you seem to be after is a statically accessible single object cache that can be invalidated.
public static class SingletonAccessor
{
private static SomeClass _instance;
private static object _lock = new Object();
public static SomeClass Singleton
{
get
{
lock (_lock)
{
if (_instance == null)
{
_instance = new SomeClass();
}
return _instance;
}
}
}
public static void Recycle()
{
lock (_lock)
{
if (_instance != null)
{
// Do any cleanup, perhaps call .Dispose if it's needed
_instance = null;
}
}
}
}
I need to create a single class instance in web application that manage a queue of process. This class have multiple thread inside that look for queue and consume it.
What is the best why to do this?
I have apply singleton design pattern, but I don't understand if have to create static or normal queue inside it. Some one can give me an example please?
SOLUTION
Ok thank you! This is my singleton class:
public sealed class MyWorkingSingletonClass
{
private static readonly ILog LOG = LogManager.GetLogger(typeof(MyWorkingSingletonClass));
private static MyWorkingSingletonClass instanza;
private static readonly object lockObject = new object();
private static ConcurrentQueue<Process> syncCoda = new ConcurrentQueue<Process>();
private MyWorkingSingletonClass()
{
}
public static MyWorkingSingletonClass Instanza
{
get
{
lock (lockObject)
{
if (instanza == null)
instanza = new PdfDucumentConverter();
return instanza;
}
}
}
public void AddProcess(Process p)
{
syncCoda.Enqueue(p);
}
public void Start()
{
Task.Factory.StartNew(WorkerTask2);
}
public static void WorkerTask2()
{
do
{
try
{
Process p;
if (syncCoda.TryDequeue(out p))
{
p.Start();
p.PriorityClass = ProcessPriorityClass.High;
p.WaitForExit();
}
}
catch (Exception ex)
{
LOG.Error(ex);
}
} while (true);
}
}
What you need is to implement new singleton class which inherits Concurrent Queue class which is thread-safe queue to ensure it will work in multi-thread environment:
public class SingletonConcurrentQueue<T> : ConcurrentQueue<T>
{
private static readonly SingletonConcurrentQueue<T>
_instance = new SingletonConcurrentQueue<T>();
static SingletonConcurrentQueue(){}
private SingletonConcurrentQueue(){}
public static SingletonConcurrentQueue<T> Instance
{
get { return _instance; }
}
}