I think the more important question is why are you doing so much casting?
In the first example:
Why is the first type of parameter IPlantItem , if you continue to distinguish it from OSiteEquipment ? The same can be said about the second parameter.
In the second example:
Why IEnumerable<IPlantItem> GetParentPArtByObjectId return an IEnumerable<IPlantItem> ? If he returned ICollection<IPlantItem> , you would not have to throw on ObservableCollection<T> . ObservableCollection<T> inherits from Collection<T> , which implements both ICollection<T> and ICollection . You should be able to remove an item from the collection without even knowing its type.
Now some tips.
Do not drop the same object multiple times.
Do not do this:
if (obj is IPlantItem) ((IPlantItem)obj).DoSomething();
Do it instead
IPlantItem plant = obj as IPlantItem; if (plant != null) plant.DoSomething();
Use basic types whenever possible. This will not allow you to quit so much. As said before, do not apply a method call to an ObserableCollection<T> on an ICollection
Use generics. If you need type logic, create an abstract base class (or just an interface if you don't need common logic) with a common parameter. Then, implement the implementations of this class for each of the implementations of the interface. Methods may also be common. I can rewrite the second example as
public void DeletePart<TPlantItem>(TPlantItem plantItem) where TPlantItem : IPlantItem { IEnumerable<TPlantItem> itmParent = GetParentPartByObjectId(_siteDocument, plantItem); ((ObservableCollection<TPlantItem>)itmParent).Remove(plantItem); }
cadrell0
source share