Reputation: 42669
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.
Upvotes: 4
Views: 1104
Reputation:
You can plan for having several "licensed products" with each product having its own set of unique license keys. Product A may unlock Feature 1,2 and 3 while Product B may only unlock Feature 1. In your constructor for where Feature 1 is instantiated, you could check for against a specific licensing code to check for whether this license key is valid for Product A OR Product B. You could do the same for Feature 2 and 3 which checks for only license key that pertains to Product B.
The check should be done statically such that if validation has passed the first time, then when you should never check for validation again if Feature A is instantiated. A static licensing manager class can probably handle this.
In the case of having a trial license key and certain features being disabled, I would embed that trial date into the license key - so even if the license key is validated, if you see a trial date, it would exception out at the constructor level.
Upvotes: 1
Reputation: 3777
maybe you could use .NET licx technique described here?
Licx based .NET licensing model
Upvotes: 0
Reputation: 5137
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.
Upvotes: 2
Reputation: 233237
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();
}
}
Upvotes: 5