Mapper - Custom List-Property
From the previous example Mapper, a custom property was created on the data entity and then that property was used throughout the application.
It may be that there is only a need to create a custom property for a view and it is not desired to create the property on the data entity, but only the model entity.
Requirement: On the list of Employees, it should be displayed how many Employees an Employee has under them.
Task: Create a property on the EmployeesList class that counts ReportTo
Execution:
- Create property 'CountReportTo' on EmployeesList
- Make mapping
- Add the property to the list control itself.
Create property¶
- Select "User Interface".
- Select "Employees".
- Select the "List" tab.
- Under "Code Tasks" select "Entity - Model" and click on "Add Code".
- Create a property as below
public partial class EmployeesList
{
public int CountReportTo { get; set; }
}
Make mapping¶
In "Code Tasks" select "Mapper" and implement the following (FullName mapping is from a previous example):
public partial class EmployeesListMapperProfile
{
partial void AfterConfiguration(IMappingExpression<Employees, EmployeesList> mapping)
{
mapping.ForMember(e => e.FullName, opt => opt.MapFrom(e => $"{e.FirstName} {e.LastName}"));
mapping.ForMember(e => e.CountReportTo, opt => opt.MapFrom(e => e.Employees2.Count));
}
}
Add the property to the list¶
In "Code Tasks" select "List-Control" and implement the following:
public partial class EmployeesList
{
partial void AfterSetLayout()
{
this.AddColumn<Northwind.Data.Entity.EmployeesList>(e => e.CountReportTo, e => e.CountReportTo);
}
}
The list now looks like this: