Split out similar code to its own code?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Split out similar code to its own code?



I have two blocks of code


private async void AorBButton_Click(object sender, RoutedEventArgs e)

AuthenticationResult authResult = null;
ResultText.Text = string.Empty;

try

authResult = await App.PublicClientApp.AcquireTokenSilentAsync(scopes, App.PublicClientApp.Users.FirstOrDefault());

catch (MsalUiRequiredException ex)

// A MsalUiRequiredException happened on AcquireTokenSilentAsync. This indicates you need to call AcquireTokenAsync to acquire a token
System.Diagnostics.Debug.WriteLine($"MsalUiRequiredException: ex.Message");

try

authResult = await App.PublicClientApp.AcquireTokenAsync(scopes);

catch (MsalException msalex)

ResultText.Text = $"Error Acquiring Token:System.Environment.NewLinemsalex";


catch (Exception ex)

ResultText.Text = $"Error Acquiring Token Silently:System.Environment.NewLineex";
return;


if (authResult != null)

// performs one thing or the other thing




So if I click on the A button, it goes through all of the code until it gets to the final if statement and does one set of instructions.
If I press the B button, it goes through all of the same code until it gets to the final if statement but does a different set of instructions.



I think I should be able to break out the code that is exactly the same in both cases and call it from the click events and run the appropriate resultant code.



Just trying to compact the code as much as I can. I know I can do it in VBA, I just wasn't positive about how to go about it in C#.





Create one method for button A, another method for button B, a third method with all the code that is the same for both, then make sure either button method calls the common method in the right place. If you ned that common method to execute something different for each button, make sure it has a parameter that can take an appropriate method or lambda to jump into.
– dumetrulo
Aug 12 at 19:53




1 Answer
1



Create another async method doing the common stuff and returning the authResult as well as a message that you can then assign to ResultText.Text. C#7's value tuples come in handy for returning the two results.


authResult


ResultText.Text


private async void AorBButton_Click(object sender, RoutedEventArgs e)

var (authResult, message) = await AquireToken();
ResultText.Text = message;

if (authResult != null)
// performs one thing or the other thing



private async Task<(AuthenticationResult authResult, string message)> AquireToken()

AuthenticationResult authResult = null;
string message = String.Empty;

try
authResult = await App.PublicClientApp.AcquireTokenSilentAsync(scopes, App.PublicClientApp.Users.FirstOrDefault());
catch (MsalUiRequiredException ex)
// A MsalUiRequiredException happened on AcquireTokenSilentAsync. This indicates you need to call AcquireTokenAsync to acquire a token
System.Diagnostics.Debug.WriteLine($"MsalUiRequiredException: ex.Message");

try
authResult = await App.PublicClientApp.AcquireTokenAsync(scopes);
catch (MsalException msalex)
message = $"Error Acquiring Token:System.Environment.NewLinemsalex";

catch (Exception ex)
message = $"Error Acquiring Token Silently:System.Environment.NewLineex";

return (authResult, message);



It seems cleaner to me to do all the I/O stuff in the Click handlers and to execute pure logic in the AquireToken method with no dependencies on TextBoxes or Labels (ResultText). This allows you to even extract this logic to its own class.


AquireToken


ResultText





Just had to add NuGet "System.ValueTuple" and it worked great!
– CET
Aug 12 at 20:29






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard