VelocityMapper 1.2.1-preview

This is a prerelease version of VelocityMapper.
The owner has unlisted this package. This could mean that the package is deprecated, has security vulnerabilities or shouldn't be used anymore.
dotnet add package VelocityMapper --version 1.2.1-preview
                    
NuGet\Install-Package VelocityMapper -Version 1.2.1-preview
                    
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="VelocityMapper" Version="1.2.1-preview" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="VelocityMapper" Version="1.2.1-preview" />
                    
Directory.Packages.props
<PackageReference Include="VelocityMapper" />
                    
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 VelocityMapper --version 1.2.1-preview
                    
#r "nuget: VelocityMapper, 1.2.1-preview"
                    
#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 VelocityMapper@1.2.1-preview
                    
#: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=VelocityMapper&version=1.2.1-preview&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=VelocityMapper&version=1.2.1-preview&prerelease
                    
Install as a Cake Tool

VelocityMapper

The fastest .NET mapper. Zero reflection. Zero overhead. Powered by Source Generators.

License: MIT NuGet NuGet Downloads .NET

VelocityMapper uses Source Generators to generate optimized mapping code at compile-time. Familiar AutoMapper-style API with superior performance.


๐Ÿ“ฆ Installation

dotnet add package VelocityMapper

Supported frameworks: .NET 6, 8, 9, 10

โš ๏ธ Required: Enable Source Generator in Your Project

VelocityMapper requires the Source Generator to work. Add this to your .csproj:

<ItemGroup>
  <ProjectReference Include="path/to/VelocityMapper.Generators.csproj" 
                    OutputItemType="Analyzer" 
                    ReferenceOutputAssembly="false" />
</ItemGroup>

If you installed via NuGet, the analyzer is automatically included - no additional configuration needed.

If you're referencing the source project, you must add the reference above to enable code generation at compile-time.


๐Ÿš€ Quick Start

1. Create your models

public class User
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Email { get; set; }
    public Address Address { get; set; }
}

public class UserDto
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Email { get; set; }
    public AddressDto Address { get; set; }
}

2. Configure mappings

using VelocityMapper;

public static class AppMapperConfig
{
    [MapperConfiguration]
    public static void ConfigureMappers()
    {
        MapperSetup.CreateMap<User, UserDto>();
        MapperSetup.CreateMap<Address, AddressDto>();
    }
}

3. Use the mapper

var user = new User { Id = 1, Name = "John", Email = "john@email.com" };

// โšก Create new instance
var dto = Mapper.To<UserDto>(user);

// Zero allocation - map to existing object
var existingDto = new UserDto();
Mapper.To(user, existingDto);

๐Ÿ“š API

Basic Mapping

// New instance
var dto = Mapper.To<UserDto>(user);

// To existing object (zero allocation)
Mapper.To(user, existingDto);

Collection Mapping

// โšก NEW API - Cleaner and faster with Span!
var users = GetUsers(); // List<User>, User[], IEnumerable<User>

// ToList - Auto-optimized with CollectionsMarshal.AsSpan (.NET 8+)
List<UserDto> dtos = Mapper.ToList<UserDto>(users);

// ToArray - Optimized with Span zero-copy
UserDto[] array = Mapper.ToArray<UserDto>(users);

// ToEnumerable - Lazy evaluation (deferred execution)
IEnumerable<UserDto> enumerable = Mapper.ToEnumerable<UserDto>(users);
var filtered = enumerable.Where(x => x.Id > 10).ToList();

// ToSpan - TRUE zero allocation (advanced)
Span<UserDto> destination = stackalloc UserDto[100];
Mapper.ToSpan(users.AsSpan(), destination);

// Legacy API (still supported, but ToList/ToArray are faster)
List<UserDto> dtos2 = CollectionMapper.MapToList(users, Mapper.To<UserDto>);

๐Ÿ”„ Mapping Behavior

Extra Properties are Automatically Ignored

VelocityMapper maps based on destination properties. Properties that exist only in the source are automatically ignored:

public class User
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Email { get; set; }
    public string PasswordHash { get; set; }  // Only exists in entity
}

public class UserDto
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Email { get; set; }
    // PasswordHash doesn't exist โ†’ ignored automatically!
}

var dto = Mapper.To<UserDto>(user);
// dto will have: Id, Name, Email
// PasswordHash is silently ignored โœ“
Scenario Behavior
Property exists in both โœ… Maps
Property only in source โœ… Silently ignores
Property only in destination โœ… Keeps default value

Attributes

public class UserDto
{
    public int Id { get; set; }
    
    [MapFrom("FirstName")]  // Map from differently named property
    public string Name { get; set; }
    
    [IgnoreMap]  // Explicitly ignore (documentation)
    public string CacheKey { get; set; }
}

๐ŸŽ๏ธ Performance

Benchmark on .NET 10 (Intel Core i5-14600KF):

Mapper Time Comparison
VelocityMapper 12.03 ns Fastest
Manual 12.22 ns baseline
Mapperly 12.29 ns 2% slower
Mapster 18.91 ns 57% slower
AutoMapper 32.87 ns 173% slower

VelocityMapper is faster than hand-written code.


๐Ÿ”ง How It Works

The Source Generator analyzes your code at compile-time and generates optimized methods:

// You write:
Mapper.CreateMap<User, UserDto>();
var dto = Mapper.To<UserDto>(user);

// The generator automatically creates:
public static UserDto To(User source)
{
    return new UserDto
    {
        Id = source.Id,           // Value types first (cache-friendly)
        Age = source.Age,
        Name = source.Name,       // Reference types after
        Email = source.Email,
        Address = source.Address is { } addr ? To(addr) : null  // Nested mapping
    };
}

๐Ÿ“‹ Quick Reference

Method Usage Allocation Performance
Mapper.To<TDest>(source) New instance DTO size โšกโšกโšก 12ns
Mapper.To(source, dest) Existing object 0 B โšกโšกโšก Zero alloc
Mapper.ToList<TDest>(enumerable) IEnumerable โ†’ List List + DTOs โšกโšกโšก Span-optimized
Mapper.ToArray<TDest>(enumerable) IEnumerable โ†’ Array Array + DTOs โšกโšกโšก Span zero-copy
Mapper.ToEnumerable<TDest>(enumerable) IEnumerable โ†’ IEnumerable Lazy โšกโšกโšก Deferred execution
Mapper.ToSpan(src, dest) Span โ†’ Span 0 B โšกโšกโšก TRUE zero alloc
CollectionMapper.* (legacy) Compatibility List + DTOs โšกโšก Slower

New API: ToList, ToArray and ToEnumerable automatically detect List/Array and use fast-path with Span!


๐Ÿ“„ License

MIT License - see LICENSE

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  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 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 is compatible.  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 is compatible.  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.
  • net10.0

    • No dependencies.
  • net6.0

    • No dependencies.
  • net8.0

    • No dependencies.
  • net9.0

    • 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