ActionBlock or TransformBlock when just modifying TInput and not creating a different TOutput?

StackOverflow https://stackoverflow.com/questions/23497613

문제

When daisy chaining multiple blocks that modify the same TInput elements, I can either use ActionBlock<TInput> or TransformBlock<TInput,TInput>, the difference being that the Transform's delegate would return the same (but modified element) while the Action delegate wouldn't return anything, it would just modify the input element.

Both work, and I suppose both make some sense semantically, but I'm assuming only one of those is the intended way.

If I had to guess, I'd say it's Action because when returning the same elements, it's not really a transformation. But if I had to guess for more pragmatic reasons, I'd say Transform, because if I'm daisy chaining these blocks Transform seems to allow me to post a "processed" element straight to the next block, not sure how I could enforce that with Action.

도움이 되었습니까?

해결책

If you want to modify the object in your block and then work with it in another block, then the only correct option is to use a TransformBlock. I'd say that it is a transformation: the resulting object has the same identity as the original, but it was modified (or “transformed”) in the block.

ActionBlock is most commonly used as the end of a pipeline, its final step. That's not what you want here, so ActionBlock is not appropriate.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top