Update sample project.
This commit is contained in:
parent
a54b15942a
commit
36161b728c
|
@ -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}";
|
||||
}
|
||||
}
|
|
@ -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; }
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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>
|
||||
|
|
|
@ -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; }
|
||||
}
|
||||
}
|
|
@ -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}";
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue