0

i am trying to understand if i need to use .singleton() in my structure map code. there is already a question about this on stackoverflow (StructureMap singleton) but i want to be sure i'm definitely using the correct syntax. One of the answers implies that if i am returning a type i need to use .singleton() so that my code here:

            x.For<IApprovedProgrammesHelper>().Use<ApprovedProgrammesHelper>();
            x.For(typeof(ICache<>))
                .Use(typeof(CacheHelper<>))
                .Dependencies.Add(typeof(TimeSpan), Settings.Instance.HttpCacheExpiration);

should look like this:

            x.For<IApprovedProgrammesHelper>().singleton().Use<ApprovedProgrammesHelper>();
            x.For(typeof(ICache<>))
                .Singleton()
                .Use(typeof(CacheHelper<>))
                .Dependencies.Add(typeof(TimeSpan), Settings.Instance.HttpCacheExpiration);

is this correct? Or have i misunderstood how this should work? I'm not completely sure what the implications are for the code either way. It's probably worth mentioning that the types in the .Use<> statement do not follow the singleton pattern anyway.

Thanks for your time in advance Sam

Community
  • 1
  • 1
  • Only you know whether or not it is safe to have *only one instance* of `CacheHelper` and `ApprovedProgrammesHelper` or not. If it (or its dependencies) have any (mutable) state and aren't thread-safe it is probably unwise to define them as singleton. There is no way anyone can answer this question for you without seeing the code for both classes, their dependencies and knowing in which context (such as framework) this code runs. – Steven Jan 10 '17 at 17:50
  • @Steven you are correct, it has taken me a little while to understand that its only really possible to say if singleton is appropriate if you know the details of the classes/interfaces involved. Your explanation has helped to elucidate the issue for me - using singleton is not correct or incorrect, it depends completely on the context. Thanks for your help. – dotcentric-samb Jan 20 '17 at 09:31

1 Answers1

0

Your use of the Singleton API is correct, however without knowing what your code is doing it's hard to say whether you should be making it a singleton.

By adding the Singleton() method call you're telling StructureMap that you want it create one instance of ICache<>, with any other requests for the object resolving to the one instance.

For instance, marking the following class as a singleton will always resolve in the date and time being the same at its first invocation:

public class SingletonExample
{

    private readonly DateTime _time;

    public SingletonExample(){
        _time = DateTime.Now;
    }

    public DateTime GetDateTime()
    {
        return _time;
    }
}
Joseph Woodward
  • 9,191
  • 5
  • 44
  • 63
  • thanks for your answer, i think i will need to look in detail at the classes in question before i can make a decision about singleton usage. Your example has helped me to understand why it might be unwise to use singleton in some circumstances. – dotcentric-samb Jan 20 '17 at 09:25