ARTICLE AD BOX
I'm developing a winforms application with an underlying database. My system has a BusinessFunction which describes the business functions a user with a given role is able to perform, and the associated forms and database tables to perform that function.
A user role has a collection of BusinessFunction and a set of permissions for each (read, write, delete)
e.g. I have a BusinessFunction called "Manage book rentals" which is associated with the Librarian role, the LibraryForm form and the Books, Rentals, and Customers tables. The Librarian role has Read and Write access for the specified tables.
Form and db access are handled in different places, but all share the same BusinessFunction values, which means I have a lot of string literals which are directly compared in a lot of different places. When I check a user's access to a form, I need to know what BusinessFunction that form is associated with, and this is the crux of my question: is there a way I can have my forms share this property, but which would allow me to get the value for a specific form without needing to instantiate it? e.g. so I can call CheckAccess(LibraryForm.BusinessFunction) without needing to instantiate LibraryForm and call CheckAccess() inside.
I've considered making a list of consts somewhere where everything has access to it, and I will likely still need to do that anyway, but aside from naming them in a way that very clearly correlates them with a form, this would only get me some of the way there.
To be clear, I'm not asking if or why I can't use static abstract or static virtual properties (there are already lots of questions asking about those), I'm asking if there is a pattern or an alternative approach I can use to get the result I'm after. Unfortunately this is a .net Framework app so I can't use new static virtual interface members.
A few potential approaches:
Constants class with const string LIBRARY_BUSINESS_FUNCTION = "Manage book rentals" or const string MANAGE_BOOK_RENTALS Easy enough to implement but a bit cumbersome and not trivial to maintain static Dictionary<string FormName, string BusinessFunction> FormFunctions This would let me call FormFunctions[nameof(LibraryForm)] I can't put this somewhere with access to the library which defines my forms or I'd have a circular reference, so the dictionary keys would be string literals - very difficult to maintain and scale Using Attributes This would get me closer to "static" access but it pushes the checks to runtime and involves a fair amount of reflection - I would lose some type safety