试图构建一个简单的管道和过滤器

时间:2019-03-24 05:39:47

标签: c# design-patterns task-parallel-library

我正在尝试实现在其中集成TPL数据流的管道和过滤器模式。我遇到的问题不是我所有的结果都被搞清楚。例如,我将99999个项目放入管道中,只有85238个出来。

EmployeeModel.cs

public class EmployeeModel
{

    public String FirstName { get; set; }

    public String LastName { get; set; }

    public String FullName { get; set; }

    public override String ToString()
    {
        return $"FirstName: {FirstName}\nLastName: {LastName}\nFullName: {FullName}\n";
    }
}

IFilter.cs

public interface IFilter<T>
{
    T Execute(T input);
}

AbstractParallelFilter.cs

public abstract class AbstractParallelFilter<T> : IFilter<T>
{
    public AbstractParallelFilter()
    {
        TransformBlock = new TransformBlock<T, T>(new Func<T, T>(Execute), new ExecutionDataflowBlockOptions()
        {
             BoundedCapacity = DataflowBlockOptions.Unbounded,
             MaxDegreeOfParallelism = Environment.ProcessorCount

        });
    }


    public abstract T Execute(T input);

    internal TransformBlock<T, T> TransformBlock { get; private set; }
}

IParallelPipeline.cs

public interface IParallelPipeline<T>
{

    IParallelPipeline<T> Register(AbstractParallelFilter<T> filter);

    IParallelPipeline<T> CompleteRegisteration();

    IParallelPipeline<T> Process(T input);

    Task CompleteProcessing();


    ConcurrentBag<T> Results { get; set; }
}

AbstractParallelPipeline.cs

public abstract class AbstractParallelPipeline<T>: IParallelPipeline<T>
{

    public AbstractParallelPipeline()
    {
        filters = new List<AbstractParallelFilter<T>>();

        Results = new ConcurrentBag<T>();
    }

    public IParallelPipeline<T> Register(AbstractParallelFilter<T> filter)
    {
        filters.Add(filter);
        return this;
    }

    public abstract IParallelPipeline<T> Process(T input);

    public Task CompleteProcessing()
    {
        if (filters.Count == 0)
            throw new Exception("No filters have been registered");

        filters.First().TransformBlock.Complete();

        return filters.Last().TransformBlock.Completion;
    }

    public IParallelPipeline<T> CompleteRegisteration()
    {
        if (filters.Count < 2)
        {
            return this;
        }
        else
        {
            for (int i = filters.Count - 2; i >= 0; i--)
            {
                filters[i].TransformBlock.LinkTo(filters[i + 1].TransformBlock, new DataflowLinkOptions() { PropagateCompletion = true });
            }

            ActionBlock<T> dumpBlock = new ActionBlock<T>(x => Results.Add(x));
            filters.Last().TransformBlock.LinkTo(dumpBlock, new DataflowLinkOptions() { PropagateCompletion = true });
        }

        return this;
    }

    public IList<AbstractParallelFilter<T>> filters;

    public ConcurrentBag<T> Results { get; set; }
}

ParallelPipeline.cs

public class ParallelPipeline<T> : AbstractParallelPipeline<T>
{
    public override IParallelPipeline<T> Process(T input)
    {
        filters.First().TransformBlock.Post(input);
        return this;
    }
}

Program.cs

class Program
{
    static void Main(string[] args)
    {
        List<EmployeeModel> employeeModels = new List<EmployeeModel>();
        int count = 99999;

        for (int i = 0; i < count; i++)
        {
            EmployeeModel employee = new EmployeeModel()
            {
                FirstName = NameGenerator.GenerateFirstName(Gender.Female),
                LastName = NameGenerator.GenerateLastName()
            };
            employeeModels.Add(employee);
        }

        IParallelPipeline<EmployeeModel> parallelPipeline = new ParallelPipeline<EmployeeModel>()
            .Register(new ParallelFirstNameToUpperFilter())
            .Register(new ParallelLastNameToUpperFilter())
            .Register(new ParallelFullNameConcatFilter())
            .CompleteRegisteration();

        for (int i = 0; i < count; i++)
        {
            parallelPipeline.Process(employeeModels[i]);
        }
        parallelPipeline
            .CompleteProcessing()
            .Wait();


        Console.WriteLine(parallelPipeline.Results.Count);

        Console.Read();
    }
}

1 个答案:

答案 0 :(得分:1)

好吧,发现了新手错误,在CompleteProcessing()期间,我需要返回ActionBlock而不是TransformBlock,因为ActionBlock是最后一个块。