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 5988697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:59:41+00:00 2026-05-22T22:59:41+00:00

I am currently learning the MVVM pattern, and the tutorial I am following uses

  • 0

I am currently learning the MVVM pattern, and the tutorial I am following uses Unity for DI. I haven’t really used DI in this way before and just wanted clarification of my thoughts on how this specific code works.

In the View I have:

private ViewModel vm;

    [Dependency]
    public ViewModel VM
    {
        set
        {
            vm = value;
            this.DataContext = vm;
        }

    }

where the dependency attribute is telling Unity to inject here. The ViewModel constructor takes an IQuoteSource object which is registered with Unity as such:

        IUnityContainer container = new UnityContainer();
        RandomQuoteSource randomQuoteSource = new RandomQuoteSource();
        container.RegisterInstance<IQuoteSource>(randomQuoteSource);
        MainWindow window = container.Resolve<MainWindow>();
        window.Show();

How exactly does this work, as I am never explicitly creating an object of the ViewModel using the property above. Is this all handled within Unity, if so how does it achieve this?

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-22T22:59:41+00:00Added an answer on May 22, 2026 at 10:59 pm

    This doesn’t have much to do with the MVVM pattern per se, other than the fact that the view’s dependency on its ViewModel is resolved via dependency injection.

    For how this works, it’s pretty simple. There are 3 simple concepts for DI:

    The first is declaring a dependency, where some object specifies that it depends on something, either via a constructor or a property (which was the case in your example, using the DependencyAttribute).

    The second concept is registration, where you register an implementation of the dependencies your objects have (in your case, you registered an implementation of the IQuoteSource). Note that you didn’t have to register the ViewModel, because it’s not really an implementation of an interface that you depend on.

    The third is what glues things together, which is resolving the dependencies, where you ask the container to resolve some type for you, and it goes and looks at what dependencies that object declared (in your case, you’re resolving the MainWindow which has a dependency on the ViewModel), finds the right registered implementation and resolves it. This behavior cascades through the resolution of the graph of objects (which resolves the dependency of the ViewModel on the IQuoteSource).

    Hope this helps 🙂

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

Sidebar

Related Questions

I am currently learning MVVM abd the tutorial I have been using, simply uses
I'm currently learning how to write a WPF application using the MVVM pattern. I'm
I'm currently learning Ruby and RoR and I stumbled across this declaration: link_to_remote(name, options
I am currently learning ffmpeg tutorial of Martin Bohme Tutorial Here and I want
I'm currently learning WPF. I really am enjoying it so far. I love how
As I'm currently learning Django / Python, I've not really been using the concept
Currently learning Spring MVC with Spring 3, I'm trying to find the correct way
I am currently learning ASP.NET MVC and I have followed Stephen Walther's tutorial: ASP.NET
I'm currently learning C# and .NET (coming from a UNIX background), and have just
I'm learning WPF and the MVVM Pattern and I'm trying to build a calendar-like

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.