public static ICommandHandler <TCommand> Default <TCommand>(object handler, IServiceProvider provider) { var logger = provider.GetService <ILogger>(); var validations = provider.GetServices <IValidator <TCommand> >(); var transactionDecorator = new TransactionDecorator <TCommand>((ICommandHandler <TCommand>)handler); var loggingDecorator = new LoggingDecorator <TCommand>(transactionDecorator, logger); var validationDecorator = new ValidationDecorator <TCommand>(loggingDecorator, validations); return(validationDecorator); // order is revers for decorators }
public static ICommandHandler <TCommand> DefaultWithRetry <TCommand>(object handler, IServiceProvider provider) { var logger = provider.GetService <ILogger>(); var cache = provider.GetService <ICache>(); var validations = provider.GetServices <IValidator <TCommand> >(); var transactionDecorator = new TransactionDecorator <TCommand>((ICommandHandler <TCommand>)handler); var loggingDecorator = new LoggingDecorator <TCommand>(transactionDecorator, logger); var validationDecorator = new ValidationDecorator <TCommand>(loggingDecorator, validations); var retryDecorator = new RetryDecorator <TCommand>(validationDecorator, logger, nrRetry: 3); var cacheDecorator = new CacheDecorator <TCommand>(retryDecorator, cache); return(cacheDecorator); // order is revers for decorators }
public void Init() { _mockGroupRepository = new Mock <IGroupRepository>(); _mockGroupRepository .Setup(repo => repo.AddAsync(It.IsAny <Group>(), null)) .ReturnsAsync(It.IsAny <Group>()); DefaultAddGroupCommandHandler addGroupCommandHandler = new DefaultAddGroupCommandHandler(_mockGroupRepository.Object); ValidationDecorator <AddGroupCommand> validationDecorator = new ValidationDecorator <AddGroupCommand>(addGroupCommandHandler, new DefaultAddGroupCommandValidator()); _authoriserDecorator = new AuthoriserDecorator <AddGroupCommand>(validationDecorator, new CompanyAddGroupAuthoriser()); _suspendGroupHandler = new SuspendGroupCommandHandler(_mockGroupRepository.Object); }
static void Main(string[] args) { var container = ConfigureContainer(); _bankAccount = container.Resolve <BankAccount>(); _logger = container.Resolve <LoggingDecorator>(); _validator = container.Resolve <ValidationDecorator>(); Console.WriteLine("Enter action followed by amount e.g. \"withdraw 2000\". Enter \"logs\" to get all logs. Enter \"exit\" to exit the program."); var input = Console.ReadLine(); while (input != "exit") { try { if (input.Contains("withdraw ")) { ProcessWithdraw(input); } else if (input.Contains("deposit ")) { ProcessDeposit(input); } else if (input.Contains("balance")) { GetBalance(); } else if (input == "logs") { Console.WriteLine("Here are all the logs:"); Console.WriteLine(_logger.GetAllLogs()); } else { Console.WriteLine("Invalid input."); } } catch (Exception ex) { _logger.Log(ex.Message); } finally { input = Console.ReadLine(); } } }