EgaSoftware.Adapter
1.0.2
dotnet add package EgaSoftware.Adapter --version 1.0.2
NuGet\Install-Package EgaSoftware.Adapter -Version 1.0.2
<PackageReference Include="EgaSoftware.Adapter" Version="1.0.2" />
<PackageVersion Include="EgaSoftware.Adapter" Version="1.0.2" />
<PackageReference Include="EgaSoftware.Adapter" />
paket add EgaSoftware.Adapter --version 1.0.2
#r "nuget: EgaSoftware.Adapter, 1.0.2"
#:package EgaSoftware.Adapter@1.0.2
#addin nuget:?package=EgaSoftware.Adapter&version=1.0.2
#tool nuget:?package=EgaSoftware.Adapter&version=1.0.2
Read-only interface adapter
This repository contains a library which allows you to adapt any object to a POCO read-only interface.
Installation
Add EgaSoftware.Adapter NuGet package to your project:
dotnet add package EgaSoftware.Adapter
Usage
Here is an example of generation and usage of an adapter.
using EgaSoftware.Adapter;
interface IInterface
{
int Amount { get; }
}
public class Adapted
{
public int Amount { get; set; }
}
public void AdaptExample()
{
var adapterFactory = AdapterFactoryBuilder.For<IInterface, Adapted>().Build();
var adaptedObject = new Adapted() { Amount = 12 };
var adapter = adapterFactory.Adapt(adaptedObject);
System.Console.WriteLine(adapter.Amount); // 12
}
Mappings
You are able to map interface properties using any logic.
using EgaSoftware.Adapter;
interface IInterface
{
string Title { get; }
}
public class Adapted
{
public string GetTitle() => "Read-only interface adapter";
}
public void AdaptExample()
{
var adapterFactory = AdapterFactoryBuilder.For<IInterface, Adapted>()
.Map(i => i.Title, obj => obj.GetTitle())
.Build();
var adaptedObject = new Adapted();
var adapter = adapterFactory.Adapt(adaptedObject);
System.Console.WriteLine(adapter.Title); // "Read-only interface adapter"
}
Default properties implementation
An adapter maps interface properties on object properties using name and type. If an interface property does not have matching object property then it is generated with default value.
using EgaSoftware.Adapter;
interface IInterface
{
int Amount { get; }
}
public class Adapted
{
public int TotalCount { get; set; }
}
public void AdaptExample()
{
var adapterFactory = AdapterFactoryBuilder.For<IInterface, Adapted>().Build();
var adaptedObject = new Adapted() { TotalCount = 12 };
var adapter = adapterFactory.Adapt(adaptedObject);
System.Console.WriteLine(adapter.Amount); // 0
}
Collections
An adapter does support IEnumerable<> and IAsyncEnumerable<> collections. Bear in mind in that case methods Adapt()/AdaptAsync() return an enumerator so you should use ToList(), ToArray() or any other similar method in order to get a collection object.
using EgaSoftware.Adapter;
interface IInterface
{
int Amount { get; }
}
public class Adapted
{
public int Amount { get; set; }
}
public void AdaptExample()
{
var adapterFactory = AdapterFactoryBuilder.For<IInterface, Adapted>().Build();
var adaptedObjects = Enumerable.Range(1, 3).Select(x => new Adapted() { Amount = x }).ToArray();
var adapter = adapterFactory.Adapt(adaptedObjects);
System.Console.WriteLine(string.Join(", ", adapter.Select(x => x.Amount))); // 1, 2, 3
}
Dependency injection
You may use adapter with standard .Net IoC container.
using EgaSoftware.Adapter.DependencyInjection;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddAdapter(config =>
{
config.AddFor<IInterface, Adapted>().Map(x => x.Amount, _ => 100);
});
public class DependentClass
{
private readonly IAdapterFactory<IInterface, Addapted> _adapterFactory;
public DependentClass(IAdapterFactory<IInterface, Addapted> adapterFactory)
{
_adapterFactory = adapterFactory;
}
}
| 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 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. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.1 is compatible. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
This package has 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.