Dispownership 0.1.0
See the version list below for details.
dotnet add package Dispownership --version 0.1.0
NuGet\Install-Package Dispownership -Version 0.1.0
<PackageReference Include="Dispownership" Version="0.1.0" />
paket add Dispownership --version 0.1.0
#r "nuget: Dispownership, 0.1.0"
// Install Dispownership as a Cake Addin #addin nuget:?package=Dispownership&version=0.1.0 // Install Dispownership as a Cake Tool #tool nuget:?package=Dispownership&version=0.1.0
Dispownership
Bringing ownership to disposables.
Well, this may be a bit overpromising, but this library does provide useful utilities for working with disposables.
Packages
Examples
These examples use the TempSubdirectory type as a stand-in for a disposable value.
Borrowed or Owned
using Dispownership;
public sealed class Example : IDisposable
{
private readonly Disposable<TempSubdirectory> _tempDirectory;
public Example()
{
// No temp directory provided, let's create one ourselves.
// We have to ensure cleanup ourselves, hence owned.
_tempDirectory = Disposable.Owned(TempSubdirectory.Create());
}
public Example(TempSubdirectory tempDirectory)
{
// Temp directory provided by caller.
// We want the caller to be responsible for disposal, hence "borrowed".
_tempDirectory = Disposable.Borrowed(tempDirectory);
}
public void Dispose()
{
// The wrapper takes care of tracking if disposal should be propagated or not,
// depending on whether the value was wrapped using Owned or Borrowed.
_tempDirectory.Dispose();
}
}
"Move"
using Dispownership;
public TempSubdirectory CreateTempSubdirectoryWithExampleFiles()
{
using var tempDirectory = Disposable.Owned(TempSubdirectory.Create());
// ^^^^^^^^^^^^^^^^
// This creates a wrapper around our disposable, allowing us to later "move" the disposable.
File.WriteAllText(Path.Combine(tempDirectory.Value.FullName, "example.txt"), contents: "Example");
// ^^^^^^^^^^^^
// If this throws, our disposable will be cleaned up by the wrapper.
return tempDirectory.Take();
// ^^^^^^
// This "moves" the disposable out of the wrapper,
// preventing the wrapper from disposing our value when the function returns.
}
Detailed Example
Sometimes you might create a disposable, that you eventually want to return. But before that, you perform some work on the disposable:
public TempSubdirectory CreateTempSubdirectoryWithExampleFiles()
{
var tempDirectory = TempSubdirectory.Create();
File.WriteAllText(Path.Combine(tempDirectory.FullName, "example.txt"), contents: "Example");
return tempDirectory;
}
But wait, what if File.WriteAllText
fails? We still want the temp directory to be cleaned up then.
Adding a using
to var tempDirectory
is no option, because it would dispose the temp directory before the function returns.
The solution is to add a try
/ catch
and dispose in case of errors:
public TempSubdirectory CreateTempSubdirectoryWithExampleFiles()
{
var tempDirectory = TempSubdirectory.Create();
try
{
File.WriteAllText(Path.Combine(tempDirectory.FullName, "example.txt"), contents: "Example");
return tempDirectory;
}
catch
{
tempDirectory.Dispose();
throw;
}
}
This is not as readable compared to the ease of using
declarations.
Using Dispownership
we can rewrite example as follows, preserving behaviour:
using Dispownership;
public TempSubdirectory CreateTempSubdirectoryWithExampleFiles()
{
using var tempDirectory = Disposable.Owned(TempSubdirectory.Create());
// ^^^^^^^^^^^^^^^^
// This creates a wrapper around our disposable, allowing us to later "move" the disposable.
File.WriteAllText(Path.Combine(tempDirectory.Value.FullName, "example.txt"), contents: "Example");
// ^^^^^^^^^^^^
// If this throws, our disposable will be cleaned up by the wrapper.
return tempDirectory.Take();
// ^^^^^^
// This "moves" the disposable out of the wrapper,
// preventing the wrapper from disposing our value when the function returns.
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. 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 is compatible. 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. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- No dependencies.
-
net7.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.