0

I'm having issues with returning objects from related domain models. The objects that are from other models are returning null.

What i am basically trying to accomplish is return an DTO that have the fields that i want from the related domain models instead of passing every field straight from the domain models to json.

Please see below code, can someone please advise.

SCREEN SHOT OF RESULT DATABASE DIAGRAM

## CourseDomainModels.cs ##

public class CourseDomainModel : IObjectWithState
{
    public int Id { get; set; }
    public string Name { get; set; }
    public Double Duration { get; set; }
    public string Description { get; set; }

    public virtual TutorDomainModel CourseTutor { get; set; }
    public virtual SubjectDomainModel CourseSubject { get; set; }

    public ICollection<EnrollmentDomainModel> Enrollments { get; set; }

    [NotMapped]
    public Common.State state { get; set; }

    [NotMapped]
    public bool InDb => this.Id != default(int);

    public object PersistenceEntityId => this.Id;
}


## TutorDomainModel.cs ##

public class TutorDomainModel : IObjectWithState
{
    public int Id { get; set; }
    public string Email { get; set; }
    public string UserName { get; set; }
    public string Password { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public Enums.Gender Gender { get; set; }

    public ICollection<CourseDomainModel> Courses;

    [NotMapped]
    public Common.State state { get; set; }

    [NotMapped]
    public bool InDb => this.Id != default(int);

    public object PersistenceEntityId => this.Id;
}

## CourseDTO.cs ##

public class CourseDTO
{
    public string Name { get; set; }

    public Double Duration { get; set; }

    public string Description { get; set; }

    public string Email { get; set; }

    public string FirstName { get; set; }

    public string LastName { get; set; }
}

## AutoMapperConfig.cs ##

public class AutoMapperConfig
{
    public static void RegisterMapping()
    {

        Mapper.CreateMap<CourseDomainModel, CourseDTO>();
    }
}

## Startup.cs ##

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        HttpConfiguration config = new HttpConfiguration();
        WebApiConfig.Register(config);
        app.UseWebApi(config);

        AutoMapperConfig.RegisterMapping();
    }
}

## CourseService.cs ##

public CourseDTO GetCourse(int id)
    {

        var course = _courseRepo.Get(id);
        CourseDTO courseView = Mapper.Map<CourseDomainModel,CourseDTO(course);

        return courseView;           
    }
Erkan Demir
  • 785
  • 1
  • 8
  • 24
  • There are no FirstName, LastName, Email properties on CourseDomainModel. That's why they can't be mapped. Any chance you mixed up the properties in this class? – Ufuk Hacıoğulları Oct 01 '15 at 06:27
  • Hey @UfukHacıoğulları FirstName, LastName, Email properties don't belong to CourseDomainModel, they below to TutorDomainModel. I would like to join these two models and return A DTO with all these fields – Erkan Demir Oct 01 '15 at 06:28
  • Hey @UfukHacıoğulları i edited the post and added the database diagram so you can see the relationship with the entities. – Erkan Demir Oct 01 '15 at 06:33
  • Besides the answer below, you can also get the mapping by convention. So if you changed Email to CourseTutorEmail in your DTO automapper can figure it out. – Steve Greene Oct 01 '15 at 15:03

2 Answers2

2

AutoMapper maps the properties of TSource to properties of TDestination, but it does not try to find properties of TDestination from child properties of TSource by default.

You can instruct AutoMapper to do so:

Mapper.CreateMap<CourseDomainModel, CourseDTO>()
    .ForMember(dest => dest.Email, opt => opt.MapFrom(src => src.CourseTutor == null ? string.Empty : src.CourseTutor.Email))
    .ForMember(dest => dest.FirstName, opt => opt.MapFrom(src => src.CourseTutor == null ? string.Empty : src.CourseTutor.FirstName))
    .ForMember(dest => dest.LastName, opt => opt.MapFrom(src => src.CourseTutor == null ? string.Empty : src.CourseTutor.LastName));

CourseDTO courseView = Mapper.Map<CourseDTO>(course);
Arghya C
  • 9,805
  • 2
  • 47
  • 66
1

AutoMapper is not AI yet, so you should explicitly specify custom member mappings:

Mapper.CreateMap<CourseDomainModel, CourseDTO>()
                .ForMember(dest => dest.Email, opt => opt.MapFrom(source => source.TutorDomainModel.Email));
Ilya Chumakov
  • 23,161
  • 9
  • 86
  • 114