c# - Singleton vs Static Class for exposing data read from xml - Stack Overflow

admin2022-06-14  66

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.

Answers:

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.

Answers:

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.

Answers:

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.

Answers:

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);

Answers:

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.

转载请注明原文地址:https://www.u19.cn/read-151645.html

New Post(0)