Advertisement
ivandrofly

Avoid duplicated guard / validation

Nov 29th, 2023
884
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.46 KB | None | 0 0
  1. using NavigationMVVM.Models;
  2. using NavigationMVVM.Services;
  3. using NavigationMVVM.Stores;
  4. using NavigationMVVM.ViewModels;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Text;
  8. using System.Windows;
  9.  
  10. namespace NavigationMVVM.Commands
  11. {
  12.     public class LoginCommand : CommandBase
  13.     {
  14.         private readonly LoginViewModel _viewModel;
  15.         private readonly AccountStore _accountStore;
  16.         private readonly INavigationService _navigationService;
  17.  
  18.         public LoginCommand(LoginViewModel viewModel, AccountStore accountStore, INavigationService navigationService)
  19.         {
  20.             _viewModel = viewModel;
  21.             _accountStore = accountStore;
  22.             _navigationService = navigationService;
  23.         }
  24.  
  25.         public override void Execute(object parameter)
  26.         {
  27.             // Account account = new Account()
  28.             // {
  29.             //     Email = $"{_viewModel.Username}@test.com",
  30.             //     Username = _viewModel.Username
  31.             // };
  32.             //
  33.             //
  34.             // _accountStore.CurrentAccount = account;
  35.             //
  36.             // _navigationService.Navigate();
  37.  
  38.  
  39.             CreateFluentExecute(() =>
  40.             {
  41.                 Account account = new Account()
  42.                 {
  43.                     Email = $"{_viewModel.Username}@test.com",
  44.                     Username = _viewModel.Username
  45.                 };
  46.  
  47.                 _accountStore.CurrentAccount = account;
  48.                 _navigationService.Navigate();
  49.             }).LogOnException().WhenParameterHasValue(parameter);
  50.  
  51.             // var execute = new LogOnException(() => { })
  52.             //     .LogOnException()
  53.             //     .WhenParameterHasValue(parameter);
  54.         }
  55.  
  56.         private static IGuard CreateFluentExecute(Action action) => new AlwaysExecute(action);
  57.     }
  58.  
  59.     public static class CommandExtensions
  60.     {
  61.         public static IGuard WhenParameterHasValue(this IGuard guard, object parameter)
  62.         {
  63.             return new ParamNotNull(parameter, guard);
  64.         }
  65.        
  66.         public static IGuard LogOnException(this IGuard guard) => LogOnException(guard);
  67.     }
  68.  
  69.     public class AlwaysExecute : IGuard
  70.     {
  71.         private readonly Action _action;
  72.  
  73.         public AlwaysExecute(Action action)
  74.         {
  75.             _action = action;
  76.         }
  77.  
  78.         public void Execute() => _action();
  79.     }
  80.    
  81.     public class ParamNotNull : IGuard
  82.     {
  83.         private readonly object _parameter;
  84.         private readonly IGuard _other;
  85.  
  86.         public ParamNotNull(object parameter, IGuard other)
  87.         {
  88.             _parameter = parameter;
  89.             _other = other;
  90.         }
  91.  
  92.         public void Execute()
  93.         {
  94.             if (_parameter is not null)
  95.             {
  96.                 _other.Execute();
  97.             }
  98.         }
  99.  
  100.     }
  101.  
  102.     public class LogOnException : IGuard
  103.     {
  104.         private readonly Action _action;
  105.  
  106.         public LogOnException(Action action)
  107.         {
  108.             _action = action;
  109.         }
  110.  
  111.         public void Execute()
  112.         {
  113.             try
  114.             {
  115.                 _action.Invoke();
  116.             }
  117.             catch (Exception e)
  118.             {
  119.                 Console.WriteLine(e);
  120.             }
  121.         }
  122.  
  123.         public void Execute(object parameter)
  124.         {
  125.         }
  126.     }
  127.  
  128.     public interface IGuard
  129.     {
  130.         void Execute();
  131.     }
  132. }
  133.  
  134.  
  135. Author: https://github.com/ivandrofly
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement