Florian Salihovic
1 min readJul 28, 2020

--

You are just applying the decorator pattern? That idea is everything but new. I am not a C# guy but the collection initializer is something pointed out directly on https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/collections

I personnally don't think it is necessary to decorate a collection. Usually the service layer should take care of these things (for example validating data), as collections just represent an intermediate state of data in applications (request/response|IO, in memory or in the persistence layer).

It might even be counter productive as this pattern introduces more classes which need to be tested in addition to the service layer, which needs extra validation anyway. The way you implemented the example and making the assumption, that there are no other guards "protecting" the applications state, you would not even know that invalid data is flowing arround in the choas surrounding the collection and the collection discards the invalid values silently.

If, and only if, there is a common understanding in the team, that these kind collection extensions are necessary, a generic approach should be chosen and not a concrete decorator. These classes should be provided as NuGet package, so that the classes can and will be used by all devs in a company so that there is a common understanding and economic value.

--

--

Florian Salihovic

Entrepreneur · Machine learning and software engineering for bread and butter