views:

1005

answers:

4

Hi,

We have a PageRoles xml file which contains the page path and the user role that can access that page.

We are maintaining a Dictionary in a static class, which gets loaded int static constructor for the class. The class has a method CheckIfRoleAllowed that takes in a page path and returns a bool.

Each page call the CheckIfRoleAllowed on Page Init.

static class PageAccessChecker
{
 static Dictionary<string, UserRoleType[]> _PageAccessPermissions;
 static FileSystemWatcher _XmlWatcher;

 static PageAccessChecker()
 {
   // Load page access permissions from xml
   // Set FileSystemWatcher watcher to watch for changes
 }

 public static CheckIfRoleAllowed(string pagePath)
 {
 }

}

Would we be better off doing this using the singleton pattern? If yes, why?

Kind regards.

+2  A: 

You do use a singleton. Simply, there are 2 usual implementations for singletons, the other being instantiating the class and having a static member referencing this instance.

Your implementation makes calls simpler IMO :

PageAccessChecker.CheckIfRoleAllowed(path);

instead of:

PageAccessChecker._default.CheckIfRoleAllowed(path);
Serge - appTranslator
+3  A: 

I can see two advantages of using the singleton pattern (if implemented through, say, a static property):

  1. you can delay loading the XML file until the first page is accessed.
  2. you can check whether the XML file has changed on disk, and automatically reload it on the next access.

A disadvantage might be that you need to make access thread-safe using a lock.

Martin v. Löwis
Actually, the file is loaded by calling a load method from the constructor.When the file changes, the Changed method of the FileSystemWatcher also calls the same method.
SharePoint Newbie
Hooray for FileSystemWatcher - I was about to suggest otherwise
annakata
+2  A: 

Actually you really don't want either a singleton or a static class.

First of all, a static class is a singleton. I guess what you are really asking is "Is it a benefit to add the rigmarole to insure that it's threat safe and there exist only one, or in other words, do I need a 'special' singleton?" To which the answer is "No", since you don't want a singleton.

A singleton is intended for objects which there can be only one, not for objects where only one is needed. This is not the case here. There is nothing about this situation which calls for a singleton. What you actually want is a thing called a "global variable".

"But, Wait!!!" you say. "Aren't global variables evil?" Well, yes, there are. But that's irrelevant here. Whether you call it a static class or a singleton or whatever, what you actually have here is a global variable. Calling it something else isn't going to change anything.

James Curran
It's not really that simple: that GlobalVar is going to be a class type, so you *are* looking at a singleton in implementation, and imho far better to address that with an interface bridge than with an actual global reference
annakata
@annakata: what's the difference? You're getting a global reference either way.
orip
@Annakata: I never said that. It should be an instance object of class. As I said, just because he needs only one doesn't mean that the implementation can't allow more than one.
James Curran
With a singlton you are able to delay initialization until it is needed. You can't do that with a global variable - unless you test it every time and initialize it before access. Then you factor that test into a static method and - hey presto - a singleton!
DJClayworth
A: 

If you keep the class constructor private, there's no real difference - they are both global variables that can be lazily initialized.

If you keep the class constructor public or protected and only use the pattern to create a global (not to enforce a single instance), you can at least test your singleton class.

But what you should really try is to avoid singletons and use dependency injection instead. See Singletons are Pathological Liars by Miško Hevery.

orip
downvoted, but it's still true :-)
orip