-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
689 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio Version 17 | ||
VisualStudioVersion = 17.10.35201.131 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Divengine.CSMapperGenerator", "generator\Divengine.CSMapperGenerator.csproj", "{B9C3C1BD-0C96-41BD-BDFF-02745EF29F33}" | ||
EndProject | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Divengine.CSMapperGenerator.Example", "example\Divengine.CSMapperGenerator.Example.csproj", "{13DCAAF3-37EF-4F26-B644-9F6F7B6D3DE2}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{B9C3C1BD-0C96-41BD-BDFF-02745EF29F33}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{B9C3C1BD-0C96-41BD-BDFF-02745EF29F33}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{B9C3C1BD-0C96-41BD-BDFF-02745EF29F33}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{B9C3C1BD-0C96-41BD-BDFF-02745EF29F33}.Release|Any CPU.Build.0 = Release|Any CPU | ||
{13DCAAF3-37EF-4F26-B644-9F6F7B6D3DE2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{13DCAAF3-37EF-4F26-B644-9F6F7B6D3DE2}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{13DCAAF3-37EF-4F26-B644-9F6F7B6D3DE2}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{13DCAAF3-37EF-4F26-B644-9F6F7B6D3DE2}.Release|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
GlobalSection(ExtensibilityGlobals) = postSolution | ||
SolutionGuid = {9D90E011-130D-489D-8607-75FEB7988987} | ||
EndGlobalSection | ||
EndGlobal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
|
||
<IsPackable>false</IsPackable> | ||
<IsTestProject>true</IsTestProject> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="coverlet.collector" Version="6.0.0" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" /> | ||
<PackageReference Include="NUnit" Version="3.14.0" /> | ||
<PackageReference Include="NUnit.Analyzers" Version="3.9.0" /> | ||
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Using Include="NUnit.Framework" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using Divengine.CSMapperGenerator.Example.Model; | ||
|
||
namespace Divengine.CSMapperGenerator.Example.Mappers | ||
{ | ||
public class CustomEmployeeMapper: EmployeeMapper | ||
{ | ||
public static new EmployeeDto Map(Employee fromEntity) | ||
{ | ||
EmployeeDto toEntity = EmployeeMapper.Map(fromEntity); | ||
|
||
toEntity.Address = Clone(fromEntity.Address); | ||
toEntity.FullName = $"{fromEntity.FirstName} {fromEntity.LastName}"; | ||
toEntity.Age = DateTime.Now.Year - fromEntity.DateOfBirth.Year; | ||
|
||
return toEntity; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
//------------------------------------------------------------------------------ | ||
// <auto-generated> | ||
// This code was generated by a Div CS Mapper Generator. | ||
// Runtime Version: 1.0.0 | ||
// See: https://divengine.org | ||
// | ||
// Changes to this file may cause incorrect behavior and will be lost if | ||
// the code is regenerated. | ||
// </auto-generated> | ||
//------------------------------------------------------------------------------ | ||
|
||
// Custom Mapper: EmployeeMapper | ||
|
||
using Divengine.CSMapperGenerator.Example.Model; | ||
|
||
namespace Divengine.CSMapperGenerator.Example.Mappers | ||
{ | ||
public class EmployeeMapper | ||
{ | ||
public static Address Clone(Address fromEntity) | ||
{ | ||
if (fromEntity == null) | ||
{ | ||
return null; | ||
} | ||
|
||
Address toEntity = new Address(); | ||
toEntity.Street = fromEntity.Street; | ||
toEntity.City = fromEntity.City; | ||
toEntity.State = fromEntity.State; | ||
toEntity.ZipCode = fromEntity.ZipCode; | ||
return toEntity; | ||
} | ||
|
||
public static EmployeeDto Clone(EmployeeDto fromEntity) | ||
{ | ||
if (fromEntity == null) | ||
{ | ||
return null; | ||
} | ||
|
||
EmployeeDto toEntity = new EmployeeDto(); | ||
toEntity.Id = fromEntity.Id; | ||
toEntity.FullName = fromEntity.FullName; | ||
toEntity.Department = fromEntity.Department; | ||
toEntity.Age = fromEntity.Age; | ||
toEntity.Address = fromEntity.Address; | ||
return toEntity; | ||
} | ||
|
||
public static Employee Clone(Employee fromEntity) | ||
{ | ||
if (fromEntity == null) | ||
{ | ||
return null; | ||
} | ||
|
||
Employee toEntity = new Employee(); | ||
toEntity.Id = fromEntity.Id; | ||
toEntity.FirstName = fromEntity.FirstName; | ||
toEntity.LastName = fromEntity.LastName; | ||
toEntity.DateOfBirth = fromEntity.DateOfBirth; | ||
toEntity.Department = fromEntity.Department; | ||
toEntity.Address = fromEntity.Address; | ||
return toEntity; | ||
} | ||
|
||
public static EmployeeDto Map(Employee fromEntity) | ||
{ | ||
if (fromEntity == null) | ||
{ | ||
return null; | ||
} | ||
|
||
EmployeeDto toEntity = new EmployeeDto(); | ||
toEntity.Id = fromEntity.Id; | ||
toEntity.Department = fromEntity.Department; | ||
toEntity.Address = fromEntity.Address; | ||
return toEntity; | ||
} | ||
|
||
public static Employee Map(EmployeeDto fromEntity) | ||
{ | ||
if (fromEntity == null) | ||
{ | ||
return null; | ||
} | ||
|
||
Employee toEntity = new Employee(); | ||
toEntity.Id = fromEntity.Id; | ||
toEntity.Department = fromEntity.Department; | ||
toEntity.Address = fromEntity.Address; | ||
return toEntity; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Folder for output the generated mappers |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace Divengine.CSMapperGenerator.Example.Model | ||
{ | ||
public class Address | ||
{ | ||
public string? Street { get; set; } | ||
public string? City { get; set; } | ||
public string? State { get; set; } | ||
public string? ZipCode { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
namespace Divengine.CSMapperGenerator.Example.Model | ||
{ | ||
public class Employee | ||
{ | ||
public int Id { get; set; } | ||
public string? FirstName { get; set; } | ||
public string? LastName { get; set; } | ||
public DateTime DateOfBirth { get; set; } | ||
public string? Department { get; set; } | ||
public Address? Address { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
namespace Divengine.CSMapperGenerator.Example.Model | ||
{ | ||
public class EmployeeDto | ||
{ | ||
public int Id { get; set; } | ||
public string? FullName { get; set; } | ||
public string? Department { get; set; } | ||
public int Age { get; set; } | ||
public Address? Address { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using Divengine.CSMapperGenerator.Example.Mappers; | ||
using Divengine.CSMapperGenerator.Example.Model; | ||
|
||
var employee = new Employee | ||
{ | ||
Id = 1, | ||
FirstName = "John", | ||
LastName = "Doe", | ||
DateOfBirth = new DateTime(1990, 1, 1), | ||
Department = "IT", | ||
Address = new Address | ||
{ | ||
Street = "123 Elm St.", | ||
City = "Springfield", | ||
State = "IL", | ||
ZipCode = "62701" | ||
} | ||
}; | ||
|
||
var dto = EmployeeMapper.Map(employee); | ||
|
||
Console.WriteLine(dto.Id); | ||
Console.WriteLine(dto.Department); | ||
|
||
var clonedDto = EmployeeMapper.Clone(dto); | ||
Console.WriteLine(clonedDto.Id); | ||
Console.WriteLine(clonedDto.Department); | ||
|
||
var dto2 = CustomEmployeeMapper.Map(employee); | ||
|
||
Console.WriteLine(dto.Address?.Street); | ||
dto.Address.Street = "123 Main St."; | ||
|
||
Console.WriteLine(dto2.Address?.Street); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# Div CS Mapper Generator | ||
|
||
## Example project | ||
|
||
This project is an example of how to use the Div CS Mapper Generator. | ||
|
||
### How to use | ||
|
||
1. Compile or download the Div CS Mapper Generator from GitHub. | ||
|
||
2. Create a `mapper.json` file in the root of the project with the following content: | ||
|
||
```json | ||
{ | ||
"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" ] // clone | ||
[ "Relative/Path/To/ModelClassA.cs", "Relative/Path/To/ModelClassB.cs" ] // map | ||
] | ||
} | ||
] | ||
} | ||
``` | ||
|
||
3. Run the following command in the root of the project: | ||
|
||
```bash | ||
$ path/to/div-cs-mapper-generator.exe path/to/mapper.json | ||
``` | ||
|
||
4. The generated files will be in the defined outputFolder | ||
|
||
### Extends and customization | ||
|
||
You can inherit the generated mapper class to override the generated methods or add new ones. | ||
|
||
```csharp | ||
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; | ||
} | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using Divengine.CSMapperGenerator.Example.Mappers; | ||
using Divengine.CSMapperGenerator.Example.Model; | ||
|
||
namespace Divengine.CSMapperGenerator.Example.Tests | ||
{ | ||
public class CustomMapperSuite | ||
{ | ||
[Test] | ||
public void MapEmployeeToEmployeeDto() | ||
{ | ||
// Arrange | ||
Employee employee = new() | ||
{ | ||
Id = 1, | ||
FirstName = "John", | ||
LastName = "Doe", | ||
DateOfBirth = new DateTime(1990, 1, 1), | ||
Department = "IT", | ||
Address = new Address | ||
{ | ||
Street = "123 Main St", | ||
City = "Springfield", | ||
State = "IL", | ||
ZipCode = "62701" | ||
} | ||
}; | ||
|
||
// Act | ||
EmployeeDto employeeDto = CustomEmployeeMapper.Map(employee); | ||
|
||
// Assert | ||
Assert.That(employeeDto.Id, Is.EqualTo(1)); | ||
Assert.That(employeeDto.FullName ?? "", Is.EqualTo("John Doe")); | ||
Assert.That(employeeDto.Department ?? "", Is.EqualTo("IT")); | ||
Assert.That(employeeDto.Age, Is.EqualTo(34)); | ||
Assert.That(employeeDto.Address?.Street ?? "", Is.EqualTo("123 Main St")); | ||
Assert.That(employeeDto.Address?.City ?? "", Is.EqualTo("Springfield")); | ||
Assert.That(employeeDto.Address?.State ?? "", Is.EqualTo("IL")); | ||
Assert.That(employeeDto.Address?.ZipCode ?? "", Is.EqualTo("62701")); | ||
|
||
employee.Address = null; | ||
|
||
Assert.That(employeeDto.Address, Is.Not.Null); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"mappers": [ | ||
{ | ||
"mapper": "EmployeeMapper", | ||
"outputFolder": "Mappers", | ||
"namespace": "Divengine.CSMapperGenerator.Example.Mappers", | ||
"classPairs": [ | ||
[ "Model/Address.cs", "Model/Address.cs" ], | ||
[ "Model/EmployeeDto.cs", "Model/EmployeeDto.cs" ], | ||
[ "Model/Employee.cs", "Model/Employee.cs" ], | ||
[ "Model/Employee.cs", "Model/EmployeeDto.cs" ] | ||
] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="CommandLineParser" Version="2.9.1" /> | ||
<PackageReference Include="Microsoft.CodeAnalysis" Version="4.10.0" /> | ||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" /> | ||
<PackageReference Include="Spectre.Console" Version="0.49.1" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace Divengine.CSMapperGenerator.Models | ||
{ | ||
public class MapperConfiguration | ||
{ | ||
public List<MapperDefinition>? Mappers { get; set; } | ||
} | ||
} |
Oops, something went wrong.