Skip to content

Commit

Permalink
Merge pull request #13 from rena0157/updating-samples
Browse files Browse the repository at this point in the history
Updating Samples
  • Loading branch information
rena0157 authored Dec 7, 2021
2 parents 791c344 + 44c7023 commit dfbb2da
Show file tree
Hide file tree
Showing 10 changed files with 121 additions and 120 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
namespace SampleMapperApp.ConsoleApp.DataModels
namespace SampleMapperApp.ConsoleApp.DataModels;

public class AddressModel
{
public class AddressModel
{
public int PersonId { get; set; }
public int PersonId { get; set; }

public string StreetNumber { get; set; }
public string StreetNumber { get; set; }

public string StreetName { get; set; }
public string StreetName { get; set; }

public string ZipCode { get; set; }
public string ZipCode { get; set; }

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

public class PersonModel
{
public class PersonModel
{
public int Id { get; set; }
public int Id { get; set; }

public string FirstName { get; set; }
public string FirstName { get; set; }

public string LastName { get; set; }
public string LastName { get; set; }

public AddressModel AddressModel { get; set; }
}
public AddressModel AddressModel { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
namespace SampleMapperApp.ConsoleApp.Domain
namespace SampleMapperApp.ConsoleApp.Domain;

public class Address
{
public class Address
public Address(string streetNumber, string streetName, ZipCode zipCode, string country)
{
public Address(string streetNumber, string streetName, ZipCode zipCode, string country)
{
// Validation ...
StreetNumber = streetNumber;
StreetName = streetName;
ZipCode = zipCode;
Country = country;
}
// Validation ...
StreetNumber = streetNumber;
StreetName = streetName;
ZipCode = zipCode;
Country = country;
}

public string StreetNumber { get; }
public string StreetNumber { get; }

public string StreetName { get; }
public string StreetName { get; }

public ZipCode ZipCode { get; }
public ZipCode ZipCode { get; }

public string Country { get; }
}
public string Country { get; }
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
namespace SampleMapperApp.ConsoleApp.Domain
namespace SampleMapperApp.ConsoleApp.Domain;

public class Person
{
public class Person
public Person(int id, string firstName, string lastName, Address address)
{
public Person(int id, string firstName, string lastName, Address address)
{
// Validation...
Id = id;
FirstName = firstName;
LastName = lastName;
Address = address;
}
// Validation...
Id = id;
FirstName = firstName;
LastName = lastName;
Address = address;
}

public int Id { get; }
public int Id { get; }

public string FirstName { get; }
public string FirstName { get; }

public string LastName { get; }
public string LastName { get; }

public Address Address { get; }
public Address Address { get; }

public string FullName => $"{FirstName} {LastName}";
}
public string FullName => $"{FirstName} {LastName}";
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
namespace SampleMapperApp.ConsoleApp.Domain
namespace SampleMapperApp.ConsoleApp.Domain;

public class ZipCode
{
public class ZipCode
public ZipCode(string value)
{
public ZipCode(string value)
{
// Validation...
Value = value;
}

public string Value { get; }
// Validation...
Value = value;
}

public string Value { get; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,33 @@
using SampleMapperApp.ConsoleApp.DataModels;
using SampleMapperApp.ConsoleApp.Domain;

namespace SampleMapperApp.ConsoleApp.Maps
namespace SampleMapperApp.ConsoleApp.Maps;

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

public AddressMap(IMapper mapper)
{
_mapper = 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 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)
public AddressModel Map((Address address, int personId) source)
{
var (address, personId) = source;
return new()
{
var (address, personId) = source;
return new()
{
PersonId = personId,
StreetName = address.StreetName,
Country = address.Country,
StreetNumber = address.StreetNumber,
ZipCode = _mapper.Map<ZipCode, string>(address.ZipCode)
};
}
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
Expand Up @@ -2,36 +2,35 @@
using SampleMapperApp.ConsoleApp.DataModels;
using SampleMapperApp.ConsoleApp.Domain;

namespace SampleMapperApp.ConsoleApp.Maps
namespace SampleMapperApp.ConsoleApp.Maps;

public class PersonMap : IMap<Person, PersonModel>, IMap<PersonModel, Person>
{
public class PersonMap : IMap<Person, PersonModel>, IMap<PersonModel, Person>
{
private readonly IMapper _mapper;
private readonly IMapper _mapper;

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

public PersonModel Map(Person source)
public PersonModel Map(Person source)
{
return new PersonModel
{
return new PersonModel
{
Id = source.Id,
FirstName = source.FirstName,
LastName = source.LastName,
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)
};
}
// Calling the mapper to map the address to the model.
AddressModel = _mapper.Map<(Address, int), AddressModel>((source.Address, source.Id))
};
}

public Person Map(PersonModel source)
{
// Convert the address model back to an address.
var address = _mapper.Map<AddressModel, Address>(source.AddressModel);
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);
}
return new Person(source.Id, source.FirstName, source.LastName, address);
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
using Mapr;
using SampleMapperApp.ConsoleApp.Domain;

namespace SampleMapperApp.ConsoleApp.Maps
namespace SampleMapperApp.ConsoleApp.Maps;

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

public ZipCode Map(string source)
{
return new(source);
}
public ZipCode Map(string source)
{
return new(source);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
using SampleMapperApp.ConsoleApp.DataModels;
using SampleMapperApp.ConsoleApp.Domain;


// Create Services
var services = new ServiceCollection();

// Add Mapr
services.AddMapr(config =>
{
// Use the Scan method to scan assemblies for types that implement IMap
config.Scan(typeof(Person).Assembly);
});

Expand All @@ -20,4 +22,11 @@
var address = new Address("123", "Fake Street", new ZipCode("90120"), "USA");
var person = new Person(1, "John", "Smith", address);

// Map the Person to a Person Model
var personModel = mapper.Map<Person, PersonModel>(person);

Console.WriteLine($"Id {{original : mapped}}: {person.Id} : {personModel.Id}");
Console.WriteLine($"First Name {{original : mapped}}: {person.FirstName} : {personModel.FirstName}");
Console.WriteLine($"Last Name {{original : mapped}}: {person.LastName} : {personModel.LastName}");

// And so on...
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net6.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" />
<PackageReference Include="Mapr" Version="1.0.0-alpha0001" />
<PackageReference Include="Mapr.DependencyInjection" Version="1.0.0-alpha0001" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
</ItemGroup>

</Project>

0 comments on commit dfbb2da

Please sign in to comment.