How can i make AutoMapper generate this code automatically?

780 Views Asked by At

I'm new using Automapper i searched a lot for a right answer but it seems i can't get it in my mind correctly so:

i have this block of code

    var user = usersDao.FindById(HttpContext.Current.User.Identity.GetUserId());
    var userDto = Mapper.Map<ApplicationUser, UserDto>(user);
        if (user.Student != null)
        {
         userDto.Account = Mapper.Map<Student, StudentDto>(user.Student);
        }
        if (user.Teacher != null)
        {
        userDto.Account = Mapper.Map<Teacher, TeacherDto>(user.Teacher);
        }
        userDto.Account.User = null;

so what i'm trying to do is i have this property in my UserDto class

AccountDto Account

both StudentDto and TeacherDto inherited From it so i want to make automapper to do self auto mapping to it, if it is from StudentDto or TeacherDto to Account

here is the classes

public class StudentDto : AccountDto
    {

    }
public class TeacherDto: AccountDto
    {

    }
public class AccountDto 
    {
        public UserDto User { get; set; }
        public string UserId { get; set; }

    }
public class UserDto 
{
    public AccountDto Account { get; set; }
}

is there a simple solution?

0

There are 0 best solutions below