[API Proposal]: Create static StreamFactory
Background and motivation
While watching the .NET API review stream VODs for #82801 (create static Stream.FromX methods), I had an idea that has - to my knowledge - not been considered yet.
This proposal does not clash with the approved resolution of #82801 (add new stream types with dedicated constructors); instead, I believe this proposal could provide the benefits without the downsides of adding Stream.FromX as originally proposed.
To re-iterate, here are some arguments that were mentioned while discussing the original issue #82801:
Stream.FromXoffers unparalleled discoverability, which would be nice to have- extension methods
AsStream()orToStream()were discussed, but- these are less discoverable
- introducing an extension method on fundamental types like
stringwas controversial
- static methods can be called from derived classes
- ugly at best (
NetworkStream.FromReadOnlyDatanot actually having anything to do withNetworkStream) - confusing or misleading at worst (
FileStream.FromText(@"C:\\myfile.txt")vsFile.OpenText(@"C:\\myfile.txt")) - this would be somewhat mitigated by the analyzer suggesting to simplify
NetworkStream.FromReadOnlyDatatoStream.FromReadOnlyData - a dedicated analyzer or an attribute marking static methods as "must be called with declaring class" are possible but extra work
- ugly at best (
Proposal:
Similar to how we have a TaskFactory, you could introduce a static class StreamFactory.
Benefits:
- That class can have as many "FromXyz" methods as you want
- Perfect discoverability by having all methods available from the same IntelliSense completion
StreamFactory.| - No cluttering of any existing type's API
- Each method can return the optimal stream type, and that would not be confusing, it would be my expectation (as long as all derive from
Stream. - Since the Stream do not inherit from
StreamFactory, method calls via subtypes is not possible and will not be done.
API Proposal
namespace System.IO;
{
public static class StreamFactory
{
public static StringStream FromText(string text, Encoding? encoding = null);
public static StringStream FromText(ReadOnlyMemory<char> text, Encoding? encoding = null);
public static ReadOnlyMemoryStream FromReadOnlyData(ReadOnlyMemory<byte> data);
public static WritableMemoryStream FromWritableData(Memory<byte> data);
// ...
}
}
API Usage
using var s = StreamFactory.FromReadOnlyData(buffer); // s is ReadOnlyMemoryStream
Alternative Designs
See #82801
Risks
No response
Tagging subscribers to this area: @dotnet/area-system-io See info in area-owners.md if you want to be subscribed.
Thanks for the suggestion! This was actually already discussed during the API review process for #82801.
The StreamFactory shape was the one initially presented in the first API review, but the review board sent it back for rework due to the non-extensibility concern: Third parties with their own types can't make their stream-creation methods look consistent with StreamFactory.From*.
@ViveliDuCh Please read my proposal before assuming it's a duplicate. I made this proposal precisely because the other one proposed these methods to be introduced in the Stream class, which was not approved because doing that downsides that the review team was not happy with. Therefore the other proposal has now been turned into an alternative which does NOT introduce the static methods as proposed. So this issue is not a duplicate.
@LWChris The shape you have proposed here was discussed on that same issue in 2023, see the top post edit history, Mar 23, 2023.
We rejected it then in potential favor of extension methods on Stream. Then, this year, we rejected that in favor of exposing the types directly.
In general (though there are exceptions), we don't like having two ways of doing the exact same thing in the BCL.
@LWChris The shape you have proposed here was discussed on that same issue in 2023, see the top post edit history, Mar 23, 2023.
I see, as alternative 3 (though return type was not explicit). It is funny how a factory class was rejected due to lack in discoverability in favor of Stream extensions, which was then rejected for layering and inheritance issues in favor of explicit types, which have the lowest discoverability of all alternatives.
Personally, I don't think a factory is a second way, it's an architectural element to absolve users from knowing about dozens of specialized strategies to create objects. But I guess it is something one can provide as NuGet package and if that gets a lot of traction and becomes essentially a must-have for any bigger project, it might be worth investigating again.
Thanks for the explanation.