Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Use features to conditionally register services #132

Closed
unaizorrilla opened this issue Jun 16, 2020 · 2 comments
Closed

Use features to conditionally register services #132

unaizorrilla opened this issue Jun 16, 2020 · 2 comments
Labels
Core enhancement New feature or request
Milestone

Comments

@unaizorrilla
Copy link
Collaborator

Add new IServiceCollection extensions to enable services depending on feature state, is like a factory but with some sugar to simplify developers code

@unaizorrilla unaizorrilla added enhancement New feature or request Core labels Jun 16, 2020
@unaizorrilla unaizorrilla added this to the Future milestone Jun 16, 2020
@unaizorrilla unaizorrilla modified the milestones: Future, Esquio-3X Jul 3, 2020
unaizorrilla added a commit that referenced this issue Jul 3, 2020
@unaizorrilla
Copy link
Collaborator Author

Fixed on features/esquio31 and will be available on Esquio 3.1

       /// <summary>
       /// Add a new service depending on a feature state.
       /// </summary>
       /// <typeparam name="TService">The system type of the service.</typeparam>
       /// <param name="services">The <see cref="IServiceCollection"/> used.</param>
       /// <param name="featureName">The feature name to be evaluated.</param>
       /// <param name="featureEnabled">The factory used to create the service implementation when the feature is enabled.</param>
       /// <param name="featureDisabled">The factory used to create the service implementation when the feature is disabled.</param>
       /// <param name="serviceLifetime">The service lifetime to set.</param>
       /// <returns>The collection of configured services.</returns>
       public static IServiceCollection Add<TService>(this IServiceCollection services,
           string featureName,
           Func<IServiceProvider, object> featureEnabled,
           Func<IServiceProvider, object> featureDisabled,
           ServiceLifetime serviceLifetime = ServiceLifetime.Transient)
       {
           _ = featureName ?? throw new ArgumentNullException(nameof(featureName));
           _ = featureEnabled ?? throw new ArgumentNullException(nameof(featureEnabled));
           _ = featureDisabled ?? throw new ArgumentNullException(nameof(featureDisabled));

           var descriptor = new ServiceDescriptor(typeof(TService), serviceProvider =>
           {
               var featureService = serviceProvider.GetService<IFeatureService>();

               var enabled = featureService.IsEnabledAsync(featureName)
                   .GetAwaiter()
                   .GetResult();

               return enabled ? featureEnabled(serviceProvider) : featureDisabled(serviceProvider);
           }, serviceLifetime);

           services.Add(descriptor);

           return services;
       }

@unaizorrilla
Copy link
Collaborator Author

After a review on this feature we decide to remove from 3.1!

Adding services to container and blocking with IFeatureService.IsEnabledAsync could be a performance problem. And this scenario will be solved easly with a factory!

We try to add a sample of this scenario but remove this Add extension method!!

unaizorrilla added a commit that referenced this issue Jul 8, 2020
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Core enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant