AutoMapper Custom Projections

AutoMapper projections in a simple way

Assume we have 2 classes

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

    public string FirstName { get; set; }

    public string LastName { get; set; }

    public string Email { get; set; }

    public Address Address { get; set; }

    public string GetFullName()
    {
        return $"{LastName}, {FirstName}";
    }
}
public class Address
{
    public int Id { get; set; }

    public string Door { get; set; }

    public string Street1 { get; set; }

    public string Street2 { get; set; }

    public string City { get; set; }

    public string State { get; set; }

    public string Country { get; set; }

    public string ZipCode { get; set; }
}        

And at the end, the destination object that will be shown in the UI:

public class UserViewModel
{
    [Display(Name = "Full Name")]
    public string FullName { get; set; }

    [Display(Name = "Country")]
    public string AddressCountry { get; set; }

    public string Email { get; set; }
}        

As you can see, in the current view model, we are applying one of the naming conventions for the automatic mapping by naming the property?AddressCountry. That means that the AutoMapper will search for a child object named?Address?and its property named?Country.

要查看或添加评论,请登录

mohamed tayel的更多文章

  • required key word

    required key word

    C# .NET Feature Highlight: Exploring the required Keyword! ??? ???? ????? ????? ?? C# ?? ??????? ????? ?? .

    1 条评论
  • ??????? ?????? ????????? ??????? ????? ???

    ??????? ?????? ????????? ??????? ????? ???

    ????? ????? ?????? ????????? ????? ?? ???? ?????????? ???? ????? ??? ????? ???????. ??? ?????? ????? ?? ????? ?????????…

    2 条评论
  • ??????? ?? ???? ???????

    ??????? ?? ???? ???????

    ?????? ????? ? ???? ???? ??????? ??? ???? ???? ????? ??? ????? ??????? ?? ???? ??????? ??? ??? ??? ? ??? ???? ?????…

    2 条评论
  • Prepare for Interviews Part 2

    Prepare for Interviews Part 2

    Don't just describe your responsibilities, we want to show off what we've actually done for a company like a backend…

  • Prepare for Interviews Part1

    Prepare for Interviews Part1

    ?A brief document that someone will glance over it about 10 seconds and decide if you are worth talking ???????…

    7 条评论
  • Oracle DBA Position

    Oracle DBA Position

    Responsible Application performance investigation and subsequent tuning of database, SQL and PL/SQL The development of…

  • ???? ???? ?????? ?? ??? HR ??? ???? ??? ?????? ?? ??????? Mail ?????? ?????!

    ???? ???? ?????? ?? ??? HR ??? ???? ??? ?????? ?? ??????? Mail ?????? ?????!

    ????? ???? ???? ?????? ?? ??? HR ??? ???? ??? ?????? ?? ??????? Mail ?????? ?????! ? ?? ?? ??????? ???? ???????? ??…

    2 条评论

社区洞察

其他会员也浏览了