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 { }
}
}
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();
Can we use the volatile keyword, because as I understand it provides instance freshness
public sealed class Singleton
{
private static volatile Singleton instance = null;
private static readonly object padlock = new object();
Singleton()
{
}
public static Singleton Instance
{
get
{
if (instance == null)
{
instance = new Singleton();
}
return instance;
}
}
}
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.
I have a class which implements the Singleton design pattern. However, whenever i try to get an instance of that class, using Activator.CreateInstance(MySingletonType) only the private constructor is called. Is there any way to invoke other method than the private constructor?
My class is defined as follow:
public class MySingletonClass{
private static volatile MySingletonClassinstance;
private static object syncRoot = new object();
private MySingletonClass()
{
//activator.createInstance() comes here each intantiation.
}
public static MySingletonClassInstance
{
get
{
if (instance == null)
{
lock (syncRoot)
{
if (instance == null)
instance = new MySingletonClass();
}
}
return instance;
}
}
}
And the instantiation as follow:
Type assemblyType = Type.GetType(realType + ", " + assemblyName);
IService service = Activator.CreateInstance(assemblyType, true) as IService;
Activator.CreateInstance, except for one extreme edge-case, always creates a new instance. I suggest that you probably dont want to use Activator here.
However, if you have no choice, the hacky hack hack hack is to make a class that inherits from ContextBoundObject, and decorate it with a custom subclass of ProxyAttribute. In the custom ProxyAttribute subclass, override CreateInstance to do whatever you want. This is all kinds of evil. But it even works with new Foo().
Hei i do not know why are you creating an object of singleton class using reflection.
the basic purpose of singleton class is that it has only one object and has global access.
however you can access any of your method in singleton class like :
public class MySingletonClass {
private static volatile MySingletonClass instance;
private static object syncRoot = new object();
private MySingletonClass() { }
public static MySingletonClass MySingletonClassInstance {
get {
if (instance == null) {
lock (syncRoot) {
if (instance == null)
instance = new MySingletonClass();
}
}
return instance;
}
}
public void CallMySingleTonClassMethod() { }
}
public class program {
static void Main() {
//calling a
methodMySingletonClass.MySingletonClassInstance
.CallMySingleTonClassMethod();
}
}
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;
}
}
}
}