0

I don't know what should I use. I have 3 classes. PasswordService, SettingsService, FileService. These classes each contain about 2 methods. The methods are being used in more assemblies. Now I'm using it as a singleton. But I'm not sure if I should. I think a static class would be enough. What do you think?

CODE:

public class PasswordService
{

    private PasswordService(){}

    private static PasswordService _instance;

    public static PasswordService Instance
    {
        get { return _instance ?? (_instance = new PasswordService()); }
    }

    public byte[] EncryptPassword(string password)
    {

        var protectedPass = Encoding.UTF8.GetBytes(password);
        return ProtectedData.Protect(protectedPass, null);
    }

    public string DecryptPassword(byte[] encryptedPassword)
    {
        var unprotectedPass = ProtectedData.Unprotect(encryptedPassword, null);
        return Encoding.UTF8.GetString(unprotectedPass, 0, unprotectedPass.Length);
    }
}
JuP
  • 535
  • 1
  • 6
  • 23
  • 1
    The answer to that question is always the same: It depends. – Nuffin Mar 18 '13 at 11:15
  • possible duplicate of [Should I use a singleton?](http://stackoverflow.com/questions/1676296/should-i-use-a-singleton) – H H Mar 18 '13 at 11:16
  • 1
    I would say that a singleton is almost always evil. Use an instance of each of these services. You will end up with a more flexible design where you can see your real dependences. – Ignacio Soler Garcia Mar 18 '13 at 11:16
  • @SoMoS: You're essentially saying "Singletons are evil, so use them". Or did you want to say "Create a new instance everywhere you want to use those services"? – Nuffin Mar 18 '13 at 11:19
  • @Nuffin: sorry, of course I meant "create a new instance everywhere you need one". If several objects have to share the same instance pass it as a parameter on the constructor or in the way that best fits the design. – Ignacio Soler Garcia Mar 18 '13 at 11:21

3 Answers3

2

You don't have any state in your class, so I do not see any reason to use instance of class. I recommend you to use static classes.

Mikhail Gubanov
  • 420
  • 6
  • 13
1

1 - It is right to create a singleton for these services since these are seems to be handle one specific task.

2 - Avoid Static as much as possible as you can not mock it efficiently if you use TDD and do automatic unit testing with contineous Integration server.

TalentTuner
  • 17,262
  • 5
  • 38
  • 63
1

I recommend you to keep the Singleton, unless you're using instances or DI. A Singleton can be refactored into instances quite easily, whereas static classes must be re-implemented to being non-static. Moreover, you can replace instances with test dummies, whereas replacing a static implementation is hardly possible.

For instance you might come across a situation where your programm must handle multiple FileConfiguration instances, for two distinct config files. A Singleton can be split into a two-instance pool.

I came across this issue with a DAO class which used to be static and capable of connecting to one DB. We had to refactor it, since a new requirement included supporting n>1 databases in one program instance.

As Mikhail points out, use static only for truly stateless stuff. A config file or a chosen password hashing algorithm in a static field is already a state, as well as the connection string in my example above is - even though they might never change at runtime.

Matthias Meid
  • 12,455
  • 7
  • 45
  • 79
  • But I don't think, that I keep any state. The methods only return the result. The classes haven't any fields. – JuP Mar 18 '13 at 11:27
  • 1
    @JurajP As for the `PasswordService` you're right, there's technically no state. However, for instance `EncryptPassword` and `DecryptPassword` are related to each other, as they share the use of `Encoding.UTF8`. Here and in other services of yours this might become a state like `readonly Encoding enc = Encoding.UTF8` for easier and less error-prone refactoring. I admit this sounds pretty far-fetched, but I regretted some static methods I made in the past that weren't *stateless and technically unrelated to each other*. Hope I expressed my point well enough... :) – Matthias Meid Mar 18 '13 at 11:37