I'm wondering to using extension method to avoid checking for null in hierarchy. The example:
// GetItems(), GetFirstOrDefault(), GetProduct(), GetIDProduct() are extension methods like:
public static SomeType GetSomeProperty( this XYZ obj ) {
if ( object.ReferenceEquals( obj, null ) ) { return default( SomeType ); }
return obj.SomeProperty;
}
// the code with extension methods
Guid? idProduct = this.Invoice.GetItems().GetFirstOrDefault().GetProduct().GetIDProduct();
// instead of
Guid? idProduct = null;
Invoice invoice = this.Invoce;
if ( null != invoice ) {
InvoiceItems items = invoice.Items;
if ( null != items && items.Count > 0 ) {
InvoiceItem item = items[0];
if ( null != item ) {
idProduct = item.IDProduct();
}
}
}
I know, there is available Null Object pattern, but the solution with this type of extension methods looks better.
Do you think, this solution is good or bad (because bad/good design, lucidity, whatever else)?
Please vote "Good" or "Bad" and why do you think so. Posts are flaged as community.