How to properly define AutoMapper config for complex class? - c#

I'm working with Entity Framework, and getting ready to redo a project that is currently using EF->BDO->DTO->client with manual conversions for each class along the way.
I came across AutoMapper and think this would be a better solution overall. I have had no issues mapping CustomerEF -> CustomerDto, OfficeEF -> OfficeDto, etc, but I am now working on a more complex class, and struggling to get everything in place.
I feel I am close, and that something has to happen in reverse, but have not been able to identify what I'm missing.
public class CaseDto
{
public int CaseId { get; set; }
public string CaseReason { get; set; }
public CustomersDto _customer { get; set; }
public OfficeDto _office { get; set; }
public CaseNotesDto[] _caseNotes { get; set; }
}
public class CustomersDto
{
public int CustomerId { get; set; }
public string CustomerName { get; set; }
}
public class OfficeDto
{
public int OfficeId { get; set; }
public string OfficeName { get; set; }
}
public class CaseNotesDto
{
public int CaseNotesId { get; set; }
public int CaseId { get; set; }
public string CaseNote { get; set; }
}
// EF objects
public class CaseEF
{
public int CaseId { get; set; }
public string CaseReason { get; set; }
public int CustomerId { get; set; }
public int OfficeId { get; set; }
}
public class CustomerEF
{
public int CustomerId { get; set; }
public string CustomerName { get; set; }
}
public class OfficeEF
{
public int OfficeId { get; set; }
public string OfficeName { get; set; }
}
public class CaseNotesEF
{
public int CaseNotesId { get; set; }
public int CaseId { get; set; }
public string CaseNote { get; set; }
}
// execution classes
public class CaseFramework
{
// set 'ef' variables
private readonly OfficeEF _officeEf = new OfficeEF { OfficeId = 1, OfficeName = "Washington" };
private readonly CustomerEF _customerEf = new CustomerEF { CustomerId = 1, CustomerName = "Blips and Chitz" };
private readonly CaseNotesEF[] _caseNotesEf =
{
new CaseNotesEF {CaseNotesId = 1, CaseNote = "Case 1", CaseId = 1000},
new CaseNotesEF {CaseNotesId = 2, CaseNote = "Case 2", CaseId = 1000}
};
private readonly CaseEF _case =
new CaseEF { CaseId = 1000, CaseReason = "Roy is back!", CustomerId = 1, OfficeId = 1 };
public CaseDto GetCase(int caseId)
{
Mapper.Initialize(cfg =>
{
cfg.CreateMap<OfficeEF, OfficeDto>();
cfg.CreateMap<CustomerEF, CustomersDto>();
cfg.CreateMap<CaseNotesEF, CaseNotesDto>();
cfg.CreateMap<CaseEF, CaseDto>()
.ForMember(dest => dest._customer, opt => opt.MapFrom(src => src.CustomerId))
.ForMember(dest => dest._office, opt => opt.MapFrom(src => src.OfficeId))
.ForMember(dest => dest._caseNotes,
opt => opt.MapFrom(src => _caseNotesEf.Where(x => x.CaseId == caseId)));
});
Mapper.AssertConfigurationIsValid();
var source = new CaseEF { CaseId = caseId };
var destination = Mapper.Map<CaseEF, CaseDto>(source);
return destination;
}
}
To run this I am doing:
var b = new CaseFramework();
var result = b.GetCase(1000);
The results are populating the CaseId (set manually) and the CaseNotesDto, but nothing else.
Having the first 3 cfg.CreateMap items in the Mapper.Initialize section makes no difference if they are there or not.
Thanks in advance for any guidance.

Appreciate the responses.
#MickyD, I was a little confused on the POCO comment, as the EF already generated a tt, and the Canonical schema/model looks like something I'll have to research more.
#Steve I checked my navigation properties from my EF database and we need to do some fixes to the relationships before making the Include method work, but I think that will ultimately be the solution. In the meantime I was able to accomplish the original goal with this:
Mapper.AssertConfigurationIsValid();
var destination = Mapper.Map<CaseDto>(_case);
destination.Customers = Mapper.Map<CustomerEF, CustomersDto>(_customerEf);
destination.Office = Mapper.Map<OfficeEF, OfficeDto>(_officeEf);
return destination;
Thanks.

