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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T16:34:35+00:00 2026-06-14T16:34:35+00:00

Hi I am trying to use Unity container in WPF MVVM application. I have

  • 0

Hi I am trying to use Unity container in WPF MVVM application. I have not used Prism as it seems to heavy. Here is the application structure. I am trying to figure out how to resolve Views to ViewModels and dependencies of the view models (services).

Application:

Views

MainWindow.xaml
CustomerList.xaml
CustomerDetail.xaml
BookList.xaml
BookDetail.xaml

ViewModels

MainViewModel

CustomerListViewModel

BoolListViewModel

BookDetailViewModel

CustomerDetailViewModel

Library

ICustomerService (AddCustomer, SaveCustomer, GetCustomers, GetCustomer)

CustomerService:ICustomerService

IBookService (GetBooks, GetBook)

BookService:IBookService

IBookReserveService(Reserve, Return)

BookReserveService:IBookReserveService
  1. MainViewModel needs reference to ICustomerService, and IBookService

  2. CustomerListViewModel needs reference to ICustomerService

  3. BoolListViewModel needs reference to IBookService

  4. BookDetailViewModel needs reference to ICustomerService, and IBookReserveService

  5. CustomerDetailViewModel needs reference to ICustomerService, and IBookReserveService

I have getter setter property for services in each viewmodels.

I am running into issues on how do I use Dependency Injection with WPF especially for Views and ViewModel. I tried with Unity to register and resolve in a console application which is working fine. But I would like some ideas on how this could be done for WPF app. I tried registering

 container.RegisterType<ICustomerService, CustomerService>()
 container.RegisterType<IBookReserveService, BookReserveService>()
 container.RegisterType<IBookService, BookService>()

and resolve it using
container.Resolve();

But I was not sure how I could tell which view must use which view model and resolve them when required and not when the app starts. Also, I dont to resolve all mapping in the app start. It should be done when the menu (Selecting a customer to view detail, selecting a book to view detail, save customer, reserve book etc.) is selected.

Mostly what I read wanted to use IView and IViewModel. But not sure I understood the advantage in it.

Any help is greatly appreciated.

  • 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-06-14T16:34:37+00:00Added an answer on June 14, 2026 at 4:34 pm

    Here’s one way you can do this. First, register your view-models and services with Unity like this:

    // Unity is the container
    _container.RegisterType<IMainViewModel, MainViewModel>();
    _container.RegisterType<IBookService, BookService>();
    

    Second, set your view’s DataContext to a view-model in the view’s constructor like this:

    public partial class MainView:UserControl
    {
       private readonly IUnityContainer _container;
    
       public MainView(IUnityContainer container)
            {
                InitializeComponent();
                _container = container;   
                this.DataContext = _container.Resolve<IMainViewModel>();            
            }      
    }
    

    Third, you’ll need to inject your services into your view-models:

    public MainViewModel(ICustomerService custService, IBookService bookService ){}
    

    There are other ways to do this using .config files, etc. but this should give you enough to get started, let me know if you need more. You asked what the advantages of DI are and there are many, I feel. Just to name a couple: promotes loose-coupling between your components and improved testability. I feel it’s one of the lynch-pins to a good design/implementation.

    UPDATE:

    With Unity >=3, you can skip the container registration if you follow the naming convention like this:

    // This will register all types with a ISample/Sample naming convention 
                container.RegisterTypes(
                    AllClasses.FromLoadedAssemblies(),
                    WithMappings.FromMatchingInterface,
                    WithName.Default);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying use gem tire to search in my application. I have tables
I'm looking for an IoC container to use in my Compact Framework application. Trying
I am trying to use the Unity IoC with the unit of work &
I'm trying to use check unit testing framework for my C application. But I
Hi I'm trying use a datepicker on a field I have. I'm trying to
I'd like to use Unity as an IoC container for an ASP.NET MVC 3
I am trying to use unity to automatically inject a datacontext on my repository
I'm trying to invoke the RegisterType method in the Unity container. RegisterType has a
Currently I am trying to use a config file to give Unity Framework information
I am trying to use the Unity event aggregator to do messaging between various

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.