Porto 1.0.1
See the version list below for details.
dotnet add package Porto --version 1.0.1
NuGet\Install-Package Porto -Version 1.0.1
<PackageReference Include="Porto" Version="1.0.1" />
paket add Porto --version 1.0.1
#r "nuget: Porto, 1.0.1"
// Install Porto as a Cake Addin
#addin nuget:?package=Porto&version=1.0.1
// Install Porto as a Cake Tool
#tool nuget:?package=Porto&version=1.0.1
Porto
The Porto library is a collection of utility, extension, attribute, and miscellaneous classes to assist your development. For too long I've been copying these classes between projects, so it was about time to put them in a single library that could be pulled down from Nuget. The library includes:
- General utilities: certificate loading, IP address and DNS extraction;
- Wrappers with interfaces to common .NET classes to improve unit testing, e.g. File, Directory...
- EnvironmentVariableAttribute and parser for setting object properties from the environment.
Installation
Porto can be installed via Nuget using the Visual Studio package manager (search for 'Porto') or by running the command below:
dotnet add package Porto
Usage
Environment Variable Attribute
Add the attribute to any properties that you'd like to set using Environment Variables. Then call the EnvironmentVariableParser.Parse<T>() method to the properties from the Environment.
using Porto.Attributes;
public class MySettings
{
// When no Presence is specified as the second argument then it defaults to Mandatory.
[EnvironmentVariable("CONNECTION_STRING")]
public string ConnectionString { get; set; }
// An optional variable will not throw an exception if it's not set in the environment.
[EnvironmentVariable("SOME_PASSWORD", Presence.Optional)]
public string Password { get; set; }
}
// Creates a new instance of MySettings, processing any EnvironmentVariable attributes along the way.
var settings = EnvironmentVariableParser.Parse<MySettings>();
Wrappers
If you've created a class that accesses the disk (for example), the only way to unit test is to interface this disk access. Porto wraps these classes with an interface so that DI can be used and unit tests can be written. A noddy example is shown below.
using Porto.System.IO;
// This is the class we want to test
public class TestSubject
{
private readonly IPFile _fileAccess;
public TestSubject(IPFile fileAccess)
{
_fileAccess = filesAccess
}
public void TestMethod(string path)
{
if (!_fileAccess.Exists(path))
throw new Exception();
// do something useful
}
}
// ... and this is the NUnit test class
[TestFixture]
public class TestClass
{
[Test]
public void Test()
{
// Arrange
var mockFile = new Mock<IPFile>();
mockFile.Setuo(x => x.Exists(It.IsAny<string>())).Throws<Exception>();
var testSubject = new TestSubject(mockFile.Object);
// Act / Assert
Assert.Throws<Exception>(() => testSubject.TestMethod("what?"));
}
}
License
The Porto library is distributed under the MIT license, the details of which can be found here: MIT
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net6.0
- Microsoft.AspNetCore.Http.Abstractions (>= 2.2.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.