StaticAbstraction 0.1.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package StaticAbstraction --version 0.1.0
NuGet\Install-Package StaticAbstraction -Version 0.1.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="StaticAbstraction" Version="0.1.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add StaticAbstraction --version 0.1.0
#r "nuget: StaticAbstraction, 0.1.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install StaticAbstraction as a Cake Addin
#addin nuget:?package=StaticAbstraction&version=0.1.0

// Install StaticAbstraction as a Cake Tool
#tool nuget:?package=StaticAbstraction&version=0.1.0

StaticAbstraction

What

A light-weight library that wraps the static methods on common objects in the System.IO and System.Reflection namespaces. It also wrappers static methods on System.DateTime and System.Console

The methods and properties on all of the wrapper objects are, in nearly all cases, direct wrappers of the static functionality. The only place where more complex work is done is where the wrapped method returns an array of wrapped objects.

Why

This is intended to alleviate developers from having to figure out and code ways around testing objects that use those static methods. Since most of those solutions involve creating custom code just like this, I figured maybe it should already exist in a reusable form.

This is meant solely to wrapper commonly used objects that provide untestable static methods so that they can be tested. All objects are direct matches to their static-bound brethren, use the same paramters, have the same overloads and so forth. Each method/property is meant to be as lightweight as possible with the least amount of extra code.

How

In order to use this, you will have to change places in your existing code so that it uses the classes and interfaces defined here instead of the static methods provided by the framework, either as direct object creations or as objects created via Dependency Injection.

i.e.. instead of doing

    File.Delete("somefilename");

you would do:

    _file.Delete("somefilename");

where _file was declared as an IFile and either created in a constructor directly or injected via dependency injection (DI).

This allows unit tests to properly mock out the .Delete() command so that it never attempts actual disk IO

These objects will also wrapper annoying to mock results from their methods (i.e. FileInfo, DirectoryInfo, Assembly) so that both the method and the results can be easily tested

Additionally, most of the objects that are wrapped by this code also have a new extension method ToStaticAbstraction() that will create an instance of the abstraction class.

Interfaces and classes

each of the common interfaces used in this module have a single implementor in the event that you need to instantiate them directly

interface class Wraps
IStaticAbstraction StAbWrapper N/A - allows access to all abstractions
IFile StAbFile System.IO.File
IPath StAbPath System.IO.Path
IDirectory StAbDirectory System.IO.Directory
IDriveInfo StAbDriveInfo System.IO.DriveInfo
IConsole StAbConsole System.Console
IDateTime StAbDateTime System.DateTime
IAssembly StAbAssembly System.Reflection.Assembly

Mocks

There are implementations of the common interfaces below. These are meant to be building blocks to be used when stubbing out method results in unit tests. In addition to implementing the interface, they also offer:

  • setters for all properties so that values can be mocked out as required.
  • all properties and methods are virtual so they can easily be overridden in a subclass if needed
  • methods/properties either do nothing or return the default for whatever type is normally returned (i.e. NULL for objects, false for bool, etc)
Interface Mock
IDirectoryInfo MockDirectoryInfo
IFileInfo MockFileInfo
IFileSystemInfo MockDFileSystemInfo
IDriveInfoDetails MockDriveInfoDetails
IAssemblyInstance MockAssemblyInstance
using StaticAbstraction.IO.Mocks;
using StaticAbstraction.Reflection.Mocks

The interfaces themselves can easily be stubbed out via NSubstitute or similar but these offer the convenience of simple object construction.

Supported .Net versions

At present, the library builds to support the following .Net versions.

  • .Net Core 2.2
  • .Net Standard 2.0
  • .Net Framework 4.8
  • .Net Framework 4.7
  • .Net Framework 4.6
  • .Net Framework 4.5

Notes

  • I have attempted to make the wrapper classes as lightweight as possible since some of this code will be in the actually final application but certain methods are going to be heavier. Most notably are those that need to convert large quantities of the wrapped object (i.e. FileInfo) into same sized arrays of wrapper objects (IFileInfo)

Still TODO

  • override basic object methods such as ToString and Equals() (more investigation needed)
  • Implement ISerializable

Suggestions

This is still in the very early stages so if you use the library and find something else that would be handy, please log an issue with the details

Product 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 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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 is compatible.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net40 is compatible.  net403 was computed.  net45 is compatible.  net451 was computed.  net452 was computed.  net46 is compatible.  net461 was computed.  net462 was computed.  net463 was computed.  net47 is compatible.  net471 was computed.  net472 was computed.  net48 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETCoreApp 2.2

    • No dependencies.
  • .NETFramework 4.0

    • No dependencies.
  • .NETFramework 4.5

    • No dependencies.
  • .NETFramework 4.6

    • No dependencies.
  • .NETFramework 4.7

    • No dependencies.
  • .NETFramework 4.8

    • No dependencies.
  • .NETStandard 2.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.

Version Downloads Last updated
1.3.1.1 572 3/18/2020
1.3.1 483 2/4/2020
0.1.0 516 7/14/2019