When working with MVVM and Prism i find myself doing a lot of casting, as most parameters are interfaces
Ex
public void AddCrSubSystemsToPlant(IPlantItem plantItm, CRArticleItem crItm)
{
OSiteSubSystem itm = (OSiteSubSystem)crItm;
itm.PartData.Order = ((OSiteEquipment)plantItm).SubSystems.Count() + 1;
((OSiteEquipment)plantItm).SubSystems.Add(itm);
}
or
public void DeletePart(IPlantItem plantItem)
{
IEnumerable<IPlantItem> itmParent = GetParentPartByObjectId(_siteDocument, plantItem);
if (plantItem is OSiteEquipment)
((ObservableCollection<OSiteEquipment>)itmParent).Remove((OSiteEquipment)plantItem);
if (plantItem is OSiteSubSystem)
((ObservableCollection<OSiteSubSystem>)itmParent).Remove((OSiteSubSystem)plantItem);
if (plantItem is OSiteComponent)
((ObservableCollection<OSiteComponent>)itmParent).Remove((OSiteComponent)plantItem);
}
My question is , whats the cost involved. Are these operations costly memory or cpu wise, should they be avoided.
Any views?
Sand casting pattern costs range from $500 to $7,500, depending on the size and complexity of the part. Because sand molds are relatively simple to make, there is a lower capital investment and lower tooling costs. Investment casting creates complex parts with excellent finishes from a ceramic mold tree.
The casting parameters such as molten alloy temperature, mold preheat temperature, pressurization rate in injection, and cooling rate after injection should be carefully determined according to the mold dimensions and fuel alloy composition.
The startup costs for a casting agency can run between $10,000 and $50,000, but it's also possible to keep them lower. The initial expenses go toward: Securing an office and audition space.
I think the more important question is why are you doing so much casting?
In the first example:
Why is the first parameter type IPlantItem
if you keep casting it to OSiteEquipment
? The same can be said about the second parameter.
In the second example:
Why does GetParentPArtByObjectId return an IEnumerable<IPlantItem>
? If it were to return an ICollection<IPlantItem>
you wouldn't have to cast to ObservableCollection<T>
. ObservableCollection<T>
inherits from Collection<T>
which implements both ICollection<T>
and ICollection
. You should be able to remove the item from the collection without even knowing its type.
Now some advice.
Don't cast the same object multiple times.
Don't do this:
if (obj is IPlantItem)
((IPlantItem)obj).DoSomething();
Do this instead
IPlantItem plant = obj as IPlantItem;
if (plant != null)
plant.DoSomething();
Use base types whenever possible. This will keep you from needing to cast so much. As previously stated, don't cast to ObserableCollection<T>
to call a method on ICollection
Use generics. If you need type specific logic, make an abstract base class(or just an interface if you don't need any shared logic) with a generic parameter. Then make implementations of that class for each of the implementations of the interface. Methods can be generic, too. 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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With