自定义映射 Dapper.NET
本文关键字:NET Dapper 映射 自定义 | 更新日期: 2023-09-27 18:31:51
我可以这样做,但我认为应该有一种方法可以做得更好;
Resource.ResourceIncrease resource = new Resource.ResourceIncrease();
using (IDbConnection connection = OpenConnection())
{
dynamic reader = connection.Query<dynamic>("UserResourceGet", new { UserId = UserId }, commandType: CommandType.StoredProcedure).SingleOrDefault();
resource.Wood.value = reader.Wood;
resource.Wood.increase = reader.WoodIncome;
resource.Food.value = reader.Food;
resource.Food.increase = reader.FoodIncome;
resource.Stone.value = reader.Stone;
resource.Stone.increase = reader.StoneIncome;
resource.Gold.value = reader.Gold;
resource.Gold.increase = reader.GoldIncome;
}
return resource;
SQL SP>
SELECT Wood, Food, Stone, Gold, WoodIncome, FoodIncome, StoneIncome, GoldIncome
FROM Resources WHERE UserId = @UserId
类>
public class Resource
{
public int Wood { get; set; }
public int Food { get; set; }
public int Stone { get; set; }
public int Gold { get; set; }
}
public class ResourceIncrease
{
public Increase Wood{ get; set; }
public Increase Food { get; set; }
public Increase Stone { get; set; }
public Increase Gold { get; set; }
public ResourceIncrease()
{
this.Wood = new Increase();
this.Food = new Increase();
this.Stone = new Increase();
this.Gold = new Increase();
}
}
public class Increase
{
public int value { get; set; }
public int increase { get; set; }
}
我将创建一个与 SP 返回匹配的类,然后使用 AutoMapper (http://automapper.org/) 将该类映射到 ResourceGrowth 类。
检查这个http://geekswithblogs.net/EltonStoneman/archive/2012/01/03/mapping-and-auto-mapping-objects.aspx