8000 GitHub - SimonNyvall/Morphiq: Attribute-based object mapping and transformation. ⚙️
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

SimonNyvall/Morphiq

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

 /$$      /$$                               /$$       /$$          
| $$$    /$$$                              | $$      |__/          
| $$$$  /$$$$  /$$$$$$   /$$$$$$   /$$$$$$ | $$$$$$$  /$$  /$$$$$$ 
| $$ $$/$$ $$ /$$__  $$ /$$__  $$ /$$__  $$| $$__  $$| $$ /$$__  $$
| $$  $$$| $$| $$  \ $$| $$  \__/| $$  \ $$| $$  \ $$| $$| $$  \ $$
| $$\  $ | $$| $$  | $$| $$      | $$  | $$| $$  | $$| $$| $$  | $$
| $$ \/  | $$|  $$$$$$/| $$      | $$$$$$$/| $$  | $$| $$|  $$$$$$$
|__/     |__/ \______/ |__/      | $$____/ |__/  |__/|__/ \____  $$
                                 | $$                          | $$
                                 | $$                          | $$
                                 |__/                          |__/

Build & Test GitHub License NuGet Downloads

Premise

Morphiq is a versatile tool that enhances object mapping and transformation in C# using attribute-based configurations. It simplifies the process of converting objects between different types, making it ideal for scenarios such as data transfer objects (DTOs), view models, and response objects in web APIs.

Features

  • Attribute-Based Mapping: Use attributes to define custom mappings, default values, and properties to ignore.
  • Custom Mapping Methods: Define custom mapping logic using methods specified by attributes.
  • Default Value Handling: Automatically set default values for properties when the source property is null.
  • Property Ignoring: Exclude specific properties from the mapping process using ignore attributes.
  • Flexible Configuration: Apply additional configuration to the target object during the mapping proces

Example

Basic Usage

using Morphiq;

public class Entity
{
    public string Name { get; set; } = "John Doe";
    public int Age { get; set; } = 30;
}

public class DTO
{
    public string Name { get; set; } = string.Empty;
    public int Age { get; set; }
}

var entity = new Entity();

var dto = entity.MorphTo<DTO>();

Console.WriteLine(dto.Name); // Output: John Doe
Console.WriteLine(dto.Age);  // Output: 30

Attributes

Some attributes are available to control the behavior of the mapping.

IgnoreProperty

using Morphiq;
using Morphiq.Attributes;

public class Entity
{
    public string Name { get; set; } = "John Doe";
    [IgnoreProperty]
    public int Age { get; set; } = 30;
}

public class DTO
{
    public string Name { get; set; }
    public int Age { get; set; }
}

var entity = new Entity();
var dto = entity.MorphTo<DTO>();

Console.WriteLine(dto.Name); // Output: John Doe
Console.WriteLine(dto.Age);  // Output: 0 (default value)

Default Value

using Morphiq;
using Morphiq.Attributes;

public class Entity
{
    [MorphToDefaultValue("Unknown")]
    public string Name { get; set; }
    public int Age { get; set; } = 30;
}

public class DTO
{
    public string Name { get; set; }
    public int Age { get; set; }
}

var entity = new Entity();
var dto = entity.MorphTo<DTO>();

Console.WriteLine(dto.Name); // Output: Unknown
Console.WriteLine(dto.Age);  // Output: 30

Custom Mapping

using Morphiq;
using Morphiq.Attributes;

public class Entity
{
    public string Name { get; set; } = "John Doe";
    public int Age { get; set; } = 30;

    [MorphToCustomMapping(nameof(MapName))]
    public string FullName { get; set; } = "John Doe";

    private string MapName(object value)
    {
        return $"Mapped: {value}";
    }
}

public class DTO
{
    public string Name { get; set; }
    public int Age { get; set; }
    public string FullName { get; set; }
}

var entity = new Entity();
var dto = entity.MorphTo<DTO>();

Console.WriteLine(dto.Name);     // Output: John Doe
Console.WriteLine(dto.Age);      // Output: 30
Console.WriteLine(dto.FullName); // Output: Mapped: John Doe

Configuration

using Morphiq;

public class Entity
{
    public string Name { get; set; } = "John Doe";
    public int Age { get; set; } = 30;
}

public class DTO
{
    public string Name { get; set; }
    public int Age { get; set; }
}

var entity = new Entity();
var dto = entity.MorphTo<DTO>(x => x.Name = "Jane Doe");

Console.WriteLine(dto.Name); // Output: Jane Doe
Console.WriteLine(dto.Age);  // Output: 30

Installation

Run the following command in your project directory:

dotnet add package Morphiq --version 0.1.0

License

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

0