Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

refactor(RulesEngine): Unified rule engine registration method #434

Merged
merged 2 commits into from
Feb 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

// ReSharper disable once CheckNamespace

namespace Masa.BuildingBlocks.RulesEngine;

public class RulesEngineOptions
{
public IServiceCollection Services { get; }

public RulesEngineOptions(IServiceCollection services)
public string Name { get; }

public RulesEngineOptions(IServiceCollection services, string name)
{
Services = services;
Name = name;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ namespace Microsoft.Extensions.DependencyInjection;
public static class ServiceCollectionExtensions
{
public static IServiceCollection AddRulesEngine(this IServiceCollection services, Action<RulesEngineOptions> action)
=> services.AddRulesEngine(Microsoft.Extensions.Options.Options.DefaultName, action);

public static IServiceCollection AddRulesEngine(this IServiceCollection services, string name, Action<RulesEngineOptions> action)
{
MasaApp.TrySetServiceCollection(services);
services.TryAddSingleton<IRulesEngineFactory, DefaultRulesEngineFactory>();
services.TryAddTransient(serviceProvider => serviceProvider.GetRequiredService<IRulesEngineFactory>().Create());
RulesEngineOptions rulesEngineOptions = new RulesEngineOptions(services);
RulesEngineOptions rulesEngineOptions = new RulesEngineOptions(services, name);
action.Invoke(rulesEngineOptions);
return services;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,15 @@ namespace Masa.BuildingBlocks.RulesEngine;
public static class RulesEngineOptionsExtensions
{
public static RulesEngineOptions UseMicrosoftRulesEngine(this RulesEngineOptions rulesEngineOptions)
=> rulesEngineOptions.UseMicrosoftRulesEngine(Options.DefaultName);

public static RulesEngineOptions UseMicrosoftRulesEngine(this RulesEngineOptions rulesEngineOptions, ReSettings? reSettings)
=> rulesEngineOptions.UseMicrosoftRulesEngine(Options.DefaultName, reSettings);
=> rulesEngineOptions.UseMicrosoftRulesEngine(null);

public static RulesEngineOptions UseMicrosoftRulesEngine(
this RulesEngineOptions rulesEngineOptions,
string name,
ReSettings? reSettings = null)
ReSettings? reSettings)
{
rulesEngineOptions.Services.Configure<RulesEngineFactoryOptions>(name, options =>
rulesEngineOptions.Services.Configure<RulesEngineFactoryOptions>(options =>
{
options.TryAddRulesEngine(name,
options.TryAddRulesEngine(rulesEngineOptions.Name,
serviceProvider => new RulesEngineClient(reSettings, serviceProvider.GetService<ILogger<RulesEngineClient>>())
);
});
Expand Down