refactor(*): Use a strongly-typed options object for reading configuration for the SftpRepository

This commit is contained in:
Josh Creek
2021-09-02 21:17:36 +01:00
parent 73be5a52f9
commit 2f152f2a3d
4 changed files with 32 additions and 10 deletions
@@ -25,6 +25,7 @@
<ItemGroup>
<PackageReference Include="Creek.HelpfulExtensions" Version="1.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="5.0.0" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
@@ -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
/// <param name="config">The configuration to initialise the repository with.</param>
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<SftpRepositoryOptions>();
this.host = sftpRepositoryOptions.Host;
this.port = sftpRepositoryOptions.Port;
this.username = sftpRepositoryOptions.Username;
this.password = sftpRepositoryOptions.Password;
this.remoteDirectoryPath = sftpRepositoryOptions.RemoteDirectoryPath;
}
/// <summary>
@@ -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; }
}
}
+5 -5
View File
@@ -1,9 +1,9 @@
{
"SftpRepository": {
"host": "",
"port": 22,
"username": "",
"password": "",
"remoteDirectoryPath": "/"
"Host": "",
"Port": 22,
"Username": "",
"Password": "",
"RemoteDirectoryPath": "/"
}
}