Kkts.Dynamic 1.0.1

dotnet add package Kkts.Dynamic --version 1.0.1                
NuGet\Install-Package Kkts.Dynamic -Version 1.0.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="Kkts.Dynamic" Version="1.0.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Kkts.Dynamic --version 1.0.1                
#r "nuget: Kkts.Dynamic, 1.0.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 Kkts.Dynamic as a Cake Addin
#addin nuget:?package=Kkts.Dynamic&version=1.0.1

// Install Kkts.Dynamic as a Cake Tool
#tool nuget:?package=Kkts.Dynamic&version=1.0.1                

Kkts.Dynamic

A lightweight and dynamic mapper, you don't need to declare any classes for View Model or Data Transfer Object, just declare property bindings.

get via nuget Kkts.Dynamic

Sample class

public class Product
{
    public int Id { get; set; }

    public string Code { get; set; }

    public string ProductName { get; set; }

    public double Price { get; set; }

    public string ExternalCode { get; set; }

    public string ExternalInformation { get; set; }

    public string Sku { get; set; }

    public Category Category { get; set; }

    public List<Tag> Tags { get; set; }
}

public class Category
{
    public int Id { get; set; }

    public string Name { get; set; }
}

public class Tag
{
    public int Id { get; set; }

    public string Name { get; set; }
}
Sample Json Data of product
{
	id: 1,
	code: 'P01',
	productName: 'Test Product',
	price: 20.5,
	externalCode: 'P01PHONE',
	externalInformation: 'Product information',
	sku: 'VN_HCM',
	category: {
		id: 1220,
		name: 'Mobile'
	},
	tags: [
		{
			id: 1,
			name: 'phone'
		},
		{
			id: 2,
			name: 'mobile'
		},
		{
			id: 3,
			name: 'mobile_phone'
		}
	]
}

Usage (Map product to dto)

var assembly = new DynamicAssembly();
// Bindings
var productBindings = new PropertyBinding[]
{
    new PropertyBinding { EntityProperty = "Id", DtoProperty = "Id", Mode = BindingMode.OneWayToDto, IsPrimaryKey = true, PrimaryKeyOrder = 0 },
    new PropertyBinding { EntityProperty = "ProductName", DtoProperty = "Name", Mode = BindingMode.TwoWay },
    new PropertyBinding { EntityProperty = "Code", DtoProperty = "Code", Mode = BindingMode.TwoWay, IsPrimaryKey = true, PrimaryKeyOrder = 1 },
    new PropertyBinding { EntityProperty = "Price", DtoProperty = "Price", Mode = BindingMode.TwoWay },
    new PropertyBinding { EntityProperty = "ExternalCode", DtoProperty = "External.Code", Mode = BindingMode.TwoWay },
    new PropertyBinding { EntityProperty = "ExternalInformation", DtoProperty = "External.Information", Mode = BindingMode.TwoWay },
    new PropertyBinding { EntityProperty = "Sku", DtoProperty = "Sku", Mode = BindingMode.TwoWay },
    new PropertyBinding { EntityProperty = "Category", DtoProperty = "Category", Mode = BindingMode.TwoWay }, // map category
    new PropertyBinding { EntityProperty = "Category.Name", DtoProperty = "CategoryName", Mode = BindingMode.TwoWay }, // map Category.Name as CategoryName
    new PropertyBinding { EntityProperty = "Tags", DtoProperty = "Tags", Mode = BindingMode.TwoWay }
};

var categoryBindings = new PropertyBinding[]
{
    new PropertyBinding { EntityProperty = "Id", DtoProperty = "Id", Mode = BindingMode.TwoWay, IsPrimaryKey = true, PrimaryKeyOrder = 1 },
    new PropertyBinding { EntityProperty = "Name", DtoProperty = "CategoryName", Mode = BindingMode.TwoWay }
};

var tagBindings = new PropertyBinding[]
{
    new PropertyBinding { EntityProperty = "Id", DtoProperty = "Id", Mode = BindingMode.TwoWay },
    new PropertyBinding { EntityProperty = "Name", DtoProperty = "Name", Mode = BindingMode.TwoWay, IsPrimaryKey = true, PrimaryKeyOrder = 1 },
};

// declare classes
Class productDtoClass = assembly.DeclareClass("Product", typeof(Product), productBindings);
Class categoryDtoClass = assembly.DeclareClass("Category", typeof(Category), categoryBindings);
Class tagDtoClass = assembly.DeclareClass("Tag", typeof(Tag), tagBindings);

assembly.Build();

Type productDtoType = productDtoClass.GetBuiltType();
var product = JsonConvert.DeserializeObject<Product>(jsonData);
var productDto = Activator.CreateInstance(productDtoType);
Mapper.MapFromEntityToDto(productDto, product);
var resultJson = JsonConvert.SerializeObject(productDto);
resultJson
{
	id: 1,
	code: 'P01',
	name: 'Test Product',
	price: 20.5,
	external: {
		code: 'P01PHONE',
		information: 'Product information'
	},
	sku: 'VN_HCM',
	categoryName: 'Mobile',
	category: {
		id: 1220,
		categoryName: 'Mobile'
	},
	tags: [
		{
			id: 1,
			name: 'phone'
		},
		{
			id: 2,
			name: 'mobile'
		},
		{
			id: 3,
			name: 'mobile_phone'
		}
	]
}
Note

DTO → Data Transfer Object

  1. You can reverse the map by using Mapper.MapFromDtoToEntity(productDto, newProduct);
  2. Binding Mode:
  • 2.1 TwoWay: it will create the property map from entity to DTO and reverse map (from dto to entity)
  • 2.2 OneWayToDto: it will create only one way map from entity to DTO (read only)
  • 2.2 OneWayToEntity: it will create only one way map from DTO to entity (for update)
  1. Use PrimaryKey = true, it means that property or properties are ids, you can get the ids by using DtoObject.GetIds(productDto) (return an object array)
  2. All dto objects are implemented interface IDtoObject
  3. For example above, you don't need to decare two classes Category and Tag if the dto structure of the classes are the same with entities
  4. All attributes of property of entity will copy to property of DTO
  5. You will never worry about null exception when calling Mapper.MapFromDtoToEntity or Mapper.MapFromEntityToDto, it always check if null before mapping a property
  6. You will never worry about performance because the class and the mapping are built by IL code

Contacts

LinkedIn Skype: linh.nhat.le

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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  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.

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.1 480 12/3/2020
1.0.0 385 11/30/2020
1.0.0-rc1 314 11/29/2020
1.0.0-beta 303 11/29/2020