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 class User
{ {
public int Id { get; set; } public int Id { get; set; }
public string FirstName { get; set; } public DateTimeOffset RegisteredAt { get; set; }
public string LastName { get; set; } public Profile Profile { get; set; }
public string FullName => $"{FirstName} {LastName}";
public long Key { get; }
} }
} }

View File

@ -1,4 +1,6 @@
using TestConsoleApp.ViewModels; using System;
using TestConsoleApp.Data.Models;
using TestConsoleApp.ViewModels;
namespace TestConsoleApp namespace TestConsoleApp
{ {
@ -6,8 +8,23 @@ namespace TestConsoleApp
{ {
private static void Main(string[] args) private static void Main(string[] args)
{ {
var userViewModel = User.From(new Data.Models.User()); var user = new User
var userViewModel2 = new Data.Models.User().ToUserViewModel(); {
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> <PropertyGroup>
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework> <TargetFramework>netcoreapp3.1</TargetFramework>
<LangVersion>8</LangVersion> <LangVersion>7.3</LangVersion>
<Nullable>annotations</Nullable> <Nullable>disable</Nullable>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>

View File

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

View File

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