Skip to content

Commit

Permalink
Added abstractions for System.Net.NetworkInformation.
Browse files Browse the repository at this point in the history
  • Loading branch information
PawelGerr committed Dec 13, 2016
1 parent cbc2500 commit 33cb25d
Show file tree
Hide file tree
Showing 59 changed files with 2,173 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ Provides interfaces for types in `System.Net.Sockets`: `Socket`, `TcpClient`, `U

**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`, `DelegatingHandler`, `HttpClientHandler`, `HttpMessageHandler`, `HttpMessageInvoker`, `MessageProcessingHandler`, `MultipartContent`, `MultipartFormDataContent`, `HttpContentHeaders`, `HttpRequestHeaders`, `HttpResponseHeaders`.

Expand Down
7 changes: 7 additions & 0 deletions Thinktecture.Abstractions.sln
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Thinktecture.Net.Primitives
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Thinktecture.Net.Sockets.Abstractions", "src\Thinktecture.Net.Sockets.Abstractions\Thinktecture.Net.Sockets.Abstractions.xproj", "{A7907887-601D-4B1D-9484-099F39C0FCEB}"
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Thinktecture.Net.NetworkInformation.Abstractions", "src\Thinktecture.Net.NetworkInformation.Abstractions\Thinktecture.Net.NetworkInformation.Abstractions.xproj", "{F927011A-CED0-4E14-82B5-789BEB1A044F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -105,6 +107,10 @@ Global
{A7907887-601D-4B1D-9484-099F39C0FCEB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A7907887-601D-4B1D-9484-099F39C0FCEB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A7907887-601D-4B1D-9484-099F39C0FCEB}.Release|Any CPU.Build.0 = Release|Any CPU
{F927011A-CED0-4E14-82B5-789BEB1A044F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F927011A-CED0-4E14-82B5-789BEB1A044F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F927011A-CED0-4E14-82B5-789BEB1A044F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F927011A-CED0-4E14-82B5-789BEB1A044F}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -123,5 +129,6 @@ Global
{35DA5ACA-3A2D-4792-85BB-B61EEC6CA0A2} = {4860E453-E56B-4BED-BA8A-DDFA89A19B0D}
{751EE0A0-51FF-4D8E-86D8-F223904E814F} = {4860E453-E56B-4BED-BA8A-DDFA89A19B0D}
{A7907887-601D-4B1D-9484-099F39C0FCEB} = {4860E453-E56B-4BED-BA8A-DDFA89A19B0D}
{F927011A-CED0-4E14-82B5-789BEB1A044F} = {4860E453-E56B-4BED-BA8A-DDFA89A19B0D}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;

namespace Thinktecture.Collections.Generic
{
/// <summary>
/// Adapter for collections.
/// </summary>
/// <typeparam name="TAbstractionItem">Item type of the abstraction.</typeparam>
/// <typeparam name="TImplementationItem">Item type of the implementation.</typeparam>
/// <typeparam name="TImplementation">Type of the implementation.</typeparam>
public class CollectionAbstractionAdapter<TAbstractionItem, TImplementationItem, TImplementation> : AbstractionAdapter, ICollectionAbstraction<TAbstractionItem, TImplementationItem, TImplementation>
where TAbstractionItem : IAbstraction<TImplementationItem>
where TImplementation : ICollection<TImplementationItem>
{
/// <summary>
/// Inner collection.
/// </summary>
protected readonly TImplementation Collection;

private readonly Func<TImplementationItem, TAbstractionItem> _toInterface;

/// <inheritdoc />
public int Count => Collection.Count;

/// <inheritdoc />
public bool IsReadOnly => Collection.IsReadOnly;

/// <summary>
/// Initializes new instance of <see cref="CollectionAbstractionAdapter{TAbstractionItem,TImplementationItem,TImplementation}"/>.
/// </summary>
/// <param name="collection">Collection to be used by the adapter.</param>
/// <param name="toInterface">Converts an item of <typeparamref name="TImplementation"/> to type <typeparamref name="TAbstractionItem"/>.</param>
public CollectionAbstractionAdapter(TImplementation collection, Func<TImplementationItem, TAbstractionItem> toInterface)
: base(collection)
{
if (collection == null)
throw new ArgumentNullException(nameof(collection));
if (toInterface == null)
throw new ArgumentNullException(nameof(toInterface));
Collection = collection;
_toInterface = toInterface;
}

/// <inheritdoc />
[EditorBrowsable(EditorBrowsableState.Never)]
public new TImplementation UnsafeConvert()
{
return Collection;
}

/// <inheritdoc />
public IEnumerator<TAbstractionItem> GetEnumerator()
{
foreach (var item in Collection)
{
yield return _toInterface(item);
}
}

/// <inheritdoc />
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}

/// <inheritdoc />
public void Add(TAbstractionItem item)
{
Collection.Add(item.ToImplementation());
}

/// <inheritdoc />
public void Add(TImplementationItem item)
{
Collection.Add(item);
}

/// <inheritdoc />
public void Clear()
{
Collection.Clear();
}

/// <inheritdoc />
public bool Contains(TAbstractionItem item)
{
return Collection.Contains(item.ToImplementation());
}

/// <inheritdoc />
public bool Contains(TImplementationItem item)
{
return Collection.Contains(item);
}

/// <inheritdoc />
public void CopyTo(TAbstractionItem[] array, int arrayIndex)
{
Collection.CopyTo(array.ToImplementation<TAbstractionItem, TImplementationItem>(), arrayIndex);
}

/// <inheritdoc />
public void CopyTo(TImplementationItem[] array, int arrayIndex)
{
Collection.CopyTo(array, arrayIndex);
}

/// <inheritdoc />
public bool Remove(TAbstractionItem item)
{
return Collection.Remove(item.ToImplementation());
}

/// <inheritdoc />
public bool Remove(TImplementationItem item)
{
return Collection.Remove(item);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;

namespace Thinktecture.Collections.Generic
{
/// <summary>
/// An abstraction for collections.
/// </summary>
/// <typeparam name="TAbstractionItem">Type of the abstraction.</typeparam>
/// <typeparam name="TImplementationItem">Tzpe of the item of the implementation.</typeparam>
/// <typeparam name="TImplementation">Type of the implementation.</typeparam>
public interface ICollectionAbstraction<TAbstractionItem, in TImplementationItem, out TImplementation> : IAbstraction<TImplementation>, ICollection<TAbstractionItem>
where TAbstractionItem : IAbstraction<TImplementationItem>
where TImplementation : ICollection<TImplementationItem>
{
/// <summary>
/// Gets inner instance of <typeparamref name="TImplementation"/>.
/// It is not intended to be used directly. Use <see cref="CollectionExtensions.ToImplementation{TAbstractionItem,TImplementationItem,TImplementation}"/> instead.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
new TImplementation UnsafeConvert();

/// <summary>
/// Adds item to collection.
/// </summary>
/// <param name="item">Item to add.</param>
void Add(TImplementationItem item);

/// <summary>
/// Gets an indicator whether the item is in collection or not.
/// </summary>
/// <param name="item">Item to check for.</param>
/// <returns><c>true</c> if the item is in collection; <c>false</c> otherwise.</returns>
bool Contains(TImplementationItem item);

/// <summary>
/// Copies the collection to provided array.
/// </summary>
/// <param name="array">Array to copy into.</param>
/// <param name="arrayIndex">Index to start to insert from.</param>
void CopyTo(TImplementationItem[] array, int arrayIndex);

/// <summary>
/// Removes the item from collection.
/// </summary>
/// <param name="item">Item to remove.</param>
/// <returns><c>true</c> if the item has been removed; <c>false</c> otherwise.</returns>
bool Remove(TImplementationItem item);
}
}
18 changes: 18 additions & 0 deletions src/Thinktecture.Abstractions/Extensions/AbstractionExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace Thinktecture
{
/// <summary>
/// Extensions for <see cref="IAbstraction"/>.
/// </summary>
public static class AbstractionExtensions
{
/// <summary>
/// Converts provided abstraction to implementation.
/// </summary>
/// <param name="abstraction">Abstraction to convert.</param>
/// <returns>Converted abstraction.</returns>
public static TImplementation ToImplementation<TImplementation>(this IAbstraction<TImplementation> abstraction)
{
return abstraction != null ? abstraction.UnsafeConvert() : default(TImplementation);
}
}
}
26 changes: 26 additions & 0 deletions src/Thinktecture.Abstractions/Extensions/CollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Collections.Generic;
using Thinktecture.Collections.Generic;

namespace Thinktecture
{
/// <summary>
/// Extension methods for <see cref="ICollection{T}"/>
/// </summary>
public static class CollectionExtensions
{
/// <summary>
/// Converts a collection of type <typeparamref name="TAbstractionItem"/> to <typeparamref name="TImplementation"/>.
/// </summary>
/// <param name="collection">Collection to convert</param>
/// <typeparam name="TAbstractionItem">Type of the abstraction.</typeparam>
/// <typeparam name="TImplementationItem">Type of the item of the implementation.</typeparam>
/// <typeparam name="TImplementation">Type of the implementation.</typeparam>
/// <returns>Converted colletion.</returns>
public static TImplementation ToImplementation<TAbstractionItem, TImplementationItem, TImplementation>(this ICollectionAbstraction<TAbstractionItem, TImplementationItem, TImplementation> collection)
where TAbstractionItem : IAbstraction<TImplementationItem>
where TImplementation : ICollection<TImplementationItem>
{
return collection == null ? default(TImplementation) : collection.UnsafeConvert();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=extensions/@EntryIndexedValue">True</s:Boolean>
</wpf:ResourceDictionary>
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#if NETSTANDARD1_3 || NET45 || NET46

using System.Net.NetworkInformation;
using Thinktecture.Net.NetworkInformation;

namespace Thinktecture
{
/// <summary>
/// Extensions for arrays.
/// </summary>
public static class ArrayExtensions
{
/// <summary>
/// Converts an array of <see cref="NetworkInterface"/> to an array of <see cref="INetworkInterface"/>.
/// </summary>
/// <param name="elements">Array to convert.</param>
/// <returns>An array of <see cref="INetworkInterface"/>.</returns>
public static INetworkInterface[] ToInterface(this NetworkInterface[] elements)
{
if (elements == null)
return null;

var abstractions = new INetworkInterface[elements.Length];

for (var i = 0; i < elements.Length; i++)
{
abstractions[i] = elements[i].ToInterface();
}

return abstractions;
}
}
}

#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#if NETSTANDARD1_3 || NET45 || NET46

using System.Net.NetworkInformation;
using Thinktecture.Net.NetworkInformation;
using Thinktecture.Net.NetworkInformation.Adapters;

namespace Thinktecture
{
/// <summary>
/// Extensions for <see cref="GatewayIPAddressInformationCollection"/>.
/// </summary>
// ReSharper disable once InconsistentNaming
public static class GatewayIPAddressInformationCollectionExtensions
{
/// <summary>
/// Converts provided collection to <see cref="IGatewayIPAddressInformationCollection"/>.
/// </summary>
/// <param name="collection">Collection to convert.</param>
/// <returns>Converted collection.</returns>
public static IGatewayIPAddressInformationCollection ToInterface(this GatewayIPAddressInformationCollection collection)
{
return (collection == null) ? null : new GatewayIPAddressInformationCollectionAdapter(collection);
}
}
}

#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#if NETSTANDARD1_3 || NET45 || NET46

using System.Net.NetworkInformation;
using Thinktecture.Net.NetworkInformation;
using Thinktecture.Net.NetworkInformation.Adapters;

namespace Thinktecture
{
/// <summary>
/// Extensions for <see cref="GatewayIPAddressInformation"/>.
/// </summary>
// ReSharper disable once InconsistentNaming
public static class GatewayIPAddressInformationExtensions
{
/// <summary>
/// Converts provided info to <see cref="IGatewayIPAddressInformation"/>.
/// </summary>
/// <param name="info">Info to convert.</param>
/// <returns>Converted info.</returns>
public static IGatewayIPAddressInformation ToInterface(this GatewayIPAddressInformation info)
{
return (info == null) ? null : new GatewayIPAddressInformationAdapter(info);
}
}
}

#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#if NETSTANDARD1_3 || NET45 || NET46

using System.Net.NetworkInformation;
using Thinktecture.Net.NetworkInformation;
using Thinktecture.Net.NetworkInformation.Adapters;

namespace Thinktecture
{
/// <summary>
/// Extensions for <see cref="IPAddressInformationCollection"/>.
/// </summary>
// ReSharper disable once InconsistentNaming
public static class IPAddressInformationCollectionExtensions
{
/// <summary>
/// Converts provided collection to <see cref="IIPAddressInformationCollection"/>.
/// </summary>
/// <param name="collection">Collection to convert.</param>
/// <returns>Converted collection.</returns>
public static IIPAddressInformationCollection ToInterface(this IPAddressInformationCollection collection)
{
return (collection == null) ? null : new IPAddressInformationCollectionAdapter(collection);
}
}
}

#endif
Loading

0 comments on commit 33cb25d

Please sign in to comment.