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

  • Home
  • SEARCH
  • 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 5995013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:54:01+00:00 2026-05-22T23:54:01+00:00

I am currently learning MVVM abd the tutorial I have been using, simply uses

  • 0

I am currently learning MVVM abd the tutorial I have been using, simply uses a single View Model with a single Model. The View Model is injected with the Model interface as such:

public ViewModel(IQuoteSource quoteSource)
{

}

This is handled via DI using Unity. My question is what if the VM is dependent on multiple Models, say an arbitrary 6 or 7? Do we simply inject each of them into the ctor or should there be seperate VM’s for this?

Or should each model try to implement a specific interface that exposes a couple of methods/events such as register to hook into and then an event once the data has been updated?

Thanks.

  • 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-22T23:54:01+00:00Added an answer on May 22, 2026 at 11:54 pm

    That is a lot of models for your VM to depend on. You can inject all those on the constructor, this makes the dependence more explicit and visible. Or you can have the VM resolve the various models from the Unity container:

    public ViewModel(IUnityContainer container) 
    {
        IQuoteSource model1 = container.Resolve<IQuoteSource>();
        ... etc ...
    }
    

    When a concrete instance is resolved using Unity and you have not specified any constructor parameters in the Resolve() call, Unity will examine the constructors from most complicated to most basic, if it finds one that takes a Unity container then it will use it (it will potentially use others first if it finds them).1 So if your VM takes a Unity container in the constructor, it can then use that container to further resolve anything it needs.

    Some may argue over which method is more suitable – direct injection of all required dependencies, or injecting just the container that has the dependencies. IMVHO there are positives and negatives to both approaches, use whichever suits your style better.

    You could also consider refactoring your code somewhat, maybe introduce a new Model that is a facade over several existing models. The other possibility is what you have termed a Model may actually be more suitable as a service (a tell-tale sign of this is if the model is used in several places to fetch data from a specific source, if this is the case then you can separate out that functionality into a service and cart that around in the Unity container for it to be consumed as necessary).

    1 Reference: Telling Unity Which Constructor to Use when Initializing a Class

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to have generalised email templates. Currently I have multiple email templates with
We manage a site for a medical charity. They have a number of links
IE is giving me an undefined NAN when i try to view the calender...
I am using a 3rd-party rotator object, which is providing a smooth, random rotation

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.