-
Notifications
You must be signed in to change notification settings - Fork 5
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
Be open to extension #143
Comments
I agree, though there may be places were it's recommended to privatize stuff. Maybe this is a bit subjective. Useful places that could be extended, For sure! |
I agree with @stijnmoreels : you don't want to change all private's to protected virtual by default. Same is true for public members: you don't want to change them all to virtual by default. public void DoSomething( Foo bar )
{
if( bar == null )
{
throw new ArgumentNullException(nameof(bar));
}
DoSomethingCore(bar);
}
protected virtual void DoSomething(Foo bar)
{
...
} see also this article for design guidelines on using Do we have specific cases where we're lacking extensibility ? .edit: Just a thought, but you could provide extensibility-points in certain methods, for instance: public async Task<Secret> GetSecretAsync( string name )
{
await OnBeforeGetSecretAsync(name);
// Retrieve secret from secretstore ...
...
await OnAfterGetSecretAsync(name, retrievedSecret);
}
protected virtual Task OnBeforeGetSecretAsync(string name) {}
protected virtual Task OnAfterGetSecretAsync(string name, Secret secret) {} (ps: should this be a discussion ? ) |
It's not a discussion because it's a must :D Today this is a pain. I agree that we shouldn't do it for all privates, that was not really my intend indeed. However, I would personally keep it simple and make the protected methods virtual itself rather than different sub pieces since you can still manage that through base.MethodName, no? Otherwise it becomes too complex imo. |
My intention was rather: should it be a 'discussion' so that we can agree on how to do it. :) When looking at the public async Task StoreSecretAsync( ... )
{
await OnBeforeStoreSecretAsync(...);
await _secretProvider.StoreSecretAsync( ... );
MemoryCache.Set(... );
await OnAfterStoreSecretAsync(...);
}
protected virtual Task OnBeforeStoreSecretAsync() {} Instead of providing protected virtual methods as extension points, you could also implement this via events. However, that might be a bit of a pain when working with async code. Anyhow, I think it's to blunt to just say that all protected methods must be virtual. Take a look at the
Edit: Although I like the public override StoreSecretAsync( .... )
{
// Do something before storing the secret
Foo();
await base.StoreSecretAsync( ... );
// Do something after the secret has been stored
Bar();
} But ... this construct allows a consumer to completely change the implementation of this method as well. And that's why I'm actually not a fan of |
The The |
As stated by @fgheysels here arcus-azure/arcus.security#282 (comment) : we may not be ready to know what the consumers want. On the other hand, if we continue the approach we're using, they are free to override the public methods and set their own limitations instead of us trying to restrict something we can't predict. |
We provide a lot of great stuff, but when you want to extend or tweak something to your needs it becomes harder because we are not always open to extension.
We should ensure that we allow people to derive and extend things by using
protected virtual
instead ofprivate
, for example, or usepublic virtual
.The text was updated successfully, but these errors were encountered: