zzzprojects/Dapper-Plus

Allow passing IConfiguration to ValidateLicense, OR add new AddSingleLicense() call?

VictorioBerra opened this issue · 4 comments

ValidateLicense appears to only check appsettings.json, why can't I provide a built IConfiguration to it instead?

I tried to AddLicense before ValidateLicense and it still errored (because I have placeholders that get transformed in my appsettings.json), so an alternative approach could be to NOT check appsettings if someone called AddLicense manually?

IE add a method called AddSingleLicense() which would be the ONLY license ValidateLicense would look for.

My license can come from:

  • args
  • environment variables
  • secrets.json
  • appsettings.[Environment].json
  • Azure KeyVault Configuration Provider

None of which are supported by ValidateLicense today.

This is my workaround for now, you must remove "Z.EntityFramework.Extensions": {...} from appsettings.json :

var config = serviceProviderFromScope.GetRequiredService<IConfiguration>();

var zDapperPlusLicense = config.GetSection("Z.EntityFramework.Extensions.DapperPlusLicense");

DapperPlusManager.AddLicense(
    zDapperPlusLicense.GetValue<string>("LicenseName"),
    zDapperPlusLicense.GetValue<string>("LicenseKey"));

var dapperLicenseValidationResult = DapperPlusManager.ValidateLicense(out var errorMessage);

if (!string.IsNullOrEmpty(errorMessage))
{
    logger.LogError("Error validating DapperPlus license {Error}", errorMessage);
}
else
{
    logger.LogInformation("DapperPlus license validation result {DapperLicenseValidationResult}", dapperLicenseValidationResult);
}

Ideally, it would look like this:

var config = serviceProviderFromScope.GetRequiredService<IConfiguration>();
var dapperLicenseValidationResult = DapperPlusManager.ValidateLicense(config , out var errorMessage);

or:

var config = serviceProviderFromScope.GetRequiredService<IConfiguration>();

// Note this is the section it looks for interally today
var zDapperPlusLicense = config.GetSection("Z.EntityFramework.Extensions");

DapperPlusManager.AddSingleLicense(
    zDapperPlusLicense.GetValue<string>("LicenseName"),
    zDapperPlusLicense.GetValue<string>("LicenseKey"));

// This will now ignore the internal check for appsettings.json
var dapperLicenseValidationResult = DapperPlusManager.ValidateLicense(out var errorMessage);

Hello @VictorioBerra ,

Thank you for reporting.

I will assign this to my developer to discuss if we should improve this part or not of our code.

Best Regards,

Jon

Hello @VictorioBerra ,

We discussed this requirement and we will not plan to do it for now. The major disadvantage is the new dependencies that will be required.

As you listed, there are multiple ways where key/value information can be stored and we cannot support them all. That's why the AddLicense method exists which let you read the license name and license value wherever you stored it. Indeed, we don't read it automatically but we believe that using the AddLicense method is simple enough.

As long as you use the AddLicense before the ValidateLicense, everything should be validated correctly.

I'm not really sure about your section name: Z.EntityFramework.Extensions.DapperPlusLicense, it seems very weird but maybe it was only an example/typo.

Let me know if that answer correctly to your request even if the answer is negative.

Best Regards,

Jon

Makes sense, thanks for discussing!