The Div CSharp Mapper Generator is a powerful tool designed to automate the creation of mapping classes in C#. It simplifies the process of mapping between models, DTOs, and other classes by generating the necessary code based on a simple JSON configuration file. This tool supports bi-directional mapping by default, with the flexibility to extend and customize the generated mappers through inheritance.
- Automatic Code Generation: Generate C# mapping classes based on a JSON configuration file.
- Bi-Directional Mapping: Supports mapping in both directions between classes.
- Object Property Handling: Automatically maps object properties, allowing for deep copies rather than reference assignments.
- Extensibility: Easily extend and customize the generated mapping methods by inheriting from the generated classes.
- Custom Logic: Override default methods to implement custom mapping logic as needed.
You can either compile the Div CSharp Mapper Generator from source or download the precompiled binary from the GitHub releases page.
Create a mapper.json file in the root of your project. This file defines the mappings you wish to generate.
{
"mappers": [
{
"namespace": "Namespace.Of.Your.Mapper",
"mapper": "MapperClassNameToGenerate",
"outputFolder": "output/folder/of/generated/mapper",
"classPairs": [
[ "Relative/Path/To/ModelClassX.cs", "Relative/Path/To/ModelClassX.cs" ] // to generate Clone method
[ "Relative/Path/To/ModelClassA.cs", "Relative/Path/To/ModelClassB.cs" ] // to generate Map method
]
}
]
}
namespace
: The namespace for the generated mapper class.mapper
: The name of the mapper class to generate.outputFolder
: The directory where the generated mapper file will be placed.classPairs
: An array of class pairs to map. Identical class pairs will generate a cloning method.
Execute the following command from the root of your project:
path/to/div-cs-mapper-generator.exe path/to/mapper.json
This command will generate the mapping classes based on the configuration provided in mapper.json.
The generated files will be found in the output folder specified in the JSON configuration.
You can inherit the generated mapper class to override the generated methods or add new ones.
namespace Namespace.Of.Your.Mapper
{
public class MapperClassNameToGenerate : MapperClassNameToGenerateBase
{
public override ModelClassB Map(ModelClassA model)
{
var result = base.Map(model);
// Custom logic here
return result;
}
}
}
Visit https://divengine.org