Choosing a Design for Integrating License Processing

For our .Net application, we have an integrated licensing component in the application. In the application code, we check the license and on its basis we need to enable / disable some functions. For example, if a user has a trial license, they can only access certain sections of the application, while a paid user can access any part. I need help with design decisions, so the impact is minimal. I don't want to sprinkle code with if..else. Please share with me some optimal ways to achieve this.

+4
source share
4 answers

As always, the answer is to replace the conditional with polymorphism .

Encapsulate all functions as Strategy and implement a disabled function as a Null Object . Both can then be encapsulated in a conditional Composite , which selects between them based on the licensing component.

As an example, imagine that you have a print function encapsulated behind the IPrinter interface. You implement the real function in the RealPrinter class and disable the NullPrinter class NullPrinter by implementing the IPrinter interface.

License verification can be implemented in the GuardedPrinter class, which will look something like this:

 public class GuardedPrinter : IPrinter { private readonly IPrinter realPrinter; private readonly IPrinter nullPrinter; // more fields and initialization omitted for brevety... public PrintResult Print() { if (this.licence.IsEnabled) { return this.realPrinter.Print(); } return this.nullPrinter.Print(); } } 
+5
source

Would you like If / Else instructions to check for enabled features or specifically request a licensed system among your main program logic?

I don’t see how you can disable functionality without If / Else instructions, but you can keep it pretty neat if you have a license class that takes care of everything for you.

I will have a license class that initializes when the application starts and has a public bool IsFeatureLicensed(string MethodName) .

Each method that provides the function that you want to protect under license will have the following:

 If (LicenceManager.IsFeatureLicensed(Reflection.MethodBase.GetCurrentMethod.Name)) { //Do your stuff! } else { //Throw exception or show error message or something. } 

The IsFeatureLicensed method in the LicenseManager class that you create will look for the method name and check whether the license allows you to use the function that the method provides. It should return True in all cases except when the license prohibits the use of this function.

Thus, each method makes an identical call to the license manager (which simplifies its maintenance), and everything related to licensing is encapsulated in one class. This allows you to very easily change the licensing method (for example, you can simply return True for everything during development), and the rest of the application does not need to be touched.

+2
source

You can plan for multiple “licensed products” for each product that has its own set of unique license keys. Product A can unlock functions 1,2 and 3, while Product B can only unlock function 1. In your constructor for which you are creating an instance of Feature 1, you can check for a specific licensing code to verify that this license key is valid for product A OR Product B. You can do the same for Feature 2 and 3, which only checks the license key related to product B.

Validation should be performed statically, so if the validation is the first time, then when you should never verify the validity of the validation if an instance of function A. The static class of the licensing manager can handle this.

If the trial license key and some functions are disabled, I would include this trial date in the license key - so even if the license key is checked, if you see the trial date, this will be an exception from the constructor level.

+1
source

Perhaps you could use the .NET licx technique described here?

Licx-based .NET licensing model

0
source

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


All Articles