From 2f152f2a3df9d00f37a81699fdb05255954b3076 Mon Sep 17 00:00:00 2001 From: Josh Creek Date: Thu, 2 Sep 2021 21:17:36 +0100 Subject: [PATCH] refactor(*): Use a strongly-typed options object for reading configuration for the SftpRepository --- .../Creek.FileRepository.csproj | 1 + .../Repositories/SftpRepository.cs | 14 +++++++++----- .../RepositoryOptions/SftpRepositoryOptions.cs | 17 +++++++++++++++++ UnitTests/appsettings.json | 10 +++++----- 4 files changed, 32 insertions(+), 10 deletions(-) create mode 100644 Creek.FileRepository/RepositoryOptions/SftpRepositoryOptions.cs diff --git a/Creek.FileRepository/Creek.FileRepository.csproj b/Creek.FileRepository/Creek.FileRepository.csproj index ce1967b..b335fd8 100644 --- a/Creek.FileRepository/Creek.FileRepository.csproj +++ b/Creek.FileRepository/Creek.FileRepository.csproj @@ -25,6 +25,7 @@ + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/Creek.FileRepository/Repositories/SftpRepository.cs b/Creek.FileRepository/Repositories/SftpRepository.cs index ce6ad90..c095d0d 100644 --- a/Creek.FileRepository/Repositories/SftpRepository.cs +++ b/Creek.FileRepository/Repositories/SftpRepository.cs @@ -1,5 +1,6 @@ using Creek.FileRepository.Helpers; using Creek.FileRepository.Models; +using Creek.FileRepository.RepositoryOptions; using Creek.HelpfulExtensions; using Microsoft.Extensions.Configuration; using Renci.SshNet; @@ -28,11 +29,14 @@ namespace Creek.FileRepository.Repositories /// The configuration to initialise the repository with. internal SftpRepository(IConfiguration config) { - this.host = config["SftpRepository:host"]; - this.port = int.Parse(config["SftpRepository:port"]); - this.username = config["SftpRepository:username"].ToString(); - this.password = config["SftpRepository:password"].ToString(); - this.remoteDirectoryPath = config["SftpRepository:remoteDirectoryPath"].ToString(); + SftpRepositoryOptions sftpRepositoryOptions = config.GetSection(SftpRepositoryOptions.SftpRepository) + .Get(); + + this.host = sftpRepositoryOptions.Host; + this.port = sftpRepositoryOptions.Port; + this.username = sftpRepositoryOptions.Username; + this.password = sftpRepositoryOptions.Password; + this.remoteDirectoryPath = sftpRepositoryOptions.RemoteDirectoryPath; } /// diff --git a/Creek.FileRepository/RepositoryOptions/SftpRepositoryOptions.cs b/Creek.FileRepository/RepositoryOptions/SftpRepositoryOptions.cs new file mode 100644 index 0000000..e76fb5a --- /dev/null +++ b/Creek.FileRepository/RepositoryOptions/SftpRepositoryOptions.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Creek.FileRepository.RepositoryOptions +{ + public class SftpRepositoryOptions + { + public const string SftpRepository = "SftpRepository"; + + public string Host { get; set; } + public int Port { get; set; } + public string Username { get; set; } + public string Password { get; set; } + public string RemoteDirectoryPath { get; set; } + } +} diff --git a/UnitTests/appsettings.json b/UnitTests/appsettings.json index d4d96ec..deca7c2 100644 --- a/UnitTests/appsettings.json +++ b/UnitTests/appsettings.json @@ -1,9 +1,9 @@ { "SftpRepository": { - "host": "", - "port": 22, - "username": "", - "password": "", - "remoteDirectoryPath": "/" + "Host": "", + "Port": 22, + "Username": "", + "Password": "", + "RemoteDirectoryPath": "/" } }