Coder Social home page Coder Social logo

pawelgerr / thinktecture.abstractions Goto Github PK

View Code? Open in Web Editor NEW
30.0 4.0 8.0 1.47 MB

Interfaces for commonly used .NET types like `File`, `Directory`, `Stream`, `Path`, `Math`, `Environment`, `Task`, etc. for better testability.

License: BSD 3-Clause "New" or "Revised" License

C# 99.94% PowerShell 0.06%
dotnet-core dotnet abstractions testability

thinktecture.abstractions's Introduction

Build Thinktecture.Abstractions

Interfaces for commonly used .NET types like File, Directory, Stream, Path, Math, Environment, etc. to be able to extend or to change the standard behavior and for better testability.

  • The interfaces have the same API like the .NET types
  • Support of .NET Standard 2.1, .NET Core 3.1 and .NET 5.0

Read my blog ".NET Abstractions - It's not just about testing!" for more info about the design decisions I made.

Usage

Just use the interface like IFile instead of File.

More examples:

public class MyClass
{
	private readonly IFile _file;
	
	public MyClass(IFile file)
	{
		_file = file;
	}

	public void MyMethod(string filePath)
	{
		...
		bool exists = _file.Exists(filePath);
		...
	}
}

Usage of static types

The projects containing the interfaces provide adapters (wrappers) that are using the static .NET classes internally.

IFile file = new FileAdapter();

Usage of non-static types

Either use the adapters like with static types or use the extension methods in namespace Thinktecture.

FileInfo fileInfo = ...;

IFileInfo fileInfoInterface = fileInfo.ToInterface();

If you need back the .NET type i.e. to call a method that does not support the abstrations just call ToImplementation()

IFileInfo fileInfoInterface = ...

FileInfo fileInfo = fileInfoInterface.ToImplementation();

Resharper: if you are using Resharper then you will see another member UnsafeConvert(). This member is for implementers of the interface only and it is not intended to be used directly.

Projects

The projects are mirroring the .NET assemblies, i.e. if the class Stream is in System.IO.dll then the interface IStream will be in Thinktecture.IO.Abstractions.dll.

Thinktecture.Net.Sockets.Abstractions

Provides interfaces for types in System.Net.Sockets: Socket, TcpClient, UdpClient, TcpListener, NetworkStream, ISocketAsyncEventArgs, LingerOption, SendPacketsElement.

Nuget: Install-Package Thinktecture.Net.Sockets.Abstractions

Thinktecture.Net.NetworkInformation.Abstractions

Provides interfaces for types in System.Net.NetworkInformation: NetworkInterface, PhysicalAddress, IPAddressInformation, IPAddressInformationCollection, IPInterfaceProperties, IPInterfaceStatistics, IPv4InterfaceProperties, IPv6InterfaceProperties, UnicastIPAddressInformation, UnicastIPAddressInformationCollection, MulticastIPAddressInformation, MulticastIPAddressInformationCollection, GatewayIPAddressInformation, GatewayIPAddressInformationCollection.

Nuget: Install-Package Thinktecture.Net.NetworkInformation.Abstractions

Thinktecture.Net.Http.Abstractions

Provides interfaces for types in System.Net.Http: HttpClient, HttpContent, HttpRequestMessage, HttpResponseMessage, HttpHeaders, HttpHeaderValueCollection, DelegatingHandler, HttpClientHandler, HttpMessageHandler, HttpMessageInvoker, MessageProcessingHandler, MultipartContent, MultipartFormDataContent, HttpContentHeaders, HttpRequestHeaders, HttpResponseHeaders.

Nuget: Install-Package Thinktecture.Net.Http.Abstractions

Thinktecture.Net.Primitives.Abstractions

Provides interfaces for types in System.Net.Primitives: IPAddress, IPEndPoint, NetworkCredential, EndPoint, Cookie, DnsEndPoint, SocketAddress, CookieCollection, CookieContainer, CredentialCache, TransportContext, ChannelBinding.

Nuget: Install-Package Thinktecture.Net.Primitives.Abstractions

Thinktecture.IO.FileSystem.Watcher.Abstractions

Provides interfaces for types in System.IO.FileSystem.Watcher: FileSystemWatcher.

Nuget: Install-Package Thinktecture.IO.FileSystem.Watcher.Abstractions

Thinktecture.IO.FileSystem.Abstractions

