Update sample project.

This commit is contained in:
Mohammadreza Taikandi 2021-02-07 10:51:49 +00:00
parent a54b15942a
commit 36161b728c
6 changed files with 56 additions and 29 deletions

View File

@ -0,0 +1,11 @@
namespace TestConsoleApp.Data.Models
{
public class Profile
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string FullName => $"{FirstName} {LastName}";
}
}

View File

@ -1,15 +1,13 @@
namespace TestConsoleApp.Data.Models
using System;
namespace TestConsoleApp.Data.Models
{
public class User
{
public int Id { get; set; }
public string FirstName { get; set; }
public DateTimeOffset RegisteredAt { get; set; }
public string LastName { get; set; }
public string FullName => $"{FirstName} {LastName}";
public long Key { get; }
public Profile Profile { get; set; }
}
}

View File

@ -1,4 +1,6 @@
using TestConsoleApp.ViewModels;
using System;
using TestConsoleApp.Data.Models;
using TestConsoleApp.ViewModels;
namespace TestConsoleApp
{
@ -6,8 +8,23 @@ namespace TestConsoleApp
{
private static void Main(string[] args)
{
var userViewModel = User.From(new Data.Models.User());
var userViewModel2 = new Data.Models.User().ToUserViewModel();
var user = new User
{
Id = 1234,
RegisteredAt = DateTimeOffset.Now,
Profile = new Profile
{
FirstName = "John",
LastName = "Doe"
}
};
var vm = user.ToUserViewModel();
Console.WriteLine("Key: {0}", vm.Key);
Console.WriteLine("RegisteredAt: {0}", vm.RegisteredAt);
Console.WriteLine("FirstName: {0}", vm.Profile.FirstName);
Console.WriteLine("LastName: {0}", vm.Profile.LastName);
}
}
}

View File

@ -3,8 +3,8 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<LangVersion>8</LangVersion>
<Nullable>annotations</Nullable>
<LangVersion>7.3</LangVersion>
<Nullable>disable</Nullable>
</PropertyGroup>
<ItemGroup>

View File

@ -1,14 +1,13 @@
// using MapTo;
using MapTo;
using MapTo;
using TestConsoleApp.Data.Models;
namespace TestConsoleApp.ViewModels
{
[MapFrom(typeof(Data.Models.User))]
public partial class User
[MapFrom(typeof(Profile))]
public partial class ProfileViewModel
{
public string FirstName { get; }
public string LastName { get; }
}
}

View File

@ -1,22 +1,24 @@
using MapTo;
using System;
using MapTo;
using TestConsoleApp.Data.Models;
namespace TestConsoleApp.ViewModels
{
[MapFrom(typeof(Data.Models.User))]
[MapFrom(typeof(User))]
public partial class UserViewModel
{
public string FirstName { get; }
[IgnoreProperty]
public string LastName { get; }
[MapTypeConverter(typeof(LastNameConverter))]
[MapProperty(SourcePropertyName = nameof(User.Id))]
[MapTypeConverter(typeof(IdConverter))]
public string Key { get; }
private class LastNameConverter : ITypeConverter<long, string>
public DateTimeOffset RegisteredAt { get; set; }
// [IgnoreProperty]
public ProfileViewModel Profile { get; set; }
private class IdConverter : ITypeConverter<int, string>
{
/// <inheritdoc />
public string Convert(long source, object[]? converterParameters) => $"{source} :: With Type Converter";
public string Convert(int source, object[] converterParameters) => $"{source:X}";
}
}
}