https://github.com/f1x3d/explicitmapper
An explicit object mapper pattern for C#/.NET
https://github.com/f1x3d/explicitmapper
asp-net-core csharp dotnet hacktoberfest object-mapper patterns
Last synced: 24 days ago
JSON representation
An explicit object mapper pattern for C#/.NET
- Host: GitHub
- URL: https://github.com/f1x3d/explicitmapper
- Owner: f1x3d
- License: mit
- Created: 2022-10-16T19:47:59.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2022-10-16T20:48:30.000Z (over 3 years ago)
- Last Synced: 2026-03-05T05:06:19.007Z (3 months ago)
- Topics: asp-net-core, csharp, dotnet, hacktoberfest, object-mapper, patterns
- Language: C#
- Homepage:
- Size: 8.79 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ExplicitMapper
An explicit object mapper pattern for C#/.NET
## Motivation
* Better developer experience
* Easily navigate to the mapping method implementation from the calling side
* Autocomplete available target types based on the mapping source
* Support for multiple mappings for the same source/target type pair
* Compared to convention-based mappers, renaming a source/target property won't silently break anything
### "There are a lot of complex objects with many properties in my project, it would require too much time to map them manually"
You can use the [Mapping Generator extension](https://marketplace.visualstudio.com/items?itemName=54748ff9-45fc-43c2-8ec5-cf7912bc3b84.MappingGenerator2022) that will generate the corresponding mapping methods for you.
### "I would prefer the convention-based approach instead of the mapping methods for most of my objects"
You can still have it - add an `ExplicitMapper` constructor with the AutoMapper's `IMapper` parameter, pass it down to the `ExplicitMapperFromSource` and use it to implement `ToTarget()` mapping methods:
```csharp
public partial class ExplicitMapper
{
private readonly IMapper _mapper;
public ExplicitMapper(IMapper mapper)
{
_mapper = mapper;
}
}
```
```csharp
public partial class ExplicitMapper
{
public ExplicitMapperFromSource Map(Source source) => new(_mapper, source);
}
public partial class ExplicitMapperFromSource
{
private readonly IMapper _mapper;
private readonly Source _source;
public ExplicitMapperFromSource(IMapper mapper, Source source)
{
_mapper = mapper;
_source = source;
}
}
```
```csharp
public partial class ExplicitMapperFromSource
{
public Target ToTarget() => _mapper.Map(_source);
}
```