Hi all,
I would appreciate a hand in designing my code. So far I haven't found a way to do this.
I have a number of classes for different entities (currently 7 but there can be more in the future). I need a method in each of these classes that authorizes user's access to a file resource. Implementation varies but an exception must be thrown if user does not have access to the file in question.
Problem is, when a file is being accessed, I need to call the authorization method of the correct class based on the data on the file. Files are stored in a db table and one of the fields in that table determines which entity the file is related to and how to find which user has access to the file.
So I must read the file data from db, then see which entity it's related to and then call the authorization method of the correct class.
Is there a way in C# to accomplish this without having to make a switch case structure on the file field value?
Example of the classes used:
- public class FileAttachment
- {
-
-
- protected void AuthorizeFileAccess(FileAttachmentData file)
- {
-
-
- }
- }
-
-
-
- public class ChatMessage
- {
- public int SenderUserId;
- public int RecipientUserId;
-
-
-
-
- }
-
- And this is what I want to avoid:
- switch (file.SourceValue)
- {
- case "ChatMessageAttachmentFile":
- var cm = new ChatMessage();
- cm.AuthorizeAttachmentAccess();
- case "OtherAttachmentFile":
- var other = new SomeOtherClass();
- other.AuthorizeAttachmentAccess();
- default:
- throw new Exception("Access denied");
- }