Mappify 1.2.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package Mappify --version 1.2.1
                    
NuGet\Install-Package Mappify -Version 1.2.1
                    
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="Mappify" Version="1.2.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Mappify" Version="1.2.1" />
                    
Directory.Packages.props
<PackageReference Include="Mappify" />
                    
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 Mappify --version 1.2.1
                    
#r "nuget: Mappify, 1.2.1"
                    
#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 Mappify@1.2.1
                    
#: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=Mappify&version=1.2.1
                    
Install as a Cake Addin
#tool nuget:?package=Mappify&version=1.2.1
                    
Install as a Cake Tool

Mappify

Mappify is a library for easy and convenient object mapping in C#. It allows you to create mappings between different types of objects using functions, providing flexibility and simplicity.

Supported Mapping Types

Mappify supports the following types of mappings:

  • Single object mapping
  • Array mapping
  • List mapping
  • Queue mapping
  • Stack mapping
  • Dictionary mapping
  • Mapping from multiple sources

Installation

You can install the library via NuGet:

dotnet add package Mappify

Usage

Setting Up Dependencies

To start working with the library, you need to register Mappify and your mapping profiles in the dependency container. You can do this as follows:

using Microsoft.Extensions.DependencyInjection;
using Mappify;

var services = new ServiceCollection();
services.AddMappify();
services.AddMappifyProfile<MyMappingProfile>(); // Replace with your mapping profile
var serviceProvider = services.BuildServiceProvider();

var mappify = serviceProvider.GetRequiredService<IMappify>();

Creating a Mapping Profile

Create a class that inherits from BaseMappingProfile and implement the CreateMaps method to define the mapping:

public class MyMappingProfile : BaseMappingProfile
{
    public override void CreateMaps(IMappify mappify)
    {
        mappify.CreateMap<SourceClass, DestinationClass>(source => new DestinationClass
        {
            Property1 = source.Property1,
            Property2 = source.Property2
        });
    }
}

Mapping Objects

Now you can use the created mapper to map objects:

var source = new SourceClass { Property1 = "Value1", Property2 = "Value2" };
var destination = mappify.Map<SourceClass, DestinationClass>(source);

Universal Mapping Method

The library provides a universal method for mapping objects:

public TD Map<TD>(object source)

This method allows mapping data from an object of any type to a target object of type TD. Example usage:

var source = new SourceClass { Property1 = "Value1", Property2 = "Value2" };
var destination = mappify.Map<DestinationClass>(source);

It also works with collections and arrays. Example usage:

var sourceList = new List<SourceClass>
{
    new SourceClass { Property1 = "Value1", Property2 = "Value2" },
    new SourceClass { Property1 = "Value3", Property2 = "Value4" }
};
var destinationList = mappify.Map<List<DestinationClass>>(sourceList);

Mapping Collections

The library also supports mapping collections. Here are a few examples:

Example 1: Mapping a List of Objects
var sourceList = new List<SourceClass>
{
    new SourceClass { Property1 = "Value1", Property2 = "Value2" },
    new SourceClass { Property1 = "Value3", Property2 = "Value4" }
};

var destinationList = mappify.MapList<DestinationClass>(sourceList);

// destinationList now contains two DestinationClass objects
Example 2: Mapping an Array of Objects
var sourceArray = new SourceClass[]
{
    new SourceClass { Property1 = "Value1", Property2 = "Value2" },
    new SourceClass { Property1 = "Value3", Property2 = "Value4" }
};

var destinationArray = mappify.MapArray<DestinationClass>(sourceArray);

// destinationArray now contains two DestinationClass objects
Example 3: Mapping a Queue of Objects
var sourceQueue = new Queue<SourceClass>();
sourceQueue.Enqueue(new SourceClass { Property1 = "Value1", Property2 = "Value2" });
sourceQueue.Enqueue(new SourceClass { Property1 = "Value3", Property2 = "Value4" });

var destinationQueue = mappify.MapQueue<SourceClass, DestinationClass>(sourceQueue);

// destinationQueue now contains two DestinationClass objects
Example 4: Mapping a Stack of Objects
var sourceStack = new Stack<SourceClass>();
sourceStack.Push(new SourceClass { Property1 = "Value1", Property2 = "Value2" });
sourceStack.Push(new SourceClass { Property1 = "Value3", Property2 = "Value4" });

