Unity allows to create a singleton with:
this.UnityContainer.RegisterType<MyClass>(new ContainerControlledLifetimeManager());
var instance = this.UnityContainer.Resolve<MyClass>();
The access to the MyClass's constructor must be public to allow the container to be able to create a new instance (for another project like a module, ...).
In this case, developers can still create a new instance by using the constructor directly:
var instance = new MyClass();
How do I prevent developers from doing this?
Another way is to create an instance (constructor access of the class is private now - singleton pattern) and register it with unity like below, I lose the dependency property injection, and my workaround is to use the service locator to initialize the dependency properties in the static method Instance:
this.UnityContainer.RegisterInstance<MyClass>(MyClass.Instance);
What is the best way to define the singleton pattern in both cases simultaneous (with and without unity)?