Automapper projection and union - c#

I have a problem with union and automapper projections.
I have two entities:
public class Entity
{
public DateTime ActionDate { get; set; }
public int SomeProp { get; set; }
}
public class ExtendedEntity
{
public DateTime ActionDate { get; set; }
public int SomeProp { get; set; }
public int SomeOtherProp { get; set; }
}
and projection:
public class EntityProjection
{
public DateTime ActionDate { get; set; }
public int SomeProp { get; set; }
public int SomeOtherProp { get; set; }
public string Source { get; set; }
}
i map entities to one projection, Entity does not have SomeOtherProp so i set 0 to it:
public class EntityProfile : Profile
{
protected override void Configure()
{
CreateMap<ExtendedEntity, EntityProjection>()
.ForMember(dest => dest.Source, opt => opt.MapFrom(src => "ext entity"));
CreateMap<Entity, EntityProjection>()
.ForMember(dest => dest.SomeOtherProp, opt => opt.MapFrom(src => 0))
.ForMember(dest => dest.Source, opt => opt.MapFrom(src => "entity"));
}
}
when i try to use next code i get error:
var entities = context.Set<Entity>()
.Project().To<EntityProjection>();
var extEntities = context.Set<ExtendedEntity>()
.Project().To<EntityProjection>();
var result = entities.Union(extEntities).OrderBy(p => p.ActionDate).ToList();
Error text: The type 'UserQuery+EntityProjection' appears in two structurally incompatible initializations within a single LINQ to Entities query. A type can be...
That means that properties in projection must be initialized in same order, how i can set projection properties initialization order by automapper?

