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.
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 :
instead of: