我有一个存储过程,该过程返回一个结果集,但具有包含客户,订单和定价数据的字段。在我的模型中,我分离了模型对象的客户,订单和价格,并创建了一个模型绑定器以将所有对象绑定到一个模型中。现在,定价模型中的设置者取决于客户基本价格的返回值。如何实现二传手?即时消息,我使用dapper进行对象映射
public class Customer
{
public int ID { get; set; }
public string Name{ get; set; }
public int baseprice {get; set;}
}
public class Order
{
public int ID { get; set; }
public string Name{ get; set; }
}
public class Pricing
{
int _price;
public int ID { get; set; }
public string Price
{ get { return this._price; }
set { this._price = Customer.baseprice * 10 }
}
}
public class CustomerModelBinder
{
public Customer Cust{get; set}
public Order order{get;set}
public Pricing pricing{get; set}
}
CustomerModelBinder cust = new CustomerModelBinder();
//put value to cust here......
答案 0 :(得分:1)
如果Pricing类保留Customer类,这将很简单。
public class Customer
{
public int ID { get; set; }
public string Name { get; set; }
public int BasePrice { get; set; }
}
public class Pricing
{
public int ID { get; set; }
public int Price => Customer.BasePrice * 10;
public Customer Customer { get; set; }
}
您可以在this post中找到如何将查询结果放入此类。
答案 1 :(得分:0)
您不能那样做,因为您正在创建实例。您可以在CustomerModelBinder
构造函数中完成此操作。请注意,您需要先设置Customer baseprice
。
public class Pricing
{
public int ID { get; set; }
public int Price { get; set; }
}
public class CustomerModelBinder
{
public Customer Cust{get; set}
public Order order{get;set}
public Pricing pricing{get; set}
public CustomerModelBinder(int custBasePrice)
{
Cust.baseprice = custBasePrice;
pricing.Price = Cust.baseprice * 10;
}
}
或者您可以在Pricing Price
public class Pricing
{
public int ID { get; set; }
private int _price;
public int Price
{
get => _price;
set
{
_price = value * 10;
}
}
}
public class CustomerModelBinder
{
public Customer Cust{get; set}
public Order order{get;set}
public Pricing pricing{get; set}
public CustomerModelBinder(int custBasePrice)
{
Cust.baseprice = custBasePrice;
pricing.Price = Cust.baseprice;
}
}