What is the difference between static class and singleton in .net mvc project?
Solution 1:
Adding a singleton via dependency injection instantiates an instance of the requested class. A static class cannot be instantiated, and so you simply access its methods from elsewhere, based on the static class's access modifiers.
Solution 2:
I would say that in context of your application the difference boils down to using DI or not and who is controlling lifetime/instantiation/injection.
Moving some functionality into some static helper class can be pretty fine if it should not differ based on environment or some other source of variability. For example ConvertMeterToMiles
seems to be a fine candidate for such handling.
SendEmail
on the other hand does not seems to be one - there can be environments where you don't want to send emails (test for example), or in future you anticipate having multiple implementations (or need to reimplement it) for this functionality (for example for some contexts email sending can be deferred using queue for example which will be handled by some background worker or another service). In this case you can highly leverage existence of DI and having encapsulated this functionality and hidden it behind contract (also I would say that handling SMTPClient
settings is cleaner when they are registered in DI and resolved for encapsulated implementation).