Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Discussion options

Summary

This proposal suggests modifying the AddFile method signature in the DiscUtils project to accept a Func<Stream> instead of a direct Stream. This change aims to optimize resource management and the method's flexibility, especially in scenarios where the Build method finalizes data writing.

Detail

Currently, the AddFile method accepts a Stream parameter directly, implying that the data stream must be available and open at the call time, regardless of when the writing actually occurs (via the Build method). This approach can lead to resource management issues, such as streams being prematurely closed or locked due to their use in other processes before the execution of Build.

The proposal is to change the AddFile method signature to accept a Func<Stream>. This would allow:

  • Postponing the creation and opening of the Stream until just before its use, ensuring its availability and optimal condition.
  • Improving the management of the Stream lifecycle, aligning with reactive and functional programming principles.
  • Offering a more robust and flexible solution for file manipulation in complex scenarios.

Previous Usage Example

stream = new FileStream(path, FileMode.Open);
someFsBuilder.AddFile("example/path", stream);
// The stream needs to be kept open and managed externally until Build

Proposed Usage Example

someFsBuilder.AddFile("example/path", () => new FileStream(path, FileMode.Open));
// The stream is created and opened internally at the optimal moment, simplifying its management

Advantages

  • Resource Management: Avoids the need to keep streams unnecessarily open or to manage their lifecycle externally.
  • Flexibility: Provides greater flexibility in file management, especially in applications that work with a large amount of data or that require fine control over file manipulation.
  • Robustness: Reduces the risk of runtime errors related to stream handling, such as attempting to write to an already closed stream.

I appreciate your consideration of this proposal and are open to discussing any related aspects. I believe this improvement can significantly add value to DiscUtils project.

Thanks!

You must be logged in to vote

Replies: 1 comment

Comment options

This is a really good suggestion. I realize that some implementations of IFileSystemBuilder already have an overload like that to AddFile, for example VirtualFileSystem:
https://github.com/LTRData/DiscUtils/blob/16702f7e1aafc82f65d34a498a1b7ce34abcfbe7/Library/DiscUtils.VirtualFileSystem/VirtualFileSystem.cs#L303

I could pull a definition of that up to the interface too. Then there are quite a few implementations to work through, but that could be done over a longer period of time because we could keep the old overload for existing implementations too.

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
💡
Ideas
Labels
None yet
2 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.