2

我可以这样做,但我认为应该有一种方法可以做得更好;

    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; }
}
4

1 回答 1

0

我将创建一个与 SP 的返回匹配的类,然后使用 AutoMapper ( http://automapper.org/ ) 将该类映射到 ResourceIncrease 类。

检查这个 http://geekswithblogs.net/EltonStoneman/archive/2012/01/03/mapping-and-auto-mapping-objects.aspx

于 2013-12-06T17:03:11.950 回答