如何使用 Express Mapper 将数据 Table 映射到 "DTO"
How to map Data Table to "DTO" using Express Mapper
我正在使用 ASP.NET Web API 和 C#。因为我是 Express Mapper 的新手,我有 ADO.NET 代码返回 results.How 列表以使用 Express Mapper 进行映射?
这是一个演示如何在 ExpressMapper 中使用自定义映射器的测试。我希望你能相应地使用它 -
public static void Main()
{
var ds = new DataSet();
var dt = new DataTable();
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Age", typeof(int));
dt.Rows.Add("Test", 10);
dt.Rows.Add("Test2", 10);
ds.Tables.Add(dt);
var mapped = Mapper.Map<DataTable, List<RequestModel>>(ds.Tables[0], new CustomTypeMapper());
}
哪里-
class RequestModel
{
public int Age { get; set; }
public string Name { get; set; }
}
class CustomTypeMapper : ICustomTypeMapper<DataTable, List<RequestModel>>
{
public List<RequestModel> Map(IMappingContext<DataTable, List<RequestModel>> context)
{
if (context.Source == null)
throw new ArgumentNullException();
var output = new List<RequestModel>();
foreach (DataRow row in context.Source.Rows)
{
output.Add(new RequestModel
{
Age = row.Field<int>("Age"),
Name = row.Field<string>("Name")
});
}
return output;
}
}
我正在使用 ASP.NET Web API 和 C#。因为我是 Express Mapper 的新手,我有 ADO.NET 代码返回 results.How 列表以使用 Express Mapper 进行映射?
这是一个演示如何在 ExpressMapper 中使用自定义映射器的测试。我希望你能相应地使用它 -
public static void Main()
{
var ds = new DataSet();
var dt = new DataTable();
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Age", typeof(int));
dt.Rows.Add("Test", 10);
dt.Rows.Add("Test2", 10);
ds.Tables.Add(dt);
var mapped = Mapper.Map<DataTable, List<RequestModel>>(ds.Tables[0], new CustomTypeMapper());
}
哪里-
class RequestModel
{
public int Age { get; set; }
public string Name { get; set; }
}
class CustomTypeMapper : ICustomTypeMapper<DataTable, List<RequestModel>>
{
public List<RequestModel> Map(IMappingContext<DataTable, List<RequestModel>> context)
{
if (context.Source == null)
throw new ArgumentNullException();
var output = new List<RequestModel>();
foreach (DataRow row in context.Source.Rows)
{
output.Add(new RequestModel
{
Age = row.Field<int>("Age"),
Name = row.Field<string>("Name")
});
}
return output;
}
}