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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:58:45+00:00 2026-05-27T18:58:45+00:00

Creating the View and View Model Using Unity Using Unity as your dependency injection

  • 0

Creating the View and View Model Using Unity

Using Unity as your dependency injection container is similar to using
MEF, and both property-based and constructor-based injection are
supported. The principal difference is that the types are typically
not implicitly discovered at run time; instead, they have to be
registered with the container.

Typically, you define an interface on the view model so the view
model’s specific concrete type can be decoupled from the view. For
example, the view can define its dependency on the view model via a
constructor argument, as shown here. C#

public QuestionnaireView() 
{
    InitializeComponent(); 
}

public QuestionnaireView(QuestionnaireViewModel viewModel) : this() 
{
    this.DataContext = viewModel;
}

The default parameter-less
constructor is necessary to allow the view to work in design-time
tools, such as Visual Studio and Expression Blend.

Alternatively, you can define a write-only view model property on the
view, as shown here. Unity will instantiate the required view model
and call the property setter after the view is instantiated. C#

public QuestionnaireView() 
{
    InitializeComponent(); 
}

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

The view model type is registered with the Unity container, as shown
here. C#

IUnityContainer container;
container.RegisterType<QuestionnaireViewModel>();

The view can then be instantiated through the container, as shown
here. C#

IUnityContainer container;
var view = container.Resolve<QuestionnaireView>();
  1. If I leave out the last part of the code regarding registering the ViewModel and instantiating the View, and just use either of the two methods of hooking the ViewModel to the View here (using a constructor or using a property) it seems the ViewModel and View it seems everything is working fine. So what is the need for the code registering the ViewModel and instantiating the View?

  2. The first example, hooking the View and ViewModel using a constructor, has no mention of Unity of all, so is Unity actually being used here?

  3. Are there any advantages of using property-based injection over construtor based injection or are they exactly the same thing?

  4. The first part of the text says “*Typically, you define an interface on the view model so the view model’s specific concrete type can be decoupled from the view”, and then gives an example. Yet this example makes no mention of interfaces at all. What is going on here, am I missing something?

  • 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-27T18:58:46+00:00Added an answer on May 27, 2026 at 6:58 pm

    To answer questions 1 & 4

    In your example, the viewmodel is of type QuestionnaireViewModel, which is a concrete class. Since it’s a concrete class, when you resolve the view using container.Resolve<QuestionnaireView>(), unity will instantiate the viewmodel for you by calling container.Resolve<QuestionnaireViewModel>() behind the scenes.

    In this case, registering your viewmodel is redundant. However, when using dependency injection you usually want to work with interfaces rather than classes, so your constructor would look like this:

    public QuestionnaireView(IQuestionnaireViewModel viewModel)
    {
        this.DataContext = viewModel;
    }
    

    Now that your constructor receives an interface rather than a class as a parameter, Unity doesn’t know which implementation of the interface you’d like to use. To tell Unity that, you need to register your viewmodel to the container:

    container.RegisterType<IQuestionnaireViewModel, QuestionnaireViewModel>();
    

    so now when you resolve your view, Unity will look up which class it should use as an implementation of IQuestionnaireViewModel, see that it’s QuestionnaireViewModel and use it.

    To answer question 2

    Unity is being used, since in order for the constructor to get its parameters, you need to resolve the view using the container. Unity is not used if you instantiate the view yourself using new QuestionnaireView(), i.e. no constructor or property injection will occur.

    To answer question 3

    I think it’s mostly a matter of what’s more comfortable and where you need to use the injected members. A lot of times you just want to set a local variable in the constructor and not create a property just for performing the injection.

    One good thing about property-injection though is the fact that you can use the container.BuildUp() method for instances that were created by using new rather than container.Resolve<>(). This way, you can inject members into properties even after creation – which is something you can’t do with constructor injection.

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

Sidebar

Related Questions

I'm creating a WPF app using Prism and Unity as the container. A couple
I recently realized that I'm creating my n-tier applications using the Anemic Model, Which
I'm creating ViewModels in my MVC application. We are using automapper for domain model
How do I modify an MVVM view model Progress property for work being done
Using the MVVM pattern creating WPF applications you have the ViewModel providing data to
Should I use the same controller and view for editing and creating models in
I am creating a dialog like in this page: http://jqueryui.com/demos/dialog/#modal-confirmation (click view source) on
When creating a view and setting group by value some field, it groups only
Can rails handle creating a view without a controller? For example, let say I
I am creating an SQL view for a file that strips out the spaces

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.