Why is UseValue only executed once? I need to call the TeamRepository for each request.
How can I achieve this?
Mapping from TeamEmployee to TeamEmployeeInput:
CreateMap<TeamEmployee, TeamEmployeeInput>()
.ForMember(x => x.Teams, x => x.UseValue(GetTeamEmployeeInputs()))
.ForMember(d => d.SelectedTeam, s => s.MapFrom(x => x.Team == null ? 0 : x.Team.Id));
private IEnumerable<TeamDropDownInput> GetTeamEmployeeInputs()
{
Team[] teams = CreateDependency<ITeamRepository>().GetAll();
return Mapper.Map<Team[], TeamDropDownInput[]>(teams);
}
Domain object:
public class TeamEmployee : Entity
{
public virtual Employee Employee { get; set; }
public virtual Team Team { get; set; }
}
View model objects:
public class TeamEmployeeInput
{
public int? Id { get; set; }
public string EmployeeLastName { get; set; }
public string EmployeeEMail { get; set; }
public string EmployeeFirstName { get; set; }
public int SelectedTeam { get; set; }
public IList<TeamDropDownInput> Teams { get; set; }
}
public class TeamDropDownInput : IDropdownList
{
public int Id { get; set; }
public string Text { get; set; }
}
AutoMapper in C# is a library used to map data from one object to another. It acts as a mapper between two objects and transforms one object type into another. It converts the input object of one type to the output object of another type until the latter type follows or maintains the conventions of AutoMapper.
AutoMapper is a great tool when used for simple conversions. When you start using more complex conversions, AutoMapper can be invaluable. For very simple conversions you could of course write your own conversion method, but why write something that somebody already has written?
When you call CreateMap, AutoMapper uses optimizers to build the code for getting/setting values on source/destination types. Currently, it uses a combination of Reflection.
The Automapper Reverse Mapping is nothing but the two-way mapping which is also called as bidirectional mapping. As of now, the mapping we discussed are one directional means if we have two types let's say Type A and Type B, then we Map Type A with Type B.
Try the MapFrom
option. It provides a delegate that will be called each time a map happens. From a quick DateTime test and my command window this seems to work.
Something like:
public class Foo {
public DateTime bar { get; set; }
}
public class Foo1
{
public DateTime bar1 { get; set; }
}
Mapper.CreateMap<Foo, Foo1>()
.ForMember(x => x.bar1, opt => opt.MapFrom(x => DateTime.Now)); // not using x, your function returns the value for bar1
I have to point out that this is not the way AutoMapper is designed to work. AutoMapper should map properties from one model to another. So if the data does not exist on modelA
you should not map that data to modelB
.
Your code change would be:
CreateMap<TeamEmployee, TeamEmployeeInput>()
.ForMember(x => x.Teams, x => x.MapFrom(x => GetTeamEmployeeInputs()))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With