Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Design choices for integrating license handling

For our .Net application we have integrated licensing component into the application. In the application code we validate the license and based on it we need to enable/disable some functionality. For example if the user has trial license they can only access specific sections of application, whereas a paid user can access any part. I need help on deciding the design such that the impact is minimum. I do not want to sprinkle the code with if..else statements. Please share with me some optimal ways to achieve this.

like image 950
Chandermani Avatar asked Feb 25 '23 13:02

Chandermani


2 Answers

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

Encapsulate each features as a Strategy, and implement the disabled feature as a Null Object. Both can then be encapsulated in a conditional Composite that selects between the two based on the licensing component.

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

The license check can be implemented in the GuardedPrinter class, which would 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();
    }
}
like image 156
Mark Seemann Avatar answered Feb 27 '23 03:02

Mark Seemann


Are you averse to having If/Else statements to check for enabled features, or specifically interrogating the license system amidst your main program logic?

I don't see how you can disable functionality without If/Else statements, but you can keep it quite neat if you have a License class that takes care of everything for you.

I would have a license class that is initialised at application launch, and has an public bool IsFeatureLicensed(string MethodName) method.

Every method providing a feature that you wanted to protect by the license would have something like:

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 have a look at the method name and check to see if the license allows use of the function that the method provides. It should return True in all cases except where the license forbids use of the feature.

This way every method makes an identical call to the license manager (making it very easy to maintain), and everything to do with the licensing stuff is encapsulated in a single class. This allows you to change your licensing method very easily (e.g. you can just Return True for everything during development), and the rest of your application doesn't need to be touched.

like image 34
Michael Rodrigues Avatar answered Feb 27 '23 02:02

Michael Rodrigues