EgaSoftware.Adapter 1.0.2

dotnet add package EgaSoftware.Adapter --version 1.0.2
                    
NuGet\Install-Package EgaSoftware.Adapter -Version 1.0.2
                    
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="EgaSoftware.Adapter" Version="1.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="EgaSoftware.Adapter" Version="1.0.2" />
                    
Directory.Packages.props
<PackageReference Include="EgaSoftware.Adapter" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add EgaSoftware.Adapter --version 1.0.2
                    
#r "nuget: EgaSoftware.Adapter, 1.0.2"
                    
#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.
#:package EgaSoftware.Adapter@1.0.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=EgaSoftware.Adapter&version=1.0.2
                    
Install as a Cake Addin
#tool nuget:?package=EgaSoftware.Adapter&version=1.0.2
                    
Install as a Cake Tool

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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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.

Version Downloads Last Updated
1.0.2 250 5/28/2025
1.0.1 214 5/28/2025