Skip to content
This repository has been archived by the owner on Nov 7, 2018. It is now read-only.

Add possibility to configure options validation in order to validate options at the start up time (not at runtime). #171

Closed
wants to merge 21 commits into from

Conversation

MaKCbIMKo
Copy link

This PR introduces the possibility to 'initialize' options at the start up time. It will prevent possible config issues that might occur in runtime because of incorrect config file.

Also, it allows to use initializers to write some custom logic to instantiate options objects.

Added possibility to configure options with initializers in order to initialize options at the start up time (not at runtime).
@dnfclas
Copy link

dnfclas commented Feb 20, 2017

Hi @MaKCbIMKo, I'm your friendly neighborhood .NET Foundation Pull Request Bot (You can call me DNFBOT). Thanks for your contribution!

In order for us to evaluate and accept your PR, we ask that you sign a contribution license agreement. It's all electronic and will take just minutes. I promise there's no faxing. https://cla2.dotnetfoundation.org.

TTYL, DNFBOT;

@dnfclas
Copy link

dnfclas commented Feb 20, 2017

@MaKCbIMKo, Thanks for signing the contribution license agreement so quickly! Actual humans will now validate the agreement and then evaluate the PR.

Thanks, DNFBOT;

@MaKCbIMKo MaKCbIMKo changed the title Add possibility to configure options with initializers in order to initialize options at the start up time (not at runtime). Add possibility to configure options with validations in order to validate options at the start up time (not at runtime). Mar 7, 2017
@MaKCbIMKo MaKCbIMKo changed the title Add possibility to configure options with validations in order to validate options at the start up time (not at runtime). Add possibility to configure options validation in order to validate options at the start up time (not at runtime). Mar 7, 2017
return services;
}

public static IServiceCollection Validate<TOptions>(this IServiceCollection services, Expression<Func<TOptions, bool>> validateExpression)
Copy link
Member

Choose a reason for hiding this comment

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

What do you think about a less restrictive signature for the validation? If we call it Validation but treat it effectively as a second set of actions that run after the Configure step. This would let you write Validations that both Assert/SetDefaults. What you have today I think would be good as something built on top, sugar that makes it easy for you to register an expression that returns a bool.

Something like Validate(Action<TOptions> options) as well.

What do you think?

Copy link
Author

Choose a reason for hiding this comment

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

So, you suggest adding a few overloads for Validate method with a lightweight signature, correct?

I was thinking about using Action<TOptions> as a parameter. But it would require to use exceptions to signal that options are invalid. As far as I remember, using bool is faster than exceptions. Moreover, if will expect exception we will have to wrap into try/catch block each separated validation call (in other case validation will be stopped after first validation failed).

I was trying to use Func<TOptions, bool> at first, but I found Expression more informative, because it provides information that might help identify the problem (and might be logged).

Copy link
Member

Choose a reason for hiding this comment

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

Yeah I was thinking we layer things on. So at the lowest layer there's a Validate that just takes an Action, then we can build additional overloads that take expression/func and return bools, that way there's still the flexibility to drop down and do something different if they don't want to follow the more constrained func/expression/bool route.

We wouldn't necessarily have to do anything to support wrapping exceptions either this way, since that's the lowest level API

Copy link
Author

Choose a reason for hiding this comment

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

I've got your point. I will add the lowest layer overloads.

Copy link
Member

Choose a reason for hiding this comment

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

I'm thinking maybe the higher level overload should be Func<TOptions, IEnumerable<Exception>> they can return multiple validation exceptions this way so its slightly more powerful than a bool

@HaoK
Copy link
Member

HaoK commented Mar 7, 2017

Take a look at #161 which is tracking some features which would be useful for options to provide for the Auth 2.0 work, named options will add a bit of a wrinkle since there'd need to be the ability to target All instances vs named instances, vs the default instance. We haven't decided that we are bringing back named options yet, but its something to consider

remove redundant rows.
OptionsCache class replaced with OptionsFactory.
OptionsFactory is an additional injection point that might be used.
Add more flexibility of configuration and validation result handling.
@MaKCbIMKo
Copy link
Author

@HaoK, I've modified (a lot) of validation stuff. Moreover, I took a look on feature (about multi-tenant) and found that it might require some validation 'on creation' time. I also added that (wasn't not a big deal).

About multi-tenancy: it all depends on how we will configure all these 'names', where we will store cached instances (for different names), but I think it won't be so difficult to modify current validation to work in a new way.

