Skip to content

Commit

Permalink
Merge pull request #6 from rena0157/develop
Browse files Browse the repository at this point in the history
Release of 0.3.0
  • Loading branch information
rena0157 authored Apr 9, 2021
2 parents ec743b0 + 6562943 commit 3715694
Show file tree
Hide file tree
Showing 17 changed files with 310 additions and 24 deletions.
2 changes: 1 addition & 1 deletion GitVersion.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
mode: ContinuousDelivery
next-version: 0.2.0
next-version: 0.3.0
branches: {}
ignore:
sha: []
Expand Down
37 changes: 28 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
# Mapr

A simple object to object mapper without any of the magic configuration.
[![Mapr Build](https://github.com/rena0157/mapr/actions/workflows/build.yml/badge.svg)](https://github.com/rena0157/mapr/actions/workflows/build.yml)

A simple mediator pattern based object to object mapper.

## How to Install

Release Version on [NuGet](https://www.nuget.org/packages/Mapr/)
```
dotnet add package Mapr --version xxx
```

Pre-Release Version (Alpha / Beta) on [GitHub](https://github.com/rena0157/mapr/packages/)
```
dotnet add package Mapr --version xxx
```

## Why Mapr?

Expand All @@ -21,13 +35,13 @@ see Mapr.DependencyInjection for `IServiceCollection`.
These `IMap` implementations are then loaded and injected using the DI container in the mapper whenever a
`Map<TSource, TDestination>(sourceObject)` is called.

## Simple Types
Here is an example of a mapper that converts from `string` to `int` and back.

TestMap.cs
```cs
// StringMapper.cs
// a map between int and string as well as a map between string and int.
public class TestMap : IMap<string, int>, IMap<int, string>
public class StringMapper : IMap<string, int>, IMap<int, string>
{
public int Map(string source)
{
Expand All @@ -42,12 +56,13 @@ public class TestMap : IMap<string, int>, IMap<int, string>

```

## Service Collection Setup

Service Collection Setup
The above map then is set up in the service registration below.

```cs

// Service Registration file
// This could be in Startup.cs for an ASP.NET Project.
var services = new ServiceCollection();

services.AddMapr(config =>
Expand All @@ -73,9 +88,13 @@ var testString = "2";
var testInt = 10;

// will return the int: 2
var mappedStringToInt = mapper.Map<string, int>(testString);
int mappedStringToInt = mapper.Map<string, int>(testString);

// will return the string: "10"
var mappedIntToString = mapper.Map<int, string>(testInt);
string mappedIntToString = mapper.Map<int, string>(testInt);

```

## More Complex Mapping

See [samples](samples/SampleMapperApp/) for more complex examples
7 changes: 6 additions & 1 deletion build/Build.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class Build : NukeBuild
.DependsOn(Pack)
.Executes(() =>
{
if (GitRepository.IsOnDevelopBranch())
if (GitRepository.IsOnDevelopBranch() || GitRepository.IsOnReleaseBranch())
{
DotNetNuGetPush(p => p
.SetTargetPath(OutputDirectory / "*.nupkg")
Expand All @@ -108,6 +108,11 @@ class Build : NukeBuild
.SetTargetPath(OutputDirectory / "*.nupkg")
.SetSource("https://api.nuget.org/v3/index.json")
.SetApiKey(NugetApiKey));

DotNetNuGetPush(p => p
.SetTargetPath(OutputDirectory / "*.nupkg")
.SetSource("https://nuget.pkg.github.com/rena0157/index.json")
.SetApiKey(GitHubToken));
}
else
{
Expand Down
17 changes: 17 additions & 0 deletions samples/SampleMapperApp/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Sample Map App

The purpose of this sample app is to present the intended use and setup of Mapr.

It consists of four parts:

1. A `Domain` folder that contains the domain models
2. A `DataModels` folder that contains DTOs (Normally this would be in different project.)
3. A `Maps` folder that contains the maps for the models and DTOs
4. A `Program.cs` file that configures and uses the `IMapper`.

The example domain object is a `Person` class. It contains a couple of properties as well as a
value object `Address`, which also contains some properties and another value object `ZipCode`.

The `Maps` folder contains the definitions for the mapping between the domain models and the
DTOs. Note that the `IMapper` interface is injected into the mapper. This allows you to map
more complex objects such as `Person` that in tern contains objects that need to be mapped.
39 changes: 39 additions & 0 deletions samples/SampleMapperApp/SampleMapperApp.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26124.0
MinimumVisualStudioVersion = 15.0.26124.0
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{593B8499-9DF8-41FC-9CFB-A0232413FA02}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SampleMapperApp.ConsoleApp", "src\SampleMapperApp.ConsoleApp\SampleMapperApp.ConsoleApp.csproj", "{24A13F15-49C6-4658-8757-B7A2CD4E291B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{24A13F15-49C6-4658-8757-B7A2CD4E291B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{24A13F15-49C6-4658-8757-B7A2CD4E291B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{24A13F15-49C6-4658-8757-B7A2CD4E291B}.Debug|x64.ActiveCfg = Debug|Any CPU
{24A13F15-49C6-4658-8757-B7A2CD4E291B}.Debug|x64.Build.0 = Debug|Any CPU
{24A13F15-49C6-4658-8757-B7A2CD4E291B}.Debug|x86.ActiveCfg = Debug|Any CPU
{24A13F15-49C6-4658-8757-B7A2CD4E291B}.Debug|x86.Build.0 = Debug|Any CPU
{24A13F15-49C6-4658-8757-B7A2CD4E291B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{24A13F15-49C6-4658-8757-B7A2CD4E291B}.Release|Any CPU.Build.0 = Release|Any CPU
{24A13F15-49C6-4658-8757-B7A2CD4E291B}.Release|x64.ActiveCfg = Release|Any CPU
{24A13F15-49C6-4658-8757-B7A2CD4E291B}.Release|x64.Build.0 = Release|Any CPU
{24A13F15-49C6-4658-8757-B7A2CD4E291B}.Release|x86.ActiveCfg = Release|Any CPU
{24A13F15-49C6-4658-8757-B7A2CD4E291B}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{24A13F15-49C6-4658-8757-B7A2CD4E291B} = {593B8499-9DF8-41FC-9CFB-A0232413FA02}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace SampleMapperApp.ConsoleApp.DataModels
{
public class AddressModel
{
public int PersonId { get; set; }

public string StreetNumber { get; set; }

public string StreetName { get; set; }

public string ZipCode { get; set; }

public string Country { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace SampleMapperApp.ConsoleApp.DataModels
{
public class PersonModel
{
public int Id { get; set; }

public string FirstName { get; set; }

public string LastName { get; set; }

public AddressModel AddressModel { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace SampleMapperApp.ConsoleApp.Domain
{
public class Address
{
public Address(string streetNumber, string streetName, ZipCode zipCode, string country)
{
// Validation ...
StreetNumber = streetNumber;
StreetName = streetName;
ZipCode = zipCode;
Country = country;
}

public string StreetNumber { get; }

public string StreetName { get; }

public ZipCode ZipCode { get; }

public string Country { get; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
namespace SampleMapperApp.ConsoleApp.Domain
{
public class Person
{
public Person(int id, string firstName, string lastName, Address address)
{
// Validation...
Id = id;
FirstName = firstName;
LastName = lastName;
Address = address;
}

public int Id { get; }

public string FirstName { get; }

public string LastName { get; }

public Address Address { get; }

public string FullName => $"{FirstName} {LastName}";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace SampleMapperApp.ConsoleApp.Domain
{
public class ZipCode
{
public ZipCode(string value)
{
// Validation...
Value = value;
}

public string Value { get; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using Mapr;
using SampleMapperApp.ConsoleApp.DataModels;
using SampleMapperApp.ConsoleApp.Domain;

namespace SampleMapperApp.ConsoleApp.Maps
{
public class AddressMap : IMap<(Address address, int personId), AddressModel>, IMap<AddressModel, Address>
{
private readonly IMapper _mapper;

public AddressMap(IMapper mapper)
{
_mapper = mapper;
}

public Address Map(AddressModel source)
{
var zipCode = _mapper.Map<string, ZipCode>(source.ZipCode);
return new Address(source.StreetNumber, source.StreetName, zipCode, source.Country);
}

public AddressModel Map((Address address, int personId) source)
{
var (address, personId) = source;
return new()
{
PersonId = personId,
StreetName = address.StreetName,
Country = address.Country,
StreetNumber = address.StreetNumber,
ZipCode = _mapper.Map<ZipCode, string>(address.ZipCode)
};
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Mapr;
using SampleMapperApp.ConsoleApp.DataModels;
using SampleMapperApp.ConsoleApp.Domain;

namespace SampleMapperApp.ConsoleApp.Maps
{
public class PersonMap : IMap<Person, PersonModel>, IMap<PersonModel, Person>
{
private readonly IMapper _mapper;

public PersonMap(IMapper mapper)
{
_mapper = mapper;
}

public PersonModel Map(Person source)
{
return new PersonModel
{
Id = source.Id,
FirstName = source.FirstName,
LastName = source.LastName,

// Calling the mapper to map the address to the model.
AddressModel = _mapper.Map<Address, AddressModel>(source.Address)
};
}

public Person Map(PersonModel source)
{
// Convert the address model back to an address.
var address = _mapper.Map<AddressModel, Address>(source.AddressModel);

return new Person(source.Id, source.FirstName, source.LastName, address);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Mapr;
using SampleMapperApp.ConsoleApp.Domain;

namespace SampleMapperApp.ConsoleApp.Maps
{
public class ZipCodeMap : IMap<ZipCode, string>, IMap<string, ZipCode>
{
public string Map(ZipCode source)
{
return source.Value;
}

public ZipCode Map(string source)
{
return new(source);
}
}
}
23 changes: 23 additions & 0 deletions samples/SampleMapperApp/src/SampleMapperApp.ConsoleApp/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using Mapr;
using Mapr.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
using SampleMapperApp.ConsoleApp.DataModels;
using SampleMapperApp.ConsoleApp.Domain;


var services = new ServiceCollection();

services.AddMapr(config =>
{
config.Scan(typeof(Person).Assembly);
});

var provider = services.BuildServiceProvider();

var mapper = provider.GetRequiredService<IMapper>();

var address = new Address("123", "Fake Street", new ZipCode("90120"), "USA");
var person = new Person(1, "John", "Smith", address);

var personModel = mapper.Map<Person, PersonModel>(person);
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Mapr" Version="0.2.0" />
<PackageReference Include="Mapr.DependencyInjection" Version="0.2.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="5.0.1" />
</ItemGroup>

</Project>
Loading

0 comments on commit 3715694

Please sign in to comment.