Vertizens.TypeMapper 0.5.0-rc.1.1

This is a prerelease version of Vertizens.TypeMapper.
There is a newer version of this package available.
See the version list below for details.
dotnet add package Vertizens.TypeMapper --version 0.5.0-rc.1.1                
NuGet\Install-Package Vertizens.TypeMapper -Version 0.5.0-rc.1.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="Vertizens.TypeMapper" Version="0.5.0-rc.1.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Vertizens.TypeMapper --version 0.5.0-rc.1.1                
#r "nuget: Vertizens.TypeMapper, 0.5.0-rc.1.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.
// Install Vertizens.TypeMapper as a Cake Addin
#addin nuget:?package=Vertizens.TypeMapper&version=0.5.0-rc.1.1&prerelease

// Install Vertizens.TypeMapper as a Cake Tool
#tool nuget:?package=Vertizens.TypeMapper&version=0.5.0-rc.1.1&prerelease                

Vertizens.TypeMapper

C# Type Mapper that uses name matching conventions but still allows for customization

Getting Started

Dependency injection usage:

services.AddTypeMappers();

ITypeMapper

This is the main interface you want to inject into your code.
Map from one type to another if the target type has an empty public constructor.

var target = mapper.Map<SourceType,TargetType>(source);

Or map onto an existing object.

mapper.Map(source, target);

Default Name Match Mapping

By default, public properties with the same name and equivalent types are matched. First it checks if they are Assignable with Type.IsAssignableFrom method. A nullable int32 is not the same as int32, it can only go from int to nullable int and not the other way around. Other than nullability assume the properties have to be the exact same name and exact same type. If not, its not getting mapped.
Next is the properties are not the same type but both are classes. This is where the type mapping in a sense gets recursive as the process starts all over but with the two property types in question. Finally, if the source is IEnumerable<> and the target is assignable by a List<> then it will also be mapped. But only if they are generics of a type that can be mapped.

ITypeMapper<TSource, TTarget>

Implement your own custom logic for mapping one type onto another. Then register in your DI container and it will get used by ITypeMapper. If you need the default Name Matching specifically for two types then inject INameMatchTypeMapper<Type1,Type2> for each one you need. You can also do it for the same type you are custom mapping so you can get the default behavior first then override or do mapping it missed. Likewise, inject IITypeMapper<TType1, Type2> to get whatever possible custom behavior you need for child property objects.

INameMatchTypeMapper<TSource,TTarget>

Just remember that ITypeMapper<TSource, TTarget> includes any customization that you write where as INameMatchTypeMapper<TSource,TTarget> maps based on the default name/type matching.

Use Case Suggestions

This is intended to not have a lot of rules that convert types, not even primitives like int32 to int64. If types are not exact that hold values then custom is required. This utility is intended where one type is essentially a subset of another and lends itself to low effort in accomplishing that task. Always check that a target type has mapped from the source type as desired.

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

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Vertizens.TypeMapper:

Package Downloads
Vertizens.SliceR

.NET Vertical Slice inspired approach of turning code into pipelines

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 141 12/7/2024
0.5.0-rc.1.4 90 11/26/2024
0.5.0-rc.1.3 67 11/24/2024
0.5.0-rc.1.2 66 11/23/2024
0.5.0-rc.1.1 68 11/23/2024
0.5.0-rc.1 73 11/22/2024
0.4.0-rc.1.1 70 11/16/2024
0.4.0-rc.1 63 11/15/2024
0.2.0-rc.1.1 67 11/15/2024
0.2.0-rc.1 96 11/10/2024
0.0.0-alpha.0.1 85 11/3/2024