Related

AutoMapper MapExpression for sub-entity fails

I'm mapping select expression (projection) of Linq query. This is done to decouple logic layer from data access layer and logic layer should use only DTOs.
Expression<Func<CountyInfoDto, CountyInfoDto>> selector = c =>
new CountyInfoDto
{
Id = c.Id,
Citizens = c.Citizens.Select(p => new CitizenDto
{
}).ToList()
};
var resEx = mapper.MapExpression<Expression<Func<CountyInfo, CountyInfoDto>>>(selector);
This mapping fails with error Expression of type 'DTOs.CitizenDto' cannot be used for return type 'Entities.Citizen' however in CountyInfoDto property Citizens has type CitizenDto. Please note all mapping profiles are valid and simple objects can be mapped properly.
If I do like this, all works:
Expression<Func<CountyInfoDto, CountyInfoDto>> selector = c =>
new CountyInfoDto
{
Id = c.Id
};
var resEx = mapper.MapExpression<Expression<Func<CountyInfo, CountyInfoDto>>>(selector);
or this also works:
Expression<Func<CountyInfoDto, CountyInfoDto>> selector = c =>
new CountyInfoDto
{
Id = c.Id,
Citizens = new List<CitizenDto>
{
new CitizenDto
{
Id = c.Citizens.First().Id
}
}
};
var resEx = mapper.MapExpression<Expression<Func<CountyInfo, CountyInfoDto>>>(selector);
is there any possibility to avoid this error?
Classes:
public class CountyInfo
{
public CountyInfo()
{
Citizens = new HashSet<Citizen>();
}
public Guid Id { get; set; }
public string Name { get; set; }
public ICollection<Citizen> Citizens { get; set; }
}
public class Citizen
{
public Guid Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string ZipCode { get; set; }
}
public class CountyInfoDto
{
public CountyInfoDto()
{
Citizens = new List<CitizenDto>();
}
public Guid Id { get; set; }
public string Name { get; set; }
public List<CitizenDto> Citizens { get; set; }
}
public class CitizenDto
{
public Guid Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string ZipCode { get; set; }
}
Mappings:
CreateMap<CountyInfo, CountyInfoDto>().ReverseMap();
CreateMap<Citizen, CitizenDto>().ReverseMap();
I'm using AutoMapper.Extensions.ExpressionMapping, after update to latest version error is: No coercion operator is defined between types 'Entities.CountyInfo' and 'DTOs.CountyInfoDto'.

Automapper nested Collections without setter

