Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7621401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:11:09+00:00 2026-05-31T04:11:09+00:00

When working with MVVM and Prism i find myself doing a lot of casting,

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T04:11:10+00:00Added an answer on May 31, 2026 at 4:11 am

    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);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on a WPF project, using MVVM and Microsoft Prism libraries .
I am currently working with the Microsoft MVVM template and find the lack of
I will be doing an MVVM Project soon and I'm working a couple tutorial/examples
I started working on a WPF project using Prism and MVVM, and I am
I am working on a WPF MVVM framework I have a ItemType Model public
I'm working on a WPF project using Prism and MVVM, I'm new using this
Hi people stackoverflow. I'm working with MVVM, I have ViewModel call UserViewModel with a
I'm building a WPF application and working with the MVVM pattern. I have 4
I'm working on a WPF project using MVVM and I'm trying to implement a
I'm working on a WPF MVVM application and I've got a TextBox on my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.