I'm working on a small project and I've decided to move over from DI to singleton pattern. Although I know 2 ways to do singleton.
The first one is where every non-model class is a singleton. This means:
UserController
DatabaseHelper
ConfigurationModule
FriendComponent
Are all singletons, however:
User
UserFriendship
DatabaseConnection
DatabaseConfig
Aren't since they're all models.
However, the second way I know is:
I have one main singleton class (I'll use class Program as example). The class looking something like:
class Program
{
private static Program _instance;
private readonly DatabaseHelper _databaseHelper;
public Program()
{
_databaseHelper = new DatabaseHelper();
}
public static Program GetInstance()
{
if (_instance == null)
{
_instance = new Program();
}
return _instance;
}
}
I know this is mainly subjective but I was wondering which of these 2 (or if both not) is best to use.
new
– Ant P Jul 10 '18 at 07:43