Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move the validaiton logic to FileStreamOptions #53869

Merged
merged 4 commits into from
Jun 9, 2021

Conversation

adamsitnik
Copy link
Member

Addresses #52928 (comment)

@adamsitnik adamsitnik added this to the 6.0.0 milestone Jun 8, 2021
@ghost
Copy link

ghost commented Jun 8, 2021

Tagging subscribers to this area: @carlossanlop
See info in area-owners.md if you want to be subscribed.

Issue Details

Addresses #52928 (comment)

Author: adamsitnik
Assignees: -
Labels:

area-System.IO

Milestone: 6.0.0

{
throw new ArgumentException(SR.Format(SR.Argument_InvalidFileModeAndAccessCombo, value, _access), nameof(value));
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. We typically consider validation that crosses properties like this to be problematic. I hadn't noticed the interactions when I suggested moving the validation logic into the setters. @bartonjs, opinions?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(A happy medium might be leaving in FileStreamOptions the validation that depends only on the value being set, and put back in FileStream the rest, e.g. here we'd keep the if (value < FileMode.CreateNew || value > FileMode.Append) check but remove the else-ifs. The validation for Share/Options/PreallocationSize/BufferSize is all fine as it's based only on the value being set.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The answer may no longer be needed, but since the question was asked:

The guidelines are that property sets shouldn't interfere with each other, but that any validation wait until a method depends on the state of those properties.

So the first if "FileMode makes no sense" makes sense here, but the remainder shouldn't happen until something wants to consume one.

If you want to keep the validation succinct, and in this type, you could add an internal Validate method, that way the same validation is shared across all consumers.

internal static void ArgumentOutOfRangeException_Enum_Value()
{
throw new ArgumentOutOfRangeException("value", SR.ArgumentOutOfRange_Enum);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this necessary? Looks like there's only a few call sites, none of which are on a hot path or in a generic context.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my goal was to avoid copying code

Copy link
Member

@stephentoub stephentoub left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks.

Co-authored-by: Stephen Toub <stoub@microsoft.com>
@adamsitnik adamsitnik merged commit 78579ef into dotnet:main Jun 9, 2021
@adamsitnik adamsitnik deleted the fileStreamOptionsImprovements branch June 9, 2021 13:49
@ghost ghost locked as resolved and limited conversation to collaborators Jul 9, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants