Skip to content

Commit 8108d3e

Browse files
committed
Update README.md
1 parent 92bb660 commit 8108d3e

File tree

1 file changed

+54
-3
lines changed

1 file changed

+54
-3
lines changed

README.md

+54-3
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@ This is why this library consists of simple interfaces for object mapping contra
1111

1212
## Usage
1313

14-
Install NuGet package to your project - you can find current release [here](https://www.nuget.org/packages/SimpleObjectMapper/). This will add ObjectMapper.Framework to your references and diagnostic analyser under analyzers node.
15-
You can then use mapper interfaces from ObjectMapper.Framework assembly to define mapping. For example let's define two classes with some matching properties:
14+
Install NuGet package to your project - you can find current release [here](https://www.nuget.org/packages/SimpleObjectMapper/). This will add ObjectMapper.Framework to your references and diagnostic analyser under analyzers node. You can then use mapper interfaces or attribute from ObjectMapper.Framework assembly to define mapping.
15+
16+
### IObjectMapper<T> interface
17+
18+
Let's define two classes with some matching properties:
1619

1720
```C#
1821
using ObjectMapper.Framework;
@@ -42,7 +45,7 @@ namespace TestClassLibrary
4245
}
4346
```
4447

45-
Let's say that we want to define mapping from class ClassA to ClassB. We specify that ClassA implements IObjectMapper<ClassB> interface. If we have cursor on IObjectMapper<ClassB> symbol, Visual Studio will display lightbulb on the right side. By clicking on lightbulb (or using space + . shortcut), you will get menu of options. If you click 'Generate implementation' interface implementation with mapping code will be generated for you. For the above example result is as follows:
48+
Let's say that we want to define mapping from class ClassA to ClassB. We specify that ClassA implements IObjectMapper<ClassB> interface. If we have caret on IObjectMapper<ClassB> symbol, Visual Studio will display lightbulb on the right side. By clicking on lightbulb (or using left control + . shortcut), you will get menu of options. If you click 'Generate implementation' interface implementation with mapping code will be generated for you. For the above example result is as follows:
4649

4750
```C#
4851
using ObjectMapper.Framework;
@@ -80,8 +83,56 @@ namespace TestClassLibrary
8083
}
8184
```
8285

86+
If you have method already defined (or generated), you can set caret on method name and 'Generate implementation' action will be available.
87+
88+
### IObjectMapperAdapter<T, U> interface
89+
8390
If you do not want to have mapping code inside your POCO classes you can use IObjectMapperAdapter<T,U>. By having cursor on IObjectMapperAdapter symbol, you can invoke 'Generate implementation' and generator will generate mapping code for mapping from class T to class U and from class U to class T.
8491

92+
If we take previous example of ClassA and ClassB and we define new class Adapter which implements IObjectMapperAdapter<ClassA, ClassB> generated code for Adapter class is as follows:
93+
94+
```C#
95+
public class Adapter : IObjectMapperAdapter<ClassA, ClassB>
96+
{
97+
public void MapObject(ClassA source, ClassB target)
98+
{
99+
target.Prop1 = source.Prop1 ?? default(int);
100+
target.Prop2 = source.Prop2;
101+
target.Prop3 = source.Prop3;
102+
target.Prop4.CopyFrom(source.Prop4);
103+
}
104+
105+
public void MapObject(ClassB source, ClassA target)
106+
{
107+
target.Prop1 = source.Prop1;
108+
target.Prop2 = source.Prop2;
109+
target.Prop3 = source.Prop3;
110+
target.Prop4.CopyFrom(source.Prop4);
111+
}
112+
}
113+
```
114+
115+
### ObjectMapperMethodAttribute attribute
116+
117+
If you want to have ad hoc method for mapping without actually implementing one of the above interfaces, you can annotate mapping method with ObjectMapperMethodAttribute attribute. This attribute can be applied only to methods with following signatures:
118+
- Method accepts exactly two parameters
119+
- Method return type is void
120+
121+
Compile time error will be raised otherwise. If you then place caret on method name, lightbulb will apear and 'Generate implementation' action will be available.
122+
123+
Example of method with applied attribute:
124+
125+
```C#
126+
[ObjectMapperMethod]
127+
private static void MapFromAToB(ClassA source, ClassB target)
128+
{
129+
target.Prop1 = source.Prop1 ?? default(int);
130+
target.Prop2 = source.Prop2;
131+
target.Prop3 = source.Prop3;
132+
target.Prop4.CopyFrom(source.Prop4);
133+
}
134+
```
135+
85136
## Code generation rules
86137

87138
1. Property names must match in both classes.

0 commit comments

Comments
 (0)