I have this code snippet running on LinqPad (C# program) with Automapper Nuget package 6.1.1 already included:
void Main()
{
Mapper.Initialize(cfg =>
{
cfg.CreateMap<Top, TopDto>().ReverseMap();
});
Mapper.AssertConfigurationIsValid();
var source = new TopDto
{
Id = 1,
Name = "Charlie",
Nicks = new List<string> { "Fernandez", "Others" }
};
var destination = Mapper.Map<Top>(source);
destination.Dump();
}
// Define other methods and classes here
public class Top
{
public Top()
{
Nicks = new List<string>();
}
public int Id { get; set; }
public string Name { get; set; }
public ICollection<string> Nicks { get; }
}
public class TopDto
{
public TopDto()
{
Nicks = new List<string>();
}
public int Id { get; set; }
public string Name { get; set; }
public ICollection<string> Nicks { get; set; }
}
And as you can see we have problems setting the nested Collection (without Setter at all). In theory this should be running fine but it is not adding any element to the Collection.
If we change the collection property adding a public setter, then all is fine.
How can I get a nested collection without adding a public setter or a setter at all?
Thanks to #LucianBargaoanu (in the comments) this is solved now, in this way:
void Main()
{
Mapper.Initialize(cfg =>
{
cfg.CreateMap<Top, TopDto>().ReverseMap()
.ForMember(d => d.Nicks, o=>
{
o.MapFrom(s => s.Nicks);
o.UseDestinationValue();
});
});
Mapper.AssertConfigurationIsValid();
var source = new TopDto(new List<string> { "Fernandez", "Others" })
{
Id = 1,
Name = "Charlie"
};
var destination = Mapper.Map<Top>(source);
destination.Dump();
}
// Define other methods and classes here
public class Top
{
public Top()
{
Nicks = new List<string>();
}
public int Id { get; set; }
public string Name { get; set; }
public ICollection<string> Nicks { get; }
}
public class TopDto
{
public TopDto(List<string> nicks)
{
Nicks = nicks;
}
public int Id { get; set; }
public string Name { get; set; }
public ICollection<string> Nicks { get; private set; }
}
Regards.

How to join two tables with linq?

I am trying to join two of my tables with linq based on an id, so far unseccesfully.
Here is how my models look :
public class WorkRole
{
public int WorkRoleId { get; set; }
public string RoleName { get; set; }
public string RoleDescription { get; set; }
public int CompanyId { get; set; }
public virtual Company Company { get; set; }
public virtual ICollection<WorkRolesUsersDetails> WorkRolesUsersDetails { get; set; }
}
public class WorkRolesUsersDetails
{
public int WRUDId { get; set; }
public int? WorkRoleId { get; set; }
public string UserDetailsId { get; set; }
public virtual WorkRole WorkRole { get; set; }
public virtual UserDetails UserDetails { get; set; }
public DateTime FocusStart { get; set; }
public DateTime FocusEnd { get; set; }
public bool isActive { get; set; }
}
I am trying to get in one view WorkRoleId, RoleName, RoleDescription and CompanyId from the first table and UserDetailsId, FocusStart, FocusEnd and isActive from the second table.
The farthest i got with my ideas was :
var query = db.WorkRoles.Join(db.WorkRolesUsersDetails,x => x.WorkRoleId,y => y.WorkRoleId,(x, y) => new { wr = x, wrud = y });
But sadly, it didn't work. I just don't know enough linq and couldn't get much out of other questions/answers here. Please, help.
Code for joining 2 tables is:
var list = db.WorkRoles.
Join(db.WorkRolesUsersDetails,
o => o.WorkRoleId, od => od.WorkRoleId,
(o, od) => new
{
WorkRoleId= o.WorkRoleId
RoleName= o.RoleName,
RoleDescription= o.RoleDescription,
CompanyId= o.CompanyId,
WRUDId= od.WRUDId,
UserDetailsId= od.UserDetailsId,
FocusStart=od.FocusStart,
FocusEnd=od.FocusEnd
})
If you are using EF may I suggest the Includes statement it works wonders. IF you have a foreign key assigned. It basically gets the other data with it.
static void Main(string[] args)
{
using (var context = new TesterEntities())
{
var peopleOrders = context.tePerson.Include("teOrder").First(p => p.PersonId == 1).teOrder.ToList();
peopleOrders.ForEach(x => Console.WriteLine($"{x.OrderId} {x.Description}"));
}
}
Combining manually without navigation context option.
public class Student
{
public int StudentID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public List<StudentTestScore> Scores { get; set; }
}
public class StudentTestScore
{
public int StudentID { get; set; }
public int Score { get; set; }
}
class Program
{
static void Main(string[] args)
{
var students = new List<Student>
{
new Student { StudentID = 1, FirstName = "Brett", LastName = "X" },
new Student { StudentID = 2, FirstName = "John", LastName = "X" }
};
var grades = new List<StudentTestScore> { new StudentTestScore { StudentID = 1, Score = 98 } };
var combined = students.Join(grades, x => x.StudentID, y => y.StudentID,
(x, y) => new
{
Student = $"{x.FirstName} {x.LastName}",
Grade = y.Score
}).ToList();
combined.ForEach(x => Console.WriteLine($"{x.Student} {x.Grade}"));
Console.ReadLine();
}

Name convention in Automapper

I know that Automapper can automatically map from:
class SomeClassA
{
public int Id { get; set; }
public B Member { get; set; }
}
class SomeClassB
{
public int Id { get; set; }
public string Name { get; set; }
}
to:
class SomeClassADto
{
public int Id { get; set; }
public int Member_Id { get; set; }
public string Member_Name { get; set; }
}
But how can I make Automapper map from my SomeClassADto to SomeClassA automatically?
No the prettiest thing in the world, but it does work. Downside is maintenance is a pain as you add properties to the DTO and class you need to update the mapping.
var config = new MapperConfiguration(x =>
{
x.CreateMap<SomeClassADto, SomeClassB>()
.ForMember(i => i.Id, i => i.MapFrom(src => src.Member_Id))
.ForMember(i => i.Name, i => i.MapFrom(src => src.Member_Name));
x.CreateMap<SomeClassADto, SomeClassA>()
.AfterMap((s, d, r) => d.Member = r.Mapper.Map<SomeClassB>(s));
});
IMapper mapper = config.CreateMapper();
var foo = mapper.Map<SomeClassA>(new SomeClassADto() { Id = 1, Member_Id = 2, Member_Name = "Name" });

Collections duplicated when trying to update a detached entity's related collection

I have two API calls. GetExam and SaveExam. GetExam serializes to JSON which means by the time I go to save, the entity is detached. This isnt a problem, I can go retrieve the entity by its primary key and update its properties manually.
However, when I do so the exam questions get its current collection duplicated. For example, if examToSave.ExamQuestions had a few questions deleted, and a new one added all selectedExam.exam_question are duplicated and the new one is added in. Eg. if 3 questions existed, I deleted 1 and added 4 there will now be 7.
Domain models:
public partial class exam
{
public exam()
{
this.exam_question = new HashSet<exam_question>();
}
public int ID { get; set; }
public string ExamName { get; set; }
public string ExamDesc { get; set; }
public Nullable<decimal> TimeToComplete { get; set; }
public bool AllowBackStep { get; set; }
public bool RandomizeAnswerOrder { get; set; }
public int Attempts { get; set; }
public virtual ICollection<exam_question> exam_question { get; set; }
}
public partial class exam_question
{
public exam_question()
{
this.exam_answer = new HashSet<exam_answer>();
}
public int ID { get; set; }
public int ExamID { get; set; }
public string QuestionText { get; set; }
public bool IsFreeForm { get; set; }
public virtual exam exam { get; set; }
public virtual ICollection<exam_answer> exam_answer { get; set; }
}
public partial class exam_answer
{
public int ID { get; set; }
public string AnswerText { get; set; }
public int QuestionID { get; set; }
public bool IsCorrect { get; set; }
public virtual exam_question exam_question { get; set; }
}
Save method:
[Route("SaveExam")]
[HttpPost]
public IHttpActionResult SaveExam(ExamViewModel examToSave)
{
using (var db = new IntranetEntities())
{
// try to locate the desired exam to update
var selectedExam = db.exams.Where(w => w.ID == examToSave.ID).SingleOrDefault();
if (selectedExam == null)
{
return NotFound();
}
// Redacted business logic
// Map the viewmodel to the domain model
Mapper.CreateMap<ExamAnswerViewModel, exam_answer>();
Mapper.CreateMap<ExamQuestionViewModel, exam_question>().ForMember(dest => dest.exam_answer, opt => opt.MapFrom(src => src.QuestionAnswers));
Mapper.CreateMap<ExamViewModel, exam>().ForMember(dest => dest.exam_question, opt => opt.MapFrom(src => src.ExamQuestions));
var viewmodel = Mapper.Map<exam>(examToSave);
// Update exam properties
selectedExam.ExamName = viewmodel.ExamName;
selectedExam.ExamDesc = viewmodel.ExamDesc;
selectedExam.AllowBackStep = viewmodel.AllowBackStep;
selectedExam.Attempts = viewmodel.Attempts;
selectedExam.RandomizeAnswerOrder = viewmodel.RandomizeAnswerOrder;
selectedExam.exam_question = viewmodel.exam_question; // DUPLICATES PROPS
// Save
db.SaveChanges();
return Ok(examToSave);
}
}

Categories

Resources