사용자 지정 매핑 Dapper.NET

.net c# dapper

문제

나는 이렇게 할 수 있었지만 더 잘할 수있는 방법이 있어야한다고 생각합니다.

    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/ )를 사용하여 해당 클래스를 ResourceIncrease 클래스에 매핑합니다.

이것을 확인하십시오 : http://geekswithblogs.net/EltonStoneman/archive/2012/01/03/mapping-and-auto-mapping-objects.aspx



아래 라이선스: CC-BY-SA with attribution
와 제휴하지 않음 Stack Overflow
아래 라이선스: CC-BY-SA with attribution
와 제휴하지 않음 Stack Overflow