[DevExpress Support Team: CLONED FROM T548158: Defining Member Permissions on an descendant class with a property overridden using K18270 approach, a duplicate key exception is thrown]
Hi @Uriah is there a solution to this beyond saying the approach is outdated? I have a legitimate need for this where I have a concept of Quote, QuoteItem and QuoteProductItem. The only difference with QuoteProductItem is that it links to a Product whereas the QuoteItem does not. What I want to have is if you set a SKU on the QuoteProductItem (which also exists on QuoteItem) it should automatically set the Product reference to the Product that matches the SKU. I'm running into same problem as Alex.
How to override persistent properties
Answers approved by DevExpress Support
If the SKU property is a QuoteItem member, you can use the virtual OnChanged method to update the Product property.
C#public class QuoteItem : XPObject {
private SKU fSku;
public SKU SKU {
get => fSku;
set {
bool modified = SetPropertyValue(nameof(SKU), ref fSku, value);
if (modified && !IsLoading)
Product = SKU.Product;
}
}
}
public class QuoteProductItem : QuoteItem {
private Product fProduct;
public Product Product {
get => fProduct;
set => SetPropertyValue(nameof(Product), ref fProduct, value);
}
protected override void OnChanged(string propertyName, object oldValue, object newValue) {
base.OnChanged(propertyName, oldValue, newValue);
if (propertyName == nameof(SKU)) {
Product = SKU.Product;
}
}
}
Thanks Uriah, that's a nice clean solution for what I'm trying to achieve. I was trying to override the SKU with new keyword and then went down the path of itroducing another property which isn't necessary with this approach.
Hello,
>> What I want to have is if you set a SKU on the QuoteProductItem (which also exists on QuoteItem) it should automatically set the Product reference to the Product that matches the SKU.
You can achieve this result without inheritance. Please check the following code and let me know if this solution meets your requirements.
public class QuoteProductItem :XPObject { private Product fProduct; public Product Product { get => fProduct; set => SetPropertyValue(nameof(Product), ref fProduct, value); } private SKU fSku; public SKU SKU { get => fSku; set { bool modified = SetPropertyValue(nameof(SKU), ref fSku, value); if (modified && !IsLoading) Product = SKU.Product; } } }
Hi @Uriah, No, that doesn't work. As QuoteProductItem needs to inherit QuoteItem as it has many other shared fields. So QuoteProductItem : QuoteItem.
Thank you for the clarification. If the SKU property is a QuoteItem member, you can use the virtual OnChanged method to update the Product property. I have provided a code snippet in the answer. Please check if this solution meets your requirements.