Base solution for your next web application
Open Closed

DefaultEditionCreator on startup #4714


User avatar
0
BobIngham created

I have three editions based on the number of devices registered with the system. The number of devices is defined as a feature. In DefaultEditionCreator I create these editions, for example here is the default edition:

var defaultEdition = _context.Editions.IgnoreQueryFilters().FirstOrDefault(e => e.Name == EditionManager.DefaultEditionName);
if (defaultEdition == null)
{
    defaultEdition = new SubscribableEdition {
        Name = EditionManager.DefaultEditionName,
        DisplayName = EditionManager.DefaultEditionName,
        MonthlyPrice = 200,
        AnnualPrice = 2000,
        TrialDayCount = 14,
        WaitingDayAfterExpire = 3
    };
    _context.Editions.Add(defaultEdition);
    _context.SaveChanges();

    CreateFeatureIfNotExists(defaultEdition.Id, AppFeatures.MaxUserCount, false);
    CreateFeatureIfNotExists(defaultEdition.Id, AppFeatures.MaxDeviceCount, true);
}

Now I create my ten device edition:

var tenDeviceEdition = _context.Editions.IgnoreQueryFilters().FirstOrDefault(e => e.Name == "10 devices");
if (tenDeviceEdition == null)
{
    tenDeviceEdition = new SubscribableEdition
    {
        Name = "10 devices",
        DisplayName = "10 devices",
        MonthlyPrice = 350,
        AnnualPrice = 3500,
        WaitingDayAfterExpire = 7
    };
    _context.Editions.Add(tenDeviceEdition);
    _context.SaveChanges();

    CreateFeatureIfNotExists(tenDeviceEdition.Id, AppFeatures.MaxUserCount, false);
    CreateFeatureIfNotExists(tenDeviceEdition.Id, AppFeatures.MaxDeviceCount, true);
}

Now I want to set the value of MaxDeviceCount for the ten device edition to 10. The method DefaultEditionCreator.CreateEditions() is synchronous. All of the methods for EditionManager and IEditionAppService are asynchronous. I want to set the edition feature value in a similar way to that used in the test, EditionAppService_Tests.Should_Update_Edition():

var output = await _editionAppService.GetEditionForEdit(new NullableIdDto(defaultEdition.Id));

            //Changing a sample feature value
            var chatFeature = output.FeatureValues.FirstOrDefault(f => f.Name == AppFeatures.ChatFeature);
            if (chatFeature != null)
            {
                chatFeature.Value = chatFeature.Value = "true";
            }

My question is:

Is there any way to set feature values for editions in DefaultEditionCreator.CreateEditions()?


1 Answer(s)
  • User Avatar
    0
    BobIngham created

    Now I see. The method CreateFeatureIfNotExists() is written to work with Boolean Features only, to support testing for chat features. By modifying the method as follows:

    private void CreateFeatureIfNotExists(int editionId, string featureName, string value)
    {
        var defaultEditionChatFeature = _context.EditionFeatureSettings.IgnoreQueryFilters()
                                                    .FirstOrDefault(ef => ef.EditionId == editionId && ef.Name == featureName);
    
        if (defaultEditionChatFeature == null)
        {
            _context.EditionFeatureSettings.Add(new EditionFeatureSetting
            {
                Name = featureName,
                Value = value,
                EditionId = editionId
            });
        }
    }
    

    And passing in a string value as opposed to a boolean for isEnabled. The method is written to work with Boolean Features only, the above method changes it to a Value Feature. In fact I believe the terminology is misleading here, a Boolean Feature is identical to a Value feature except that it holds a string value for "true" or "false". Correct me if I am wrong. I think the source would benefit from making the above changes, as it stands it is not possible to test Value features without modification.