I have a stored procedure which returns one result set but has the fields with customer, orders, and pricing data. In my Model, I separated the model objects customer, orders, and pricing and I created a Model binder to bind all the object in one model. Now, the setters in pricing model depends on the returned value of the customer baseprice. How can I achieve the setters? By the way im using dapper for object mapping
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......
If Pricing class hold Customer class, it'll be simple.
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; }
}
You can find how to put query results into such class in this post.
You can't do it like that since you are creating instances. You could do it in the CustomerModelBinder
constructor. Note, you need to set the Customer baseprice
first.
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;
}
}
Or you could do the multiplication in 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;
}
}