Provides interfaces for types in System.IO.FileSystem: Directory, File, FileSystemInfo, DirectoryInfo, FileInfo, FileStream, SafeFileHandle.

Nuget: Install-Package Thinktecture.IO.FileSystem.Abstractions

Thinktecture.IO.Abstractions

Provides interfaces for types in System.IO: Stream, MemoryStream, BinaryReader, BinaryWriter, StreamReader, StreamWriter, StringReader, StringWriter, TextReader, TextWriter

Nuget: Install-Package Thinktecture.IO.Abstractions

Thinktecture.Text.Encoding.Abstractions

Provides interfaces for types in System.Text.Encoding: Encoding, Encoder, Decoder

Nuget: Install-Package Thinktecture.Text.Encoding.Abstractions

Thinktecture.Runtime.Extensions.Abstractions

Provides interfaces for type in System.Runtime.Extensions: Path, BitConverter, Convert, Environment, Math, Random, UriBuilder, Stopwatch, WebUtility

Nuget: Install-Package Thinktecture.Runtime.Extensions.Abstractions

Thinktecture.Runtime.Handles.Abstractions

Provides interfaces for types in System.Runtime.Handles: SafeHandle, SafeWaitHandle

Nuget: Install-Package Thinktecture.Runtime.Handles.Abstractions

Thinktecture.Runtime.Abstractions

Provides interfaces for types in System.Runtime: StringBuilder, EventArgs, statics of Guid (like Guid.NewGuid()), statics of DateTime (like DateTime.Now).

Nuget: Install-Package Thinktecture.Runtime.Abstractions

thinktecture.abstractions's People

Contributors

jasonwoods-7 avatar joedm09 avatar jtone123 avatar pawelgerr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

thinktecture.abstractions's Issues

Socket async methods?

The Socket async methods in modern System.Net.Sockets like BeginAccept, BeginConnect, BeginSend and so on would be nice to be in the interface and adapter in this library.

I was thinking about using the library via NuGet package to avoid having to write my own wrapper like this, but this became a blocker real quick. From the other issue, I assume this library is still not likely to be updated soon. However, if one were to fork to add these and make a PR, would it be likely to make its way back here and get into a fresh NuGet package? (Probably a longshot and my be easier all around if I just write my own for the one class I want to wrap, but figured to ask anyway.)

Cant Mock HttpHeaderValueCollection in IHttpRequestHeaders

When unit of work sets accept headers with a call like _httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); the DefaultRequestHeaders.Accept is null and cannot be mocked since HttpHeaderValueCollection<T> is a sealed class which does not inherit from an interface (excluding enumables like ICollection) and does not expose a public constructor.

As soon as I get time I'll try to add some abstractions and put together a PR, but might take a few days.

.NET 5 support

Hey guys, we tried upgrading our application to .NET 5 and we're getting the following error so I wonder when you're planning to release the version that's going to support .NET 5 or maybe there's some workaround in the meantime that we can use to make it work:

Unable to activate type 'Thinktecture.Net.Http.Adapters.HttpClientAdapter'. The following constructors are ambiguous:
Void .ctor(System.Net.Http.HttpMessageHandler)
Void .ctor(System.Net.Http.HttpClient))

UnsafeConvert

As a feedback: awesome work, but what is the purpose of having UnsafeConvert in the interface?

If I am implementing my own, say, ITextWriter, I have to implement this method twice, but it doesn't make any sense, what could I possibly return here? There may be some class I'm wrapping, but it won't be a 100% match to ITextWriter, so why bother getting the wrapped item?

Plus, the default ReSharper's implementation doesn't always readily work, and the [[EditorBrowsable(EditorBrowsableState.Never)] feels hackish.

Why not:

  1. Leave this method in AbstractionAdapter<TImplementation>.
  2. Make it internal.
  3. Implement the ToImplementation extensions on IAbstraction<T> abstraction as (abstraction as Abstraction<T>)?.UnsafeConvert()?

This way there won't be this garbage in custom implementations, and the method will be truly hidden.

Thanks.

Thread Deadlocks HttpClient Awaiter

I'm not sure the exact fail scenario however when I call a dependency from a WebService the awaiter for the HttpClientAdapter tasks such as PostAsync never return.

This issue can be solved when directly interacting with the native System.Net.HttpClient by calling .ConfigureAwait(false) however the awaiter cannot be configured for within abstraction and therefore the native HttpClient which is being wrapped is never able to return a result and hangs forever.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.