Browse Source

Merge pull request #254 from colinin/4.3

add nrules module
pull/274/head
cKey 5 years ago
committed by GitHub
parent
commit
e71765d87e
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 7
      aspnet-core/LINGYUN.MicroService.Common.sln
  2. 29
      aspnet-core/modules/rules/LINGYUN.Abp.Rules.NRules/LINGYUN/Abp/Rules/NRules/AbpNRulesModule.cs
  3. 14
      aspnet-core/modules/rules/LINGYUN.Abp.Rules.NRules/LINGYUN/Abp/Rules/NRules/AbpNRulesOptions.cs
  4. 4
      aspnet-core/modules/rules/LINGYUN.Abp.Rules.NRules/LINGYUN/Abp/Rules/NRules/ActionInterceptor.cs
  5. 4
      aspnet-core/modules/rules/LINGYUN.Abp.Rules.NRules/LINGYUN/Abp/Rules/NRules/DependencyResolver.cs
  6. 26
      aspnet-core/modules/rules/LINGYUN.Abp.Rules.NRules/LINGYUN/Abp/Rules/NRules/NRulesContributor.cs
  7. 35
      aspnet-core/modules/rules/LINGYUN.Abp.Rules.NRules/LINGYUN/Abp/Rules/NRules/RuleActivator.cs
  8. 9
      aspnet-core/modules/rules/LINGYUN.Abp.Rules.NRules/LINGYUN/Abp/Rules/NRules/RuleBase.cs
  9. 30
      aspnet-core/modules/rules/LINGYUN.Abp.Rules.NRules/Microsoft/Extensions/DependencyInjection/NRulesServiceCollectionExtensions.cs
  10. 27
      aspnet-core/tests/LINGYUN.Abp.Rules.NRules.Tests/LINGYUN.Abp.Rules.NRules.Tests.csproj
  11. 10
      aspnet-core/tests/LINGYUN.Abp.Rules.NRules.Tests/LINGYUN/Abp/Rules/NRules/AbpNRulesTestModule.cs
  12. 13
      aspnet-core/tests/LINGYUN.Abp.Rules.NRules.Tests/LINGYUN/Abp/Rules/NRules/TestInput.cs
  13. 10
      aspnet-core/tests/LINGYUN.Abp.Rules.NRules.Tests/LINGYUN/Abp/Rules/NRules/TestInputRule.cs

7
aspnet-core/LINGYUN.MicroService.Common.sln

@ -152,6 +152,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LINGYUN.Abp.Rules.RulesEngi
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LINGYUN.Abp.Rules.RulesEngine.Tests", "tests\LINGYUN.Abp.Rules.RulesEngine.Tests\LINGYUN.Abp.Rules.RulesEngine.Tests.csproj", "{CF253F0A-3A45-40EE-875F-0E57C8968C48}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LINGYUN.Abp.Rules.RulesEngine.Tests", "tests\LINGYUN.Abp.Rules.RulesEngine.Tests\LINGYUN.Abp.Rules.RulesEngine.Tests.csproj", "{CF253F0A-3A45-40EE-875F-0E57C8968C48}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LINGYUN.Abp.Rules.NRules.Tests", "tests\LINGYUN.Abp.Rules.NRules.Tests\LINGYUN.Abp.Rules.NRules.Tests.csproj", "{EF03A0A7-A93E-4DEF-A33E-F5A82E995FBF}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
@ -394,6 +396,10 @@ Global
{CF253F0A-3A45-40EE-875F-0E57C8968C48}.Debug|Any CPU.Build.0 = Debug|Any CPU {CF253F0A-3A45-40EE-875F-0E57C8968C48}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CF253F0A-3A45-40EE-875F-0E57C8968C48}.Release|Any CPU.ActiveCfg = Release|Any CPU {CF253F0A-3A45-40EE-875F-0E57C8968C48}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CF253F0A-3A45-40EE-875F-0E57C8968C48}.Release|Any CPU.Build.0 = Release|Any CPU {CF253F0A-3A45-40EE-875F-0E57C8968C48}.Release|Any CPU.Build.0 = Release|Any CPU
{EF03A0A7-A93E-4DEF-A33E-F5A82E995FBF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EF03A0A7-A93E-4DEF-A33E-F5A82E995FBF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EF03A0A7-A93E-4DEF-A33E-F5A82E995FBF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EF03A0A7-A93E-4DEF-A33E-F5A82E995FBF}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE
@ -470,6 +476,7 @@ Global
{B8E5026B-188F-422F-A1EA-502C4A394585} = {C8891F1A-E6E5-448A-B527-EBFA44D20808} {B8E5026B-188F-422F-A1EA-502C4A394585} = {C8891F1A-E6E5-448A-B527-EBFA44D20808}
{E861BE01-689D-4637-A5DC-E78E234F83FB} = {C8891F1A-E6E5-448A-B527-EBFA44D20808} {E861BE01-689D-4637-A5DC-E78E234F83FB} = {C8891F1A-E6E5-448A-B527-EBFA44D20808}
{CF253F0A-3A45-40EE-875F-0E57C8968C48} = {B86C21A4-73B7-471E-B73A-B4B905EC9435} {CF253F0A-3A45-40EE-875F-0E57C8968C48} = {B86C21A4-73B7-471E-B73A-B4B905EC9435}
{EF03A0A7-A93E-4DEF-A33E-F5A82E995FBF} = {B86C21A4-73B7-471E-B73A-B4B905EC9435}
EndGlobalSection EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {06C707C6-02C0-411A-AD3B-2D0E13787CB8} SolutionGuid = {06C707C6-02C0-411A-AD3B-2D0E13787CB8}

29
aspnet-core/modules/rules/LINGYUN.Abp.Rules.NRules/LINGYUN/Abp/Rules/NRules/AbpNRulesModule.cs

@ -1,6 +1,6 @@
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using NRules.Fluent; using System;
using NRules.RuleModel; using System.Collections.Generic;
using Volo.Abp.Modularity; using Volo.Abp.Modularity;
namespace LINGYUN.Abp.Rules.NRules namespace LINGYUN.Abp.Rules.NRules
@ -9,14 +9,37 @@ namespace LINGYUN.Abp.Rules.NRules
typeof(AbpRulesModule))] typeof(AbpRulesModule))]
public class AbpNRulesModule : AbpModule public class AbpNRulesModule : AbpModule
{ {
public override void PreConfigureServices(ServiceConfigurationContext context)
{
AddDefinitionRules(context.Services);
}
public override void ConfigureServices(ServiceConfigurationContext context) public override void ConfigureServices(ServiceConfigurationContext context)
{ {
context.Services.AddSingleton<IRuleRepository, RuleRepository>(); context.Services.AddNRules();
Configure<AbpRulesOptions>(options => Configure<AbpRulesOptions>(options =>
{ {
options.Contributors.Add<NRulesContributor>(); options.Contributors.Add<NRulesContributor>();
}); });
} }
private static void AddDefinitionRules(IServiceCollection services)
{
var definitionRules = new List<Type>();
services.OnRegistred(context =>
{
if (typeof(RuleBase).IsAssignableFrom(context.ImplementationType))
{
definitionRules.Add(context.ImplementationType);
}
});
services.Configure<AbpNRulesOptions>(options =>
{
options.DefinitionRules.AddIfNotContains(definitionRules);
});
}
} }
} }

14
aspnet-core/modules/rules/LINGYUN.Abp.Rules.NRules/LINGYUN/Abp/Rules/NRules/AbpNRulesOptions.cs

@ -0,0 +1,14 @@
using Volo.Abp.Collections;
namespace LINGYUN.Abp.Rules.NRules
{
public class AbpNRulesOptions
{
public ITypeList<RuleBase> DefinitionRules { get; }
public AbpNRulesOptions()
{
DefinitionRules = new TypeList<RuleBase>();
}
}
}

4
aspnet-core/modules/rules/LINGYUN.Abp.Rules.NRules/LINGYUN/Abp/Rules/NRules/DefaultActionInterceptor.cs → aspnet-core/modules/rules/LINGYUN.Abp.Rules.NRules/LINGYUN/Abp/Rules/NRules/ActionInterceptor.cs

@ -4,11 +4,11 @@ using System.Collections.Generic;
namespace LINGYUN.Abp.Rules.NRules namespace LINGYUN.Abp.Rules.NRules
{ {
public class DefaultActionInterceptor : IActionInterceptor public class ActionInterceptor : IActionInterceptor
{ {
public void Intercept(IContext context, IEnumerable<IActionInvocation> actions) public void Intercept(IContext context, IEnumerable<IActionInvocation> actions)
{ {
// TODO: Intercept
} }
} }
} }

4
aspnet-core/modules/rules/LINGYUN.Abp.Rules.NRules/LINGYUN/Abp/Rules/NRules/DefaultDependencyResolver.cs → aspnet-core/modules/rules/LINGYUN.Abp.Rules.NRules/LINGYUN/Abp/Rules/NRules/DependencyResolver.cs

@ -3,11 +3,11 @@ using System;
namespace LINGYUN.Abp.Rules.NRules namespace LINGYUN.Abp.Rules.NRules
{ {
public class DefaultDependencyResolver : IDependencyResolver public class DependencyResolver : IDependencyResolver
{ {
private readonly IServiceProvider _serviceProvider; private readonly IServiceProvider _serviceProvider;
public DefaultDependencyResolver( public DependencyResolver(
IServiceProvider serviceProvider) IServiceProvider serviceProvider)
{ {
_serviceProvider = serviceProvider; _serviceProvider = serviceProvider;

26
aspnet-core/modules/rules/LINGYUN.Abp.Rules.NRules/LINGYUN/Abp/Rules/NRules/NRulesContributor.cs

@ -1,6 +1,8 @@
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using NRules; using NRules;
using NRules.RuleModel; using NRules.Fluent;
using System;
using System.Linq; using System.Linq;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -10,19 +12,28 @@ namespace LINGYUN.Abp.Rules.NRules
{ {
public class NRulesContributor : RuleContributorBase, ISingletonDependency public class NRulesContributor : RuleContributorBase, ISingletonDependency
{ {
private ISessionFactory _sessionFactory; private readonly AbpNRulesOptions _options;
private readonly IServiceProvider _serviceProvider;
public NRulesContributor(
IServiceProvider serviceProvider,
IOptions<AbpNRulesOptions> options)
{
_options = options.Value;
_serviceProvider = serviceProvider;
}
public override void Initialize(RulesInitializationContext context) public override void Initialize(RulesInitializationContext context)
{ {
var repository = context.GetRequiredService<IRuleRepository>(); context.GetRequiredService<RuleRepository>()
_sessionFactory = repository.Compile(); .Load(loader => loader.From(_options.DefinitionRules));
_sessionFactory.DependencyResolver = new DefaultDependencyResolver(context.ServiceProvider);
_sessionFactory.ActionInterceptor = new DefaultActionInterceptor();
} }
public override Task ExecuteAsync<T>(T input, object[] @params = null, CancellationToken cancellationToken = default) public override Task ExecuteAsync<T>(T input, object[] @params = null, CancellationToken cancellationToken = default)
{ {
var session = _sessionFactory.CreateSession(); using (var scope = _serviceProvider.CreateScope())
{
var session = scope.ServiceProvider.GetRequiredService<ISession>();
session.Insert(input); session.Insert(input);
if (@params != null && @params.Any()) if (@params != null && @params.Any())
@ -32,6 +43,7 @@ namespace LINGYUN.Abp.Rules.NRules
// TODO: 需要研究源码 // TODO: 需要研究源码
session.Fire(cancellationToken); session.Fire(cancellationToken);
}
return Task.CompletedTask; return Task.CompletedTask;
} }

35
aspnet-core/modules/rules/LINGYUN.Abp.Rules.NRules/LINGYUN/Abp/Rules/NRules/RuleActivator.cs

@ -0,0 +1,35 @@
using NRules.Fluent;
using NRules.Fluent.Dsl;
using System;
using System.Collections.Generic;
namespace LINGYUN.Abp.Rules.NRules
{
public class RuleActivator : IRuleActivator
{
private readonly IServiceProvider _serviceProvider;
public RuleActivator(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
}
public IEnumerable<Rule> Activate(Type type)
{
var collectionType = typeof(IEnumerable<>).MakeGenericType(type);
var rules = _serviceProvider.GetService(collectionType);
if (rules != null)
{
return (IEnumerable<Rule>)rules;
}
return ActivateDefault(type);
}
private static IEnumerable<Rule> ActivateDefault(Type type)
{
yield return (Rule)Activator.CreateInstance(type);
}
}
}

9
aspnet-core/modules/rules/LINGYUN.Abp.Rules.NRules/LINGYUN/Abp/Rules/NRules/RuleBase.cs

@ -0,0 +1,9 @@
using NRules.Fluent.Dsl;
using Volo.Abp.DependencyInjection;
namespace LINGYUN.Abp.Rules.NRules
{
public abstract class RuleBase : Rule, ITransientDependency
{
}
}

30
aspnet-core/modules/rules/LINGYUN.Abp.Rules.NRules/Microsoft/Extensions/DependencyInjection/NRulesServiceCollectionExtensions.cs

@ -0,0 +1,30 @@
using LINGYUN.Abp.Rules.NRules;
using NRules;
using NRules.Extensibility;
using NRules.Fluent;
using NRules.RuleModel;
namespace Microsoft.Extensions.DependencyInjection
{
public static class NRulesServiceCollectionExtensions
{
public static IServiceCollection AddNRules(this IServiceCollection services)
{
services.AddSingleton<IActionInterceptor, ActionInterceptor>();
services.AddSingleton<IRuleActivator, RuleActivator>();
services.AddSingleton<IDependencyResolver, DependencyResolver>();
services.AddSingleton<IRuleRepository, RuleRepository>();
services.AddSingleton((serviceProvider) =>
{
return serviceProvider.GetRequiredService<IRuleRepository>().Compile();
});
services.AddScoped((serviceProvider) =>
{
return serviceProvider.GetRequiredService<ISessionFactory>().CreateSession();
});
return services;
}
}
}

27
aspnet-core/tests/LINGYUN.Abp.Rules.NRules.Tests/LINGYUN.Abp.Rules.NRules.Tests.csproj

@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<RootNamespace />
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="1.3.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\modules\rules\LINGYUN.Abp.Rules.NRules\LINGYUN.Abp.Rules.NRules.csproj" />
<ProjectReference Include="..\LINGYUN.Abp.TestBase\LINGYUN.Abp.TestsBase.csproj" />
</ItemGroup>
</Project>

10
aspnet-core/tests/LINGYUN.Abp.Rules.NRules.Tests/LINGYUN/Abp/Rules/NRules/AbpNRulesTestModule.cs

@ -0,0 +1,10 @@
using Volo.Abp.Modularity;
namespace LINGYUN.Abp.Rules.NRules
{
[DependsOn(
typeof(AbpNRulesModule))]
public class AbpNRulesTestModule : AbpModule
{
}
}

13
aspnet-core/tests/LINGYUN.Abp.Rules.NRules.Tests/LINGYUN/Abp/Rules/NRules/TestInput.cs

@ -0,0 +1,13 @@
namespace LINGYUN.Abp.Rules.NRules
{
public class TestInput
{
public string Required { get; set; }
public int Integer1 { get; set; }
public int Integer2 { get; set; }
public string Length { get; set; }
}
}

10
aspnet-core/tests/LINGYUN.Abp.Rules.NRules.Tests/LINGYUN/Abp/Rules/NRules/TestInputRule.cs

@ -0,0 +1,10 @@
namespace LINGYUN.Abp.Rules.NRules
{
public class TestInputRule : RuleBase
{
public override void Define()
{
// TODO: Test
}
}
}
Loading…
Cancel
Save