public void TestComplexDtoTreeMapping() { //var test = MapperConfig.GetLambda<Participant, ParticipantDto>(new[] { "Department", "Department.Institution", "Department.Manikins", "Department.Manikins.CourseSlotScenarios", "ProfessionalRole.CourseParticipants" }); //Console.WriteLine(test); var test2 = MapperConfig.GetToDtoLambda <Course, CourseDto>(CurrentUser, new[] { "CourseParticipants", "CourseFormat.CourseSlots.Activity.ActivityChoices", "CourseSlotActivities", "CourseSlotPresenters", "CourseScenarioFacultyRoles", "CourseFormat.CourseType.Scenarios" }); Console.WriteLine(test2); using (MedSimDbContext db = new MedSimDbContext()) { var c = db.Courses.Select(test2).First(); /* * var c = db.Courses.Select(m => new CourseDto() * { * CourseParticipants = m.CourseParticipants.Select(cp => new CourseParticipantDto() * { * ParticipantId = cp.ParticipantId, * }).ToList(), * CourseFormat = new CourseFormatDto() * { * Id = m.CourseFormat.Id, * CourseSlots = m.CourseFormat.CourseSlots.Select(cs => new CourseSlotDto() * { * Id = cs.Id, * MinutesDuration = cs.MinutesDuration, * Day = cs.Day, * Activity = cs.Activity==null * ?null * :new CourseActivityDto() * { * Id = cs.Activity.Id, * ActivityChoices = cs.Activity.ActivityChoices.Select(at => new ActivityDto() * { * Id = m.Id, * }).ToList() * } * }).ToList() * } * }).First(); */ Console.Write(Newtonsoft.Json.JsonConvert.SerializeObject(c, Newtonsoft.Json.Formatting.Indented)); } }
void Remap(SaveResult result) { result.Entities = result.Entities.Select(o => { var treeTop = MapperConfig.GetToDtoLambda(MapperConfig.GetDtoType(o.GetType()), _currentUser); //possibly Assert(treeTop.WhereExpression.Compile().DynamicInvoke(o);) //possible bottleneck - could cast to all the different types return(treeTop.SelectExpression.Compile().DynamicInvoke(o)); }).ToList(); foreach (var k in result.DeletedKeys) { k.EntityTypeName = k.EntityTypeName.Replace(":#SP.DataAccess", "Dto:#SP.Dto"); } }
public void TestEntityMappingNavProperty() { const string propName = "Department"; using (MedSimDbContext db = new MedSimDbContext()) { var mapNavEx = MapperConfig.GetToDtoLambda <Participant, ParticipantDto>(CurrentUser, new[] { propName }); Console.WriteLine(mapNavEx.ToString()); var part = db.Users.Include(propName).AsNoTracking().First(p => p.UserName == testUserName); var partVm = db.Users.Select(mapNavEx).First(p => p.Email == part.Email); Assert.AreEqual(part.FullName, partVm.FullName); Assert.AreEqual(part.Department.Id, partVm.Department.Id); Assert.AreEqual(part.Department.InstitutionId, partVm.Department.InstitutionId); } }
public void TestEntityMappingCollection() { const string collectionPropName = "CourseParticipants"; using (MedSimDbContext db = new MedSimDbContext()) { var mapNavEx = MapperConfig.GetToDtoLambda <Participant, ParticipantDto>(CurrentUser, new[] { collectionPropName }); Console.WriteLine(mapNavEx.ToString()); var part = db.Users.Include(collectionPropName).AsNoTracking().First(p => p.UserName == testUserName); var partVm = db.Users.Select(mapNavEx).First(p => p.Email == part.Email); Assert.AreEqual(part.FullName, partVm.FullName); var cp = part.CourseParticipants.First(); var cpvm = partVm.CourseParticipants.First(); Assert.AreEqual(cp.ParticipantId, cpvm.ParticipantId); Assert.AreEqual(cp.CourseId, cpvm.CourseId); } }