mirror of https://github.com/abpframework/abp.git
16 changed files with 279 additions and 4 deletions
@ -1,9 +1,33 @@ |
|||||
using Volo.Abp.Modularity; |
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using Microsoft.Extensions.DependencyInjection; |
||||
|
using Volo.Abp.Modularity; |
||||
|
|
||||
namespace Volo.Abp.TextTemplating |
namespace Volo.Abp.TextTemplating |
||||
{ |
{ |
||||
public class AbpTextTemplatingModule : AbpModule |
public class AbpTextTemplatingModule : AbpModule |
||||
{ |
{ |
||||
|
public override void PreConfigureServices(ServiceConfigurationContext context) |
||||
|
{ |
||||
|
AutoAddDefinitionProviders(context.Services); |
||||
|
} |
||||
|
|
||||
|
private static void AutoAddDefinitionProviders(IServiceCollection services) |
||||
|
{ |
||||
|
var definitionProviders = new List<Type>(); |
||||
|
|
||||
|
services.OnRegistred(context => |
||||
|
{ |
||||
|
if (typeof(ITemplateDefinitionProvider).IsAssignableFrom(context.ImplementationType)) |
||||
|
{ |
||||
|
definitionProviders.Add(context.ImplementationType); |
||||
|
} |
||||
|
}); |
||||
|
|
||||
|
services.Configure<AbpTextTemplatingOptions>(options => |
||||
|
{ |
||||
|
options.DefinitionProviders.AddIfNotContains(definitionProviders); |
||||
|
}); |
||||
|
} |
||||
} |
} |
||||
} |
} |
||||
|
|||||
@ -0,0 +1,14 @@ |
|||||
|
using Volo.Abp.Collections; |
||||
|
|
||||
|
namespace Volo.Abp.TextTemplating |
||||
|
{ |
||||
|
public class AbpTextTemplatingOptions |
||||
|
{ |
||||
|
public ITypeList<ITemplateDefinitionProvider> DefinitionProviders { get; } |
||||
|
|
||||
|
public AbpTextTemplatingOptions() |
||||
|
{ |
||||
|
DefinitionProviders = new TypeList<ITemplateDefinitionProvider>(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,9 @@ |
|||||
|
namespace Volo.Abp.TextTemplating |
||||
|
{ |
||||
|
public interface ITemplateDefinitionContext |
||||
|
{ |
||||
|
TemplateDefinition GetOrNull(string name); |
||||
|
|
||||
|
void Add(params TemplateDefinition[] definitions); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,15 @@ |
|||||
|
using System.Collections.Generic; |
||||
|
using JetBrains.Annotations; |
||||
|
|
||||
|
namespace Volo.Abp.TextTemplating |
||||
|
{ |
||||
|
public interface ITemplateDefinitionManager |
||||
|
{ |
||||
|
[NotNull] |
||||
|
TemplateDefinition Get([NotNull] string name); |
||||
|
|
||||
|
IReadOnlyList<TemplateDefinition> GetAll(); |
||||
|
|
||||
|
TemplateDefinition GetOrNull(string name); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,7 @@ |
|||||
|
namespace Volo.Abp.TextTemplating |
||||
|
{ |
||||
|
public interface ITemplateDefinitionProvider |
||||
|
{ |
||||
|
void Define(ITemplateDefinitionContext context); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,38 @@ |
|||||
|
using System.Collections.Generic; |
||||
|
using System.Collections.Immutable; |
||||
|
|
||||
|
namespace Volo.Abp.TextTemplating |
||||
|
{ |
||||
|
public class TemplateDefinitionContext : ITemplateDefinitionContext |
||||
|
{ |
||||
|
protected Dictionary<string, TemplateDefinition> EmailTemplates { get; } |
||||
|
|
||||
|
public TemplateDefinitionContext(Dictionary<string, TemplateDefinition> emailTemplates) |
||||
|
{ |
||||
|
EmailTemplates = emailTemplates; |
||||
|
} |
||||
|
|
||||
|
public virtual TemplateDefinition GetOrNull(string name) |
||||
|
{ |
||||
|
return EmailTemplates.GetOrDefault(name); |
||||
|
} |
||||
|
|
||||
|
public virtual IReadOnlyList<TemplateDefinition> GetAll() |
||||
|
{ |
||||
|
return EmailTemplates.Values.ToImmutableList(); |
||||
|
} |
||||
|
|
||||
|
public virtual void Add(params TemplateDefinition[] definitions) |
||||
|
{ |
||||
|
if (definitions.IsNullOrEmpty()) |
||||
|
{ |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
foreach (var definition in definitions) |
||||
|
{ |
||||
|
EmailTemplates[definition.Name] = definition; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,74 @@ |
|||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Collections.Immutable; |
||||
|
using System.Linq; |
||||
|
using Microsoft.Extensions.DependencyInjection; |
||||
|
using Microsoft.Extensions.Options; |
||||
|
using Volo.Abp.DependencyInjection; |
||||
|
|
||||
|
namespace Volo.Abp.TextTemplating |
||||
|
{ |
||||
|
public class TemplateDefinitionManager : ITemplateDefinitionManager, ISingletonDependency |
||||
|
{ |
||||
|
protected Lazy<IDictionary<string, TemplateDefinition>> EmailTemplateDefinitions { get; } |
||||
|
|
||||
|
protected AbpTextTemplatingOptions Options { get; } |
||||
|
|
||||
|
protected IServiceProvider ServiceProvider { get; } |
||||
|
|
||||
|
public TemplateDefinitionManager( |
||||
|
IOptions<AbpTextTemplatingOptions> options, |
||||
|
IServiceProvider serviceProvider) |
||||
|
{ |
||||
|
ServiceProvider = serviceProvider; |
||||
|
Options = options.Value; |
||||
|
|
||||
|
EmailTemplateDefinitions = |
||||
|
new Lazy<IDictionary<string, TemplateDefinition>>(CreateEmailTemplateDefinitions, true); |
||||
|
} |
||||
|
|
||||
|
public virtual TemplateDefinition Get(string name) |
||||
|
{ |
||||
|
Check.NotNull(name, nameof(name)); |
||||
|
|
||||
|
var template = GetOrNull(name); |
||||
|
|
||||
|
if (template == null) |
||||
|
{ |
||||
|
throw new AbpException("Undefined template: " + name); |
||||
|
} |
||||
|
|
||||
|
return template; |
||||
|
} |
||||
|
|
||||
|
public virtual IReadOnlyList<TemplateDefinition> GetAll() |
||||
|
{ |
||||
|
return EmailTemplateDefinitions.Value.Values.ToImmutableList(); |
||||
|
} |
||||
|
|
||||
|
public virtual TemplateDefinition GetOrNull(string name) |
||||
|
{ |
||||
|
return EmailTemplateDefinitions.Value.GetOrDefault(name); |
||||
|
} |
||||
|
|
||||
|
protected virtual IDictionary<string, TemplateDefinition> CreateEmailTemplateDefinitions() |
||||
|
{ |
||||
|
var templates = new Dictionary<string, TemplateDefinition>(); |
||||
|
|
||||
|
using (var scope = ServiceProvider.CreateScope()) |
||||
|
{ |
||||
|
var providers = Options |
||||
|
.DefinitionProviders |
||||
|
.Select(p => scope.ServiceProvider.GetRequiredService(p) as ITemplateDefinitionProvider) |
||||
|
.ToList(); |
||||
|
|
||||
|
foreach (var provider in providers) |
||||
|
{ |
||||
|
provider.Define(new TemplateDefinitionContext(templates)); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
return templates; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,9 @@ |
|||||
|
using Volo.Abp.DependencyInjection; |
||||
|
|
||||
|
namespace Volo.Abp.TextTemplating |
||||
|
{ |
||||
|
public abstract class TemplateDefinitionProvider : ITemplateDefinitionProvider, ITransientDependency |
||||
|
{ |
||||
|
public abstract void Define(ITemplateDefinitionContext context); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,24 @@ |
|||||
|
using Microsoft.Extensions.Options; |
||||
|
using Shouldly; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Volo.Abp.TextTemplating |
||||
|
{ |
||||
|
public class AbpTextTemplatingOptions_Tests : AbpTextTemplatingTestBase |
||||
|
{ |
||||
|
private readonly AbpTextTemplatingOptions _options; |
||||
|
|
||||
|
public AbpTextTemplatingOptions_Tests() |
||||
|
{ |
||||
|
_options = GetRequiredService<IOptions<AbpTextTemplatingOptions>>().Value; |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_Auto_Add_TemplateDefinitionProviders_To_Options() |
||||
|
{ |
||||
|
_options |
||||
|
.DefinitionProviders |
||||
|
.ShouldContain(typeof(TestTemplateDefinitionProvider)); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,12 @@ |
|||||
|
using Volo.Abp.Testing; |
||||
|
|
||||
|
namespace Volo.Abp.TextTemplating |
||||
|
{ |
||||
|
public abstract class AbpTextTemplatingTestBase : AbpIntegratedTest<AbpTextTemplatingTestModule> |
||||
|
{ |
||||
|
protected override void SetAbpApplicationCreationOptions(AbpApplicationCreationOptions options) |
||||
|
{ |
||||
|
options.UseAutofac(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,22 @@ |
|||||
|
using Shouldly; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Volo.Abp.TextTemplating |
||||
|
{ |
||||
|
public class TemplateDefinitionTests : AbpTextTemplatingTestBase |
||||
|
{ |
||||
|
private readonly ITemplateDefinitionManager _templateDefinitionManager; |
||||
|
|
||||
|
public TemplateDefinitionTests() |
||||
|
{ |
||||
|
_templateDefinitionManager = GetRequiredService<ITemplateDefinitionManager>(); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_Retrieve_Template_Definition() |
||||
|
{ |
||||
|
var definition = _templateDefinitionManager.Get(TestTemplates.TestTemplate1); |
||||
|
definition.Name.ShouldBe(TestTemplates.TestTemplate1); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,14 @@ |
|||||
|
namespace Volo.Abp.TextTemplating |
||||
|
{ |
||||
|
public class TestTemplateDefinitionProvider : TemplateDefinitionProvider |
||||
|
{ |
||||
|
public override void Define(ITemplateDefinitionContext context) |
||||
|
{ |
||||
|
context |
||||
|
.Add(new TemplateDefinition( |
||||
|
TestTemplates.TestTemplate1 |
||||
|
) |
||||
|
); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,7 @@ |
|||||
|
namespace Volo.Abp.TextTemplating |
||||
|
{ |
||||
|
public static class TestTemplates |
||||
|
{ |
||||
|
public const string TestTemplate1 = "TestTemplate1"; |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue