Compare commits

...

8 Commits

10 changed files with 167 additions and 34 deletions

View File

@@ -47,7 +47,11 @@ try
?? throw new InvalidOperationException(
"The 'MediatRLicense' configuration value is missing or empty." +
"Please ensure it is properly set in the configuration source.");
builder.Services.AddWorkFlowServices(opt => opt.MediatRLicense = mediatRLicense).AddWorkFlowRepositories();
builder.Services.AddWorkFlowServices(opt =>
{
opt.MediatRLicense = mediatRLicense;
opt.ConfigMapping(config.GetSection("MappingOptions"));
}).AddWorkFlowRepositories();
builder.Services.AddUserManager<WFDBContext>();

View File

@@ -0,0 +1,17 @@
{
"MappingOptions": {
"TfFileUri": {
"Scheme": "https",
"Host": "dd-gan.digitaldata.works",
"Port": 8443,
"Path": "api/file"
},
"TfFileIconUri": {
"Scheme": "https",
"Host": " dd-gan.digitaldata.works",
"Port": 8443,
"Path": "api/file",
"Query": "icon=true"
}
}
}

View File

@@ -1,4 +1,5 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using WorkFlow.Application.Mapping;
namespace WorkFlow.Application;
@@ -7,26 +8,45 @@ public static class DependencyInjection
{
public static IServiceCollection AddWorkFlowServices(this IServiceCollection services, Action<WorkFlowServiceOptions>? options = null)
{
WorkFlowServiceOptions diOptions = new();
options?.Invoke(diOptions);
WorkFlowServiceOptions sOptions = new(services);
options?.Invoke(sOptions);
services.AddAutoMapper(typeof(MappingProfile).Assembly);
services.AddMediatR(cfg =>
{
cfg.RegisterServicesFromAssembly(typeof(DependencyInjection).Assembly);
cfg.LicenseKey = diOptions.MediatRLicense;
cfg.LicenseKey = sOptions.MediatRLicense;
});
services.AddSingleton(diOptions.UriBuilderFactory());
services.AddTransient<UriBuilderResolver>();
if(!sOptions.IsMappingConfigured)
services.Configure<MappingOptions>(_ => { });
services.AddTransient<TfFileUriResolver>();
services.AddTransient<TfFileIconUriResolver>();
return services;
}
public class WorkFlowServiceOptions
{
public string MediatRLicense { get; set; } = string.Empty;
private readonly IServiceCollection _services;
public Func<UriBuilder> UriBuilderFactory { get; set; } = () => new UriBuilder();
internal bool IsMappingConfigured { get; private set; } = false;
public WorkFlowServiceOptions(IServiceCollection services) => _services = services;
private void EnsureSingleMappingConfiguration(Action action)
{
if (IsMappingConfigured)
throw new InvalidOperationException("Mapping configuration has already been set.");
action();
IsMappingConfigured = true;
}
public void ConfigMapping(IConfiguration config) => EnsureSingleMappingConfiguration(() => _services.Configure<MappingOptions>(config));
public void ConfigMapping(Action<MappingOptions> options) => EnsureSingleMappingConfiguration(() => _services.Configure(options));
public string? MediatRLicense { get; set; }
}
}

View File

@@ -2,7 +2,7 @@
public class TfFileDto
{
public UriBuilder Url { get; set; } = null!;
public string? Url { get; set; }
public string? Headline { get; set; }
@@ -10,5 +10,5 @@ public class TfFileDto
public string? Comment { get; set; }
public string? Icon { get; set; }
public string? IconUrl { get; set; }
}

View File

