【发布时间】:2018-11-12 19:50:00
【问题描述】:
我正在尝试在 C# 中创建一个通用(通用)pipeline,以便在许多项目中重复使用。这个想法与ASP.NET Core Middleware 非常相似。它更像是一个可以动态组合的巨大函数(双向管道)(类似于BRE)
它需要获取一个输入模型,管理一系列之前加载的处理器,并在输入旁边返回一个封装在超模型中的输出模型。
这就是我所做的。我创建了一个Context 类,代表整体数据/模型:
public class Context<InputType, OutputType> where InputType : class, new() where OutputType : class, new()
{
public Context()
{
UniqueToken = new Guid();
Logs = new List<string>();
}
public InputType Input { get; set; }
public OutputType Output { get; set; }
public Guid UniqueToken { get; }
public DateTime ProcessStartedAt { get; set; }
public DateTime ProcessEndedAt { get; set; }
public long ProcessTimeInMilliseconds
{
get
{
return (long)ProcessEndedAt.Subtract(ProcessStartedAt).TotalMilliseconds;
}
}
public List<string> Logs { get; set; }
}
然后我创建了一个接口,在真实处理器上强制签名:
public interface IProcessor
{
void Process<InputType, OutputType>(Context<InputType, OutputType> context, IProcessor next) where InputType : class, new() where OutputType : class, new();
}
然后我创建了一个Container,来管理整个管道:
public class Container<InputType, OutputType> where InputType : class, new() where OutputType : class, new()
{
public static List<IProcessor> Processors { get; set; }
public static void Initialize()
{
LoadProcessors();
}
private static void LoadProcessors()
{
// loading processors from assemblies dynamically
}
public static Context<InputType, OutputType> Execute(InputType input)
{
if (Processors.Count == 0)
{
throw new FrameworkException("No processor is found to be executed");
}
if (input.IsNull())
{
throw new BusinessException($"{nameof(InputType)} is not provided for processing pipeline");
}
var message = new Context<InputType, OutputType>();
message.Input = input;
message.ProcessStartedAt = DateTime.Now;
Processors[0].Process(message, Processors[1]);
message.ProcessEndedAt = DateTime.Now;
return message;
}
}
我知道如何从给定文件夹中的程序集中动态加载处理器,所以这不是问题。但我被困在这些点上:
【问题讨论】:
-
请编辑问题以将其限制为具有足够详细信息的特定问题,以确定适当的答案。避免一次问多个不同的问题。请参阅How to Ask 页面以获得澄清此问题的帮助。
-
这是一个有趣的问题,我必须以非常相似的方式为插件和链接创建一个架构。但是,我不确定任何人都能理解您的意思,或者您真正想要实现的目标......没有模式,没有库,这只是设计架构,您是架构师.此外,任何建议都只是一种意见。我实际上认为您有能力自己解决这个问题并最了解您的担忧
-
我认为容器负责流动。处理器要么在列表中排序要么每个处理器实例都应该用包含流的信息(如下一个属性)包装并存储在将由容器执行的列表中
-
也许对 tpl 数据流感兴趣? (example)