committed by
GitHub
13 changed files with 211 additions and 21 deletions
@ -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>(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -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); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,9 @@ |
|||||
|
using NRules.Fluent.Dsl; |
||||
|
using Volo.Abp.DependencyInjection; |
||||
|
|
||||
|
namespace LINGYUN.Abp.Rules.NRules |
||||
|
{ |
||||
|
public abstract class RuleBase : Rule, ITransientDependency |
||||
|
{ |
||||
|
} |
||||
|
} |
||||
@ -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; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -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> |
||||
@ -0,0 +1,10 @@ |
|||||
|
using Volo.Abp.Modularity; |
||||
|
|
||||
|
namespace LINGYUN.Abp.Rules.NRules |
||||
|
{ |
||||
|
[DependsOn( |
||||
|
typeof(AbpNRulesModule))] |
||||
|
public class AbpNRulesTestModule : AbpModule |
||||
|
{ |
||||
|
} |
||||
|
} |
||||
@ -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; } |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,10 @@ |
|||||
|
namespace LINGYUN.Abp.Rules.NRules |
||||
|
{ |
||||
|
public class TestInputRule : RuleBase |
||||
|
{ |
||||
|
public override void Define() |
||||
|
{ |
||||
|
// TODO: Test
|
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue