Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Create Master Detail DTO #390


User avatar
0
behiunforgiven created

How Can I Create a DTO from these classes :

public class RegisterRequest : Entity
    {
        public int? UnitId { get; set; }
       .
       .
       .
       .
       public virtual Unit Unit { get; set; }
    }

and

public class Unit : Entity
    {
        public Unit()
        {
            RegisterRequests = new HashSet<RegisterRequest>();
        }
       public string UnitName { get; set; }
       .
       .
       .
       public virtual ICollection<RegisterRequest> RegisterRequests { get; set; }
    }

that contains whole "RegisterRequest" class and "UnitName" of "Unit" class.

thank you


4 Answer(s)
  • User Avatar
    0
    hikalkan created
    Support Team

    Hi,

    You can create RegisterRequestDto and UnitDto and relate these similar to your entities. Doesn't it work?

  • User Avatar
    0
    behiunforgiven created

    Thanx for your answer but, No, it doesn't work

    I Can add "UnitName" to My DTO by and fill it by a foreach loop.

    But I want to use AutoMapper.

  • User Avatar
    0
    behiunforgiven created

    I use this code to solve this:

    Mapper.CreateMap<RegisterRequest, RegisterRequestOutDto>()
                    .ForMember(dest => dest.UnitName, opt =>
                        opt.MapFrom(src => src.Unit.UnitName))
    
  • User Avatar
    0
    hikalkan created
    Support Team

    If you define UnitUnitName to your RegisterRequestDto, AutoMapper automatically fills it by convention (see automapper docs to understand why). Surely, "UnitUnitName" is not a good naming, but this comes another naming problem. UnitName should be Name in Unit class as a better practice. In that case, you can add UnitName to your RegisterRequestDto.