https://github.com/invaderzim85/zimlabs.mapper
Easy way to map two objects into one
https://github.com/invaderzim85/zimlabs.mapper
Last synced: 8 months ago
JSON representation
Easy way to map two objects into one
- Host: GitHub
- URL: https://github.com/invaderzim85/zimlabs.mapper
- Owner: InvaderZim85
- License: mit
- Created: 2021-05-09T12:56:36.000Z (about 5 years ago)
- Default Branch: main
- Last Pushed: 2022-10-07T17:30:21.000Z (over 3 years ago)
- Last Synced: 2025-09-03T09:23:37.022Z (9 months ago)
- Language: C#
- Size: 169 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ZimLabs.Mapper
Easy way to map two objects into one
 
## Usage
### Mapping
The two objects:
1. The objects
```csharp
internal class TargetPerson
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
// Ignore this property!
[IgnoreProperty]
public string IgnoreValue { get; set; }
public int? OtherId { get; set; }
}
internal class SourcePerson
{
public int? Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string IgnoreValue { get; set; }
public int OtherId { get; set; }
}
```
2. Map two objects (both objects exists)
```csharp
var sourcePerson = new SourcePerson
{
Id = null,
FirstName = "Bender",
LastName = "Rodriguez",
IgnoreValue = "Some fancy value",
OtherId = 666
};
var targetPerson = new TargetPerson();
Mapper.Map(sourcePerson, targetPerson);
```
3. Map two objects but create a new target object
```csharp
var sourcePerson = new SourcePerson
{
Id = null,
FirstName = "Bender",
LastName = "Rodriguez",
IgnoreValue = "Some fancy value",
OtherId = 666
};
var targetPerson = Mapper.CreateAndMap(sourcePerson);
```
### Get changes
1. The objects
```csharp
var firstPerson = new TargetPerson
{
Id = 1,
FirstName = "Bender",
LastName = "Rodriguez",
IgnoreValue = "Some fancy value",
OtherId = 666
};
var secondPerson = new TargetPerson
{
Id = 1,
FirstName = "Philip J.",
LastName = "Fry",
IgnoreValue = "Some fancy value",
OtherId = 123
};
```
2. Get the changes
```csharp
var changes = firstPerson.GetChanges(secondPerson);
```
The variable `change` contains a list with all changes. Every entry contains the following properties:
- *Property*: The name of the property
- *OldValue* The "original" old value
- *NewValue* The "new" value
3. The result
```
+-----------+-----------+-----------+
| Property | OldValue | NewValue |
+-----------+-----------+-----------+
| FirstName | Bender | Philip J. |
| LastName | Rodriguez | Fry |
| OtherId | 666 | 123 |
+-----------+-----------+-----------+
```