var destinationStack = mappify.MapStack<SourceClass, DestinationClass>(sourceStack);

// destinationStack now contains two DestinationClass objects
Example 5: Mapping a Dictionary of Objects
var sourceDictionary = new Dictionary<int, SourceClass>
{
    { 1, new SourceClass { Property1 = "Value1", Property2 = "Value2" } },
    { 2, new SourceClass { Property1 = "Value3", Property2 = "Value4" } }
};

var destinationDictionary = mappify.MapDictionary<SourceClass, DestinationClass, int>(sourceDictionary);

// destinationDictionary now contains two DestinationClass objects with the same keys

Mapping from Multiple Sources

There are several overloads for mapping from multiple sources into one object.

// Mapping from 1 source.
public TD Map<TS1, TD>(TS1 source1)

// Mapping from 2 sources.
public TD Map<TS1, TS2, TD>(TS1 source1, TS2 source2)

// Mapping from 3 sources.
public TD Map<TS1, TS2, TS3, TD>(TS1 source1, TS2 source2, TS3 source3)

// Mapping from 4 sources.
public TD Map<TS1, TS2, TS3, TS4, TD>(TS1 source1, TS2 source2, TS3 source3, TS4 source4)

// Mapping from 5 sources.
public TD Map<TS1, TS2, TS3, TS4, TS5, TD>(TS1 source1, TS2 source2, TS3 source3, TS4 source4, TS5 source5)
Method 1: Mapping from a Single Source
public TD Map<TS1, TD>(TS1 source)

This method allows mapping data from a single source source of type TS1 to a target object of type TD. It is useful when you need to convert one object to another.

Example usage:

var singleSource = new SourceClass { Property1 = "Value1", Property2 = "Value2" };
var singleDestination = mappify.Map<SourceClass, DestinationClass>(singleSource);
Method 2: Mapping from Multiple Sources
public TD Map<TS1, TS2, TS3, TS4, TS5, TD>(TS1 source1, TS2 source2, TS3 source3, TS4 source4, TS5 source5)

This method allows mapping data from multiple sources (up to five) into a target object of type TD. This is convenient when you need to combine data from multiple objects into one.

Example usage:

var source1 = new SourceClass { Property1 = "Value1" };
var source2 = new OtherSourceClass { OtherProperty = "Value2" };
var source3 = new AdditionalSourceClass { AdditionalProperty = "Value3" };
var source4 = new FourthSourceClass { FourthProperty = "Value4" };
var source5 = new FifthSourceClass { FifthProperty = "Value5" };

// Creating mapping from multiple sources
mappify.CreateMap<SourceClass, OtherSourceClass, AdditionalSourceClass, FourthSourceClass, FifthSourceClass, CombinedDestinationClass>(
        (source1, source2, source3, source4, source5) => 
        {
            var combinedDestination = new CombinedDestinationClass
            {
                CombinedProperty1 = source1.Property1,
                CombinedProperty2 = source2.OtherProperty,
                CombinedProperty3 = source3.AdditionalProperty,
                CombinedProperty4 = source4.FourthProperty,
                CombinedProperty5 = source5.FifthProperty
            };
            return combinedDestination;
        });


var combinedDestination = Mappify.Map<SourceClass, OtherSourceClass, AdditionalSourceClass, FourthSourceClass, FifthSourceClass, CombinedDestinationClass>(source1, source2, source3, source4, source5);

Exceptions

The library defines exceptions that may occur during mapping errors:

  • MappifyException: occurs if the mapping for a given type is not defined.

Contribution

If you want to contribute to the project, please create a fork of the repository, make changes, and create a pull request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Contacts

If you have any questions or suggestions, you can contact me at lirikvolirvag@gmail.com.


Let me know if you need any further assistance!

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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.2 221 12/7/2024
1.2.2 5,478 11/23/2024
1.2.1 150 11/22/2024
1.2.0 137 11/22/2024
1.1.0 165 11/20/2024
1.0.2 194 11/18/2024
1.0.1 146 11/18/2024
1.0.0 130 11/17/2024