@@ -0,0 +1,52 @@
namespace WorkFlow.Application.Mapping;
public class MappingOptions
{
public UriBuilderOptions TfFileUri { get; set; } = new();
public UriBuilderOptions TfFileIconUri { get; set; } = new();
public class UriBuilderOptions
{
public string? Scheme { get; set; }
public string? Host { get; set; }
public int? Port { get; set; }
private string _path = "/";
public string Path
{
get => _path;
set => _path = value.Trim('/');
}
private string _query = string.Empty;
public string Query
{
get => _query;
set => _query = value.TrimStart('?');
}
public UriBuilder ToBuilder
{
get
{
var uriBuilder = new UriBuilder()
{
Scheme = Scheme ?? "http",
Host = Host ?? "localhost",
Path = Path,
Query = Query,
};
if (Port is int port)
uriBuilder.Port = port;
return uriBuilder;
}
}
}
}

View File

@@ -29,6 +29,7 @@ public class MappingProfile : AutoMapper.Profile
.ForMember(dest => dest.Others, opt => opt.MapFrom(src => new string?[] { src.State2, src.State3, src.State4 }));
CreateMap<TfFile, TfFileDto>()
.ForMember(dest => dest.Url, opt => opt.MapFrom<UriBuilderResolver>());
.ForMember(dest => dest.Url, opt => opt.MapFrom<TfFileUriResolver>())
.ForMember(dest => dest.IconUrl, opt => opt.MapFrom<TfFileIconUriResolver>());
}
}

View File

@@ -0,0 +1,24 @@
using AutoMapper;
using Microsoft.Extensions.Options;
using System.Web;
using WorkFlow.Application.Dto;
using WorkFlow.Domain.Entities;
namespace WorkFlow.Application.Mapping;
public class TfFileIconUriResolver : IValueResolver<TfFile, TfFileDto, string?>
{
private readonly Func<UriBuilder> _uriBuilderFactory;
public TfFileIconUriResolver(IOptions<MappingOptions> options) => _uriBuilderFactory = () => options.Value.TfFileIconUri.ToBuilder;
public string? Resolve(TfFile source, TfFileDto destination, string? destMember, ResolutionContext context)
{
if(string.IsNullOrEmpty(source.Icon))
return null;
var builder = _uriBuilderFactory();
builder.AppendPath(source.Icon);
return builder.ToString();
}
}

View File

@@ -0,0 +1,23 @@
using AutoMapper;
using Microsoft.Extensions.Options;
using WorkFlow.Application.Dto;
using WorkFlow.Domain.Entities;
namespace WorkFlow.Application.Mapping;
public class TfFileUriResolver : IValueResolver<TfFile, TfFileDto, string?>
{
private readonly Func<UriBuilder> _uriBuilderFactory;
public TfFileUriResolver(IOptions<MappingOptions> options) => _uriBuilderFactory = () => options.Value.TfFileUri.ToBuilder;
public string? Resolve(TfFile source, TfFileDto destination, string? destMember, ResolutionContext context)
{
if (string.IsNullOrEmpty(source.Path))
return null;
var builder = _uriBuilderFactory();
builder.AppendPath(source.Path);
return builder.ToString();
}
}

View File

@@ -0,0 +1,14 @@
using System.Web;
namespace WorkFlow.Application.Mapping;
internal static class UriBuilderExtensions
{
public static void AppendPath(this UriBuilder uriBuilder, string path, bool encode = true)
{
if(encode)
path = HttpUtility.UrlEncode(path);
uriBuilder.Path = $"{uriBuilder.Path.TrimEnd('/', '\\')}/{path.Trim('/', '\\')}";
}
}

View File

@@ -1,22 +0,0 @@
using AutoMapper;
using System.Web;
using WorkFlow.Application.Dto;
using WorkFlow.Domain.Entities;
namespace WorkFlow.Application.Mapping;
public class UriBuilderResolver : IValueResolver<TfFile, TfFileDto, UriBuilder>
{
private readonly UriBuilder _uriBuilder;
public UriBuilderResolver(UriBuilder uriBuilder) => _uriBuilder = uriBuilder;
public UriBuilder Resolve(TfFile source, TfFileDto destination, UriBuilder destMember, ResolutionContext context)
{
var builder = new UriBuilder(_uriBuilder.Uri)
{
Path = HttpUtility.UrlEncode(source.Path)
};
return builder;
}
}