Very late answer, and the short version seems to be "You can't".
I had exactly the same question (Can I force Automapper to initialise properties in a certain order?) and ended up mapping everything within a LINQ select statement.
For ease, I made it a static method within my DTO (cut-down code):
public static IQueryable<MyDto> QueryableFromTaskType1(
IQueryable<TaskType1> query)
{
return query.Select(src => new MyDto()
{
TaskId = src.Id,
AssetTypeName = src.Asset.AssetType.Name,
AssetId = src.Asset.Id,
AssetCode = src.Asset.Code,
AssetName = src.Asset.Name,
});
}
public static IQueryable<MyDto> QueryableFromTaskType2(
IQueryable<TaskType2> query)
{
return query.Select(src => new MyDto()
{
TaskId = src.Id,
AssetTypeName = src.AssetTypeName,
AssetId = src.AssetId,
AssetCode = src.AssetCode,
AssetName = src.AssetName,
});
}
then you can get your objects, as an IQueryable, simply pass them through the appropriate static method (which appends a select into the DTO - or projects as it's otherwise known) and then Union or Concat the resulting IQueryables.
The only downside is that Automapper will normally deal with recursive automapping, although I'm pretty certain that wouldn't map to SQL well anyway, so you probably don't lose much.

Related

Why result of IQueryable type converts to empty object?

public IHttpActionResult GetAllCollections(CollectionsDTO collectionsDTO)
{
if (!ModelState.IsValid)
return BadRequest();
try
{
var collectionsSectionRolesFlatDTO = (from c in db.Collections
join sr in db.SectionRole
on c.SectionRoleId equals sr.Id
select new CollectionsSectionRolesFlatDTO
{
Collections = new CollectionsDTO
{
CollectionTitleAr = c.CollectionTitleAr,
CollectionTitleEn = c.CollectionTitleEn,
CoverImagePath = c.CoverImagePath,
SectionRoleId = c.SectionRoleId,
},
SectionRole = new SectionRoleDto
{
NameAr = sr.NameAr,
NameEn = sr.NameEn
}
})
.AsQueryable();
var collectionsDto = Mapper.Map<List<CollectionsSectionRolesDTO>>(collectionsSectionRolesFlatDTO);
return Ok(collectionsDto);
}
catch (Exception ex)
{
return BadRequest("GetAllCollection: "+ ex.ToString());
}
}
DTOs:
public class CollectionsDTO
{
public int Id { get; set; }
public string CollectionTitleEn { get; set; }
public string CollectionTitleAr { get; set; }
public string CoverImagePath { get; set; }
public int? SectionRoleId { get; set; }
}
public class CollectionsSectionRolesFlatDTO
{
public SectionRoleDto SectionRole { get; set; }
public CollectionsDTO Collections { get; set; }
}
public class CollectionsSectionRolesDTO
{
public int Id { get; set; }
public string CollectionTitleEn { get; set; }
public string CollectionTitleAr { get; set; }
public string CoverImagePath { get; set; }
public int? SectionRoleId { get; set; }
//section role
public string NameAr { get; set; }
public string NameEn { get; set; }
}
public class SectionRoleDto
{
public int Id { get; set; }
public string NameEn { get; set; }
public string NameAr { get; set; }
}
Automapper:
CreateMap<Collections, CollectionsDTO>();
CreateMap<CollectionsSectionRolesFlatDTO, CollectionsSectionRolesDTO>();
Now the problem is mindboggling.
this line
var collectionsDto = Mapper.Map<List<CollectionsSectionRolesDTO>>(collectionsSectionRolesFlatDTO);
is supposed to return the data but instead it returns what I have passed in the function param.
Why is this happening? I have spent literally hours but nothing. I checked all the automapper setting but this doesn't work.
I am new to c# so any help regarding it would be appreciated. thanks.
Why would you be going through the trouble of double projection? Just configure Automapper with the information to get a desired DTO out of the object graph. Ensure you have navigation properties set up for your relationships to make querying against the object graph a lot simpler. Linq & EF does not need to be written as a substitute for SQL. (explicit joins) Navigation properties allow EF to provide those joins entirely behind the scenes for simpler querying.
First, the mapping:
CreateMap<Collections, CollectionsSectionRolesDTO>()
// Id, NameAr, and NameEn should auto-resolve.
.ForMember(x => x.CollectionTitleEn, opt => opt.MapFrom(src => src.Collection.CollectionTitleEn))
.ForMember(x => x.CollectionTitleAr, opt => opt.MapFrom(src => src.Collection.CollectionTitleAr));
// Continue for fields coming from Collection...
then to query using the automapper config. (config)
var results = db.Collections
.ProjectTo<CollectionsSectionRolesDTO>(config)
.ToList();
Done and dusted. No need to select and flatten data first into memory then use Automapper to create a new in-memory collection of the desired view model. Just project down to the desired view model directly within the query.
Double-projection is useful where you need to flatten data down first in order to perform conversions or transforms that cannot be converted in SQL. The first projection (Select) would typically be to an anonymous type or a DTO using ProjectTo, then fed into code that would provide the necessary transformations to produce the end result view models/DTOs.

Automapper ignores the mapping for derived class

I have a class hierarchy as described below:
public partial class EmployeeDTO
{
public virtual long Id { get; set; }
public virtual string Name { get; set; }
public virtual string ReportsTo { get; set; }
}
public class ManagerDTO: EmployeeDTO
{
public override string ReportsTo
{
get => null;
set => throw new InvalidOperationException("ReportsTo can not be set to other than the preset value.");
}
}
public class EmployeeModel
{
public long Id { get; set; }
public string FullName { get; set; }
public EmployeeType Type { get; set; }
public string ReportsTo { get; set; }
}
public enum EmployeeType
{
Employee = 0,
Manager = 1,
}
Mapping configuration
CreateMap<EmployeeDTO, EmployeeModel>()
.ForMember(empmodel => empmodel.FullName, empdto => empdto.MapFrom(empdto => empdto.Name))
.ForMember(empmodel => empmodel.Type, empdto => empdto.MapFrom(empdto => EmployeeType.Employee));
CreateMap<ManagerDTO, EmployeeModel>()
.ForMember(model => model.FullName, dto => dto.MapFrom(dto => dto.Name))
.ForMember(model => model.Type, dto => dto.MapFrom(dto => EmployeeType.Manager));
CreateMap<EmployeeModel, ManagerDTO>()
.ForMember(dto => dto.Name, model => model.MapFrom(model => model.FullName))
.ForMember(t => t.ReportsTo, t => t.Ignore());
CreateMap<EmployeeModel, EmployeeDTO>()
.ForMember(dto => dto.Name, model => model.MapFrom(model => model.FullName));
I am trying to patch the entity as follows:
[HttpPatch]
public IActionResult Patch([FromBody] Delta<EmployeeDTO> entityDelta)
{
var existingModel = new EmployeeModel
{
Id = 123,
FullName = "XYZ",
Type = Shared.EmployeeType.Manager,
ReportsTo = null
};
var existingEntity = _mapper.Map<EmployeeDTO>(existingModel);
entityDelta.Patch(existingEntity);
// Following mapping incorrectly sets type of updatedModel as EmployeeType.Employee
var updatedModel = _mapper.Map(existingEntity, existingModel);
return Ok(updatedModel);
}
After executing the above code expected type of the updatedModel to be Manager, but the type is Employee instead.
I have tried using the other overloads for the Map. Map(entity) works as expected when explicit type is provided for the previous mapping ie. Map(model). However, this overload creates another instance of the dbmodel with the same id, which leads to further issues.
To avoid having two runtime DbEntries with the same id, I am trying to use the Map(entity, existimgModel) overload of the mapper, which is failing to give the desired outcome.
Any ideas on how to solve this?
Sample code

How to assign value to destination property on aftermap in automapper

I have these classes
public class Student
{
public int StudentId { get; set; }
public int Name { get; set; }
public string SubjectIds { get; set; } //Comma delimited string of subject ID
public string Grades { get; set; } //Comma delimited string of subject ID
}
public class StudentListItemDto
{
public int StudentId { get; set; }
public int StudentName { get; set; }
public IEnumerable<SubjectGradeDto> Tags { get; set; }
}
public class SubjectGradeDto
{
public int SubjectId { get; set; }
public string Grade { get; set; }
}
In my mapping profile I have this where I map IEnumerable<Student> to List<StudentListItemDto>:
CreateMap<IEnumerable<Student>, List<StudentListItemDto>>()
.AfterMap((src, dest) =>
{
List<StudentListItemDto> iStudentListItemDto;
iStudentListItemDto= src.Select(p =>
new StudentListItemDto
{
StudentId= p.StudentId,
StudentName= p.Name,
Tags = p.SubjectIds!= null ? p.SubjectIds.Split(",").Select((tag, i) => new SubjectGradeDto { SubjectIds= int.Parse(tag), Grade= p.Grades.Split(",")[i] }) : null
}).ToList();
dest = iStudentListItemDto; // I'm assigning values to destination
});
The reason why I am mapping it this ways is because I need to map Subjects and Grades property of Students class, which is are comma delimited values to IEnumerable<SubjectGradeDto>
Further improvement to the mapping would be much appreciated.
But my problem is when I am assigning
dest = iStudentListItemDto;
in the AfterMap. When I debug it, it has a value, but when the mapping gets executed here
IEnumerable<StudentListItemDto> iStudentListItemDto= new List<StudentListItemDto>();
iStudentListItemDto = _mapper.Map<IEnumerable<Student>, IEnumerable<StudentListItemDto>>(searchResult, iStudentListItemDto);
where search result is IEnumerable<Student>
iStudentListItemDto evaluates to empty List.
The question is why?
In your CreateMap your mapping IEnumerable to List and when effectively mapping you are mapping from IEnumerable to IEnumerable.
List is IEnumerable, but IEnumerable is not a List, basically.
With this mapping you created you only have the direction from IEnumerable to List.
A better approach, as I can see you are only using this way for mapping the tags, is to do something like this
CreateMap<Student, StudentListItemDto>()
.ForMember(dto => dto.StudentName, opt => opt.MapFrom(s => s.Name))
.ForMember(dto => dto.Tags, opt => opt.Ignore())
.AfterMap((student, dto) =>
{
dto.Tags = student.SubjectIds!= null ? student.SubjectIds.Split(",").Select((tag, i) => new SubjectGradeDto { SubjectIds= int.Parse(tag), Grade= student.Grades.Split(",")[i] }) : null;
});
Now if AutoMapper sees that you are mapping a single object or a list of these objects, it will map each object of this list correctly.
This because you are not using the whole list to create this list of Tags.

Automapper: How to flatten complex object to plain object

public class Complex
{
public A A { get; set; }
public A B { get; set; }
}
public class A
{
public int a1 { get; set; }
public int a2 { get; set; }
}
public class B
{
public int b1 { get; set; }
public int b2 { get; set; }
}
//----------------Source Object End Here---------------------
public class Simple <----[This Simple class has only properties of A class]
{
public int aa1 { get; set; }
public int aa2 { get; set; }
}
//----------------Destination Object End Here---------------------
CreateMap<A, Simple>()
.ForMember(dest => dest.aa1, opt => opt.MapFrom(src => src.a1))
.ForMember(dest => dest.aa2, opt => opt.MapFrom(src => src.a2))
// Mapper IS NOT AVAILABLE HERE AS I AM USING PROFILE BASED CONFIGURATION
CreateMap<Complex, Simple>()
.ConvertUsing(src => Mapper.Map<A, Simple>(src.A)); <------Error at this line
//----------------Automammer config End Here---------------------
How to flatten from Complex to Simple? I don't wish to map Complex.A to Simple one by one again in the Complex to Simple config as it is already configured above.
Finally, I figured out with another overloaded method of ConvertUsing
CreateMap<Complex, Simple>()
.ConvertUsing((src,ctx) => {
return ctx.Mapper.Map<Complex, Simple>(src.A)
});
I feel this overloaded method has quite a multiple possibilities and flexibility. I don't have further issue of accessing Mapper directly as mentioned in the question. This overloaded method has its own context parameter (ResolutionContext). We can use Mapper from this context parameter like ctx.Mapper.Map<Complex, Simple>(src.A)

Automapper not ignoring nested property

I've had a look through the various similar posts but can't spot the error of my ways with this one. Basically I have two views which update different parts of a "Settings" object. The view models contain one of two properties and depending on which is being set, should ignore the other. It works fine when it's ViewModel ==> Entity direct but when Automapper tries to update the nested object it fails.
I have the following object structure:
public class Account
{
public int AccountId { get; set; }
public DateTime DateToBeIgnored { get; set; }
public AccountSetting Settings { get; set; }
}
public class AccountSetting
{
public string PropertyOne { get; set; }
public string PropertyTwo { get; set; }
}
public class AccountViewModel
{
public int AccountId { get; set; }
public DateTime DateToBeIgnored { get; set; }
public AccountSettingViewModel Settings { get; set; }
}
public class AccountSettingViewModel
{
public string PropertyTwo { get; set; }
}
public class OtherAccountSettingViewModel
{
public string PropertyOne { get; set; }
}
With the mappings:
void WireUpMappings()
{
// From the entities to the view models
Mapper.CreateMap<Account, AccountViewModel>();
Mapper.CreateMap<AccountSetting, AccountSettingViewModel>();
Mapper.CreateMap<AccountSetting, OtherAccountSettingViewModel>();
// From the view models to the entities
Mapper.CreateMap<AccountViewModel, Account>()
.ForMember(dest => dest.DateToBeIgnored, opt => opt.Ignore());
Mapper.CreateMap<AccountSettingViewModel, AccountSetting>()
.ForMember(dest => dest.PropertyTwo, opt => opt.Ignore());
Mapper.CreateMap<OtherAccountSettingViewModel, AccountSetting>()
.ForMember(dest => dest.PropertyOne, opt => opt.Ignore());
Mapper.AssertConfigurationIsValid();
}
When mapping [OtherAccountSettingViewModel --> AccountSetting], only the property "PropertyTwo" is assigned (and the original value for "PropertyOne" remains unchanged) -this is what I would expect.
However when mapping [AccountViewModel --> Account] "DateToBeIgnored" is ignored as intended whereas Account.AccountSetting.PropertyTwo's previous value is replaced with "null".
Can anyone spot the error of my ways?
Here is the solution:
[TestMethod]
public void TestMethod1()
{
Mapper.CreateMap<AccountViewModel, Account>()
.ForMember(dest => dest.DateToBeIgnored, opt => opt.Ignore())
.ForMember(dest=>dest.Settings, opt=>opt.UseDestinationValue());
Mapper.CreateMap<AccountSettingViewModel, AccountSetting>()
.ForMember(dest=>dest.PropertyOne, opt=>opt.Ignore())
.ForMember(dest => dest.PropertyTwo, opt => opt.MapFrom(a => a.PropertyTwo));
Mapper.AssertConfigurationIsValid();
AccountViewModel viewmodel = new AccountViewModel()
{
AccountId = 3,
DateToBeIgnored = DateTime.Now,
Settings = new AccountSettingViewModel() { PropertyTwo = "AccountSettingViewModelPropTwo" }
};
Account account = new Account()
{
AccountId = 10,
DateToBeIgnored = DateTime.Now,
Settings = new AccountSetting() { PropertyOne = "AccountPropOne", PropertyTwo = "AccountPropTwo" }
};
account = Mapper.Map<AccountViewModel, Account>(viewmodel, account);
Assert.IsNotNull(account);
}

Categories

Resources