ISingletonDependency and Caching

Topics: Writing modules
Jul 13, 2012 at 3:20 PM

Just got a quick question:

Since ISingletonDependency is one instance per shell/tenant, once I got it created, I should be able to get hold of it whereever I want by injection, so if I have some large objects kept there, then what's the points to use caching?

(Caching is more friendly to memory footprint because it can Gc cold objects; while I believe ISingleton will always be alive?)



Jul 13, 2012 at 3:25 PM

Why would you hijack an object that has a completely different purpose, to do something for which there is a perfectly good solution already, that is designed to solve your problem?

Jul 13, 2012 at 3:32 PM

No no I was not trying to hijack ISingle, I'm writing a module which needs to load many XML files, I decided to do the loading and parsing in an ISingle thus it will only be called once, I was suppose to use caching to hold the parsed XElement for downstream use, but then I realized that I already can reach it just via ISingle from everywhere, thus my confusion/question here.

Jul 13, 2012 at 3:45 PM
Edited Jul 13, 2012 at 3:46 PM

Yeah, well, every time you feel like you can use ISingleton, you should pause and ask yourself if you should. Most of the time, you should not. So by default, don't. Singletons are evil and they will make you life miserable (but not right now, only when you suspect it the least and things have been looking like they were working fine for a few months). From what you describe, it's a mistake. There doesn't seem to be a good case for a singleton there. For caching, certainly, but a singleton, doesn't look like it.

Jul 13, 2012 at 4:09 PM

So some details on the evil side of ISingleton? I seems can't find it from the documentation.

What about I use IOrchardShellEvents.Activated() for my module XML loading? Let me try again:

public class myShellEvents : IOrchardShellEvents


  private readonly ICacheManager _cacheManager;

  public myShellEvents(ICacheManager cacheManager)

  { _cacheManager = cacheManager; }

  void IOrchardShellEvents.Activated()


     myXmlLoader my = new myXmlLoader(_cachaManager);



Is this ok?


Jul 13, 2012 at 4:15 PM

Singletons are needed in some key infrastructure pieces but that's about it.

I would also not eagerly load those documents on shell activation, but rather do it lazily the first time they are needed. Then again, I know near to nothing about what you are trying to accomplish here so I may be wrong.

Jul 13, 2012 at 4:21 PM

To lazy load is that because the delay to overall first page showing time? Can you suggest some places I should do this? ( I know I can do that in the controller, then the first user will suffer a bit ). Or maybe use background loading?

Thanks for all the help!



Jul 13, 2012 at 4:24 PM

Surely you have a service class that is responsible for handling the data from that XML file? That would be a good place to lazily load the data.

I would only use background loading if you determine that the first load really is a problem. In any case, your first solution was going to make the first user of the site suffer, instead of making the first user of the feature suffer.