Handling various types of users in an application

Many of the web applications I contributed to (mainly ASP.NET) need to handle several and different types of users.

Say you have a school portal that both students and teachers use daily. On the first page of the application, users come across almost the same graphical interface, with the exception of some links to some tools that only teachers have access to. Let's say this is a messaging tool. The teacher can have different roles that determine who the teacher can send to.

For instance:

  • A teacher with the Publisher role is allowed to send everyone to the school.
  • A teacher who does not have additional roles is allowed to send only to all his classes.
  • In the future, parents will also be able to access this portal and view detailed information about their children.

The problem that I always run into is that my code is always cluttered with if -statements when handling different types of users throughout the application. Not only because of different types of users, but also according to different business rules. I feel like I can’t find a way to properly handle different types of users.

I assume the role concept in ASP.NET solves this, but you would still end up with if statistics around the application.

My question is: is there any best practice on how to handle different types of users / users in an application without infecting the code with if -statements?

+5
source share
1 answer

You must share these responsibilities (for example, the ability to send a teacher). You can do this using a strategy template.

So, the teacher has an additional property Publishing, which serves as an interface. An implementation may have several implementations (for example, NoPublishing for a teacher who does not have publishing functions, or DefaultPublishing). Each teacher can have the Publishing property set to either NoPublishing or DefaultPublishing. If necessary, you can even change the runtime.

Example:

 public class Teacher { public IPublishing Publishing { get; } } interface IPublishing { void Send(); } public NoPublishing : IPublishing { public void Send() { // Implementatation } } public PublishDefault : IPublishing { public void Send() { // Send a message the default way } } 

Create a teacher:

 var teacher = new Teacher(); 

Create a publisher strategy.

 var defaultStrategy = new PublishDefault(); 

Plug them

 teacher.Publishing = defaultStrategy; 

Now you can send a message:

 teacher.Publishing.Send(); 

Depending on which publishing strategy has been enabled, it will not send or send anything by default.

You only need to create an instance of each used publishing strategy once and reuse it for each Teacher (or even other classes that should be able to send).

If you need other publishing features, just add a new strategy (e.g. SmsPublishing, LetterPublishing, etc.).

You can even change the strategy on the fly if necessary (by reassigning the publication property).

Why not implement the interface not directly in Teacher?

  • The principle of separation of ideas: IPublish contains a certain and different responsibility.
  • IPublish may contain functionality that can be used later in different classes or even in other projects, so they can be reused.
  • Testing is easier because IPublish does not need any knowledge of Teacher.
  • The ability to change publisher behavior in real time.

(note: I don't have a compiler here, so the code is for explanation only).

+2
source

Source: https://habr.com/ru/post/1238079/


All Articles