But, only if you have some roadblocks that you still not sure (or don't know) how to break...

@HaoK
Copy link
Member

HaoK commented Mar 14, 2017

Thanks, I'll try to take a look this week

@HaoK
Copy link
Member

HaoK commented Mar 16, 2017

So I'm going to be starting a PR for #161, it will be a pretty lightweight feature to start, I'll submit it by EOD tomorrow, so you can provide input and insight since you've been playing around with validation for a while. The initial critical feature is named options support, so the first iteration of the PR will likely have very minimal validation support (think Action<TOption>).

@MaKCbIMKo
Copy link
Author

Ok, waiting for your PR.

@HaoK
Copy link
Member

HaoK commented Mar 16, 2017

Take a look at #176

@MaKCbIMKo
Copy link
Author

@HaoK - I reviewed #176 and left a comment.

What I found is that your PR might reduce the amount of changes and be some kind of 'base' for this PR. (see the comments for #176).

@MaKCbIMKo
Copy link
Author

@HaoK - Is it fine to base this PR over your initial Options 2.0 changes? Or there more changes are comming?

Maybe something in my PR need to be reconsidered...

@HaoK
Copy link
Member

HaoK commented Mar 29, 2017

Feel free to rebase if you want, there likely will be more changes coming, but it won't be for a few weeks

Add unit tests for validation.
# Conflicts:
#	src/Microsoft.Extensions.Options/OptionsCache.cs
#	src/Microsoft.Extensions.Options/OptionsManager.cs
#	src/Microsoft.Extensions.Options/OptionsMonitor.cs
#	src/Microsoft.Extensions.Options/OptionsServiceCollectionExtensions.cs
#	test/Microsoft.Extensions.Options.Test/Microsoft.Extensions.Options.Test.csproj
#	test/Microsoft.Extensions.Options.Test/OptionsSnapshotTest.cs
@MaKCbIMKo
Copy link
Author

@HaoK - I have added a possibility to validate all named options. Please take a look.

p.s. there is some problems with Travis CI check (Error: Unable to locate libunwind. Install libunwind to continue) - not sure how to solve that weather the problem with my changes.

# Conflicts:
#	src/Microsoft.Extensions.Options.ConfigurationExtensions/ConfigurationChangeTokenSource.cs
#	src/Microsoft.Extensions.Options/LegacyOptionsCache.cs
#	src/Microsoft.Extensions.Options/OptionsServiceCollectionExtensions.cs
@MaKCbIMKo
Copy link
Author

Changes from dev branch has been merged. @HaoK - may I ask you to review the PR?

@MaKCbIMKo
Copy link
Author

About 'AppVeyor build failed' - seems like it faced the exception during downloading "https://dotnetcli.azureedge.net/dotnet/Sdk/2.0.0-preview3-006785/dotnet-sdk-2.0.0-preview3-006785-win-x64.zip" (I managed to download it locally). then it switched to alt download link "https://dotnetcli.azureedge.net/dotnet/Sdk/2.0.0-preview3-006785/dotnet-dev-win-x64.2.0.0-preview3-006785.zip" - which seems to be not working.

@HaoK - maybe we just need to try to re-run the AppVeyor build?

@MaKCbIMKo
Copy link
Author

@HaoK - did you have a chance to take a look at this PR?

@HaoK
Copy link
Member

HaoK commented Aug 25, 2017

I took a look, but we haven't signed up to do this work yet, so drilling into the details/implementation is a bit premature until we actually are trying to get this feature implemented.

@MaKCbIMKo
Copy link
Author

Do you mean that this feature is not in roadmap?

Can we do anything in order to sign this up?

@HaoK
Copy link
Member

HaoK commented Aug 25, 2017

Yes, feedback/comments in #227 saying why you want/need this feature will help get it prioritized higher/sooner.

@aspnet-hello
Copy link

This issue was moved to dotnet/aspnetcore#2392

@aspnet aspnet locked and limited conversation to collaborators Jan 1, 2018
@aspnet-hello
Copy link

Sorry I was a bad bot. Re-opening this PR.

@aspnet-hello aspnet-hello reopened this Jan 2, 2018
@aspnet aspnet unlocked this conversation Jan 2, 2018
@natemcmaster natemcmaster changed the base branch from dev to master July 2, 2018 17:58
@HaoK
Copy link
Member

HaoK commented Jul 31, 2018

See #266 for initial validation support

@HaoK HaoK closed this Jul 31, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants