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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:06:25+00:00 2026-05-28T19:06:25+00:00

This question might look naive, but I couldn’t understand this code in the ViewModelLocator.cs

  • 0

This question might look naive, but I couldn’t understand this code in the ViewModelLocator.cs file:

static ViewModelLocator()
{
    ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);

    if (ViewModelBase.IsInDesignModeStatic)
    {
        SimpleIoc.Default.Register<IDataService, Design.DesignDataService>();
    }
    else
    {
        SimpleIoc.Default.Register<IDataService, DataService>();         
    }

    SimpleIoc.Default.Register<MainViewModel>();
}

I see that we use a DataService to get data (from WCF service for example) and assigning it to the MainViewModel. But what if I’m registering more than one ViewModel? like this:

static ViewModelLocator()
{
    ....
    SimpleIoc.Default.Register<MainViewModel>();
    SimpleIoc.Default.Register<Page2ViewModel>();
}

and let’s say I have another DataService (DataService2 for example), but this one I’ll use with the Page2ViewModel. how can I do that?

Also, if someone can help me (or even give me a link to read) about the above code. I have no clue what it means.

  • 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-28T19:06:26+00:00Added an answer on May 28, 2026 at 7:06 pm

    You are not assigning any IDataService to the MainViewModel here. You are registering a type mapping, so your container will be aware that it should return a DataService whenever an IDataService required.

    This is related to dependency injection http://en.wikipedia.org/wiki/Dependency_injection

    The DI container auto-wires the dependencies, so when you need a specific type, you can call

    ServiceLocator.Current.GetInstance<IDataService>()

    or

    ServiceLocator.Current.GetInstance<MainViewModel>()
    

    etc. If it can build it (so you registered your types), it will resolve the full dependency graph for you.

    For example, if your MainViewModel has a constructor dependency on IDataService, and you are not in design mode, a DataService will be injected to the MainViewModel constructor. Don’t be afraid from the buzzword injected, it is just a call to the MainViewModel constructor with the appropriate parameters :).

    So, MainViewModel will not interference with Page2ViewModel here.

    I made a simple sample for you to demonstrate what happens (I used Unity, http://unity.codeplex.com/ , but the syntax is almost the same):

    class Program
    {
        static void Main(string[] args)
        {
            var container = new UnityContainer();
            container.RegisterType<IService, Service1>();
            container.RegisterType<IService, Service2>("MySpecificService");
            container.RegisterType<IRepository, Repository>();
            ServiceLocator.SetLocatorProvider(() => new UnityServiceLocator(container));
    
            var viewModel = ServiceLocator.Current.GetInstance<MainViewModel>();
            viewModel.Foo();
        }
    }
    
    interface IService
    {
    }
    
    interface IRepository
    {   
    }
    
    class Service1 : IService
    {
        public Service1(IRepository repository)
        {
            Console.WriteLine("Service1 created");
        }
    }
    
    class Service2 : IService
    {
        public Service2()
        {
            Console.WriteLine("Service2 created");
        }
    }
    
    class Repository : IRepository
    {
        public Repository()
        {
            Console.WriteLine("Repository created");
        }
    }
    
    class MainViewModel
    {
        public MainViewModel(IService service)
        {
            Console.WriteLine("MainViewModel created");
        }
    
        public void Foo()
        {
            var specificService = ServiceLocator.Current.GetInstance<IService>("MySpecificService");
        }
    }
    

    the output is:

    Repository created
    Service1 created
    MainViewModel created
    Service2 created
    

    Because you need a MainViewModel (maybe in SimpleIoC you need to register MainViewModel too, in Unity, it can resolve concrete classes without mapping), the container trying to create one, but it realizes that MainViewModel needs an IService, and it finds the default one from the mapping, which is Service1, but it realizes that Service1 needs an IRepository, and it finds the default one, so it can pass a Repository to the Service1 constructor, then the Service1 instance to the MainViewModel constructor. All the dependencies resolved.

    The Foo call is an example how you can register more than one type to the same interface. Dependency injection is a much more bigger topic, but auto-wiring is an important part of it.

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

Sidebar

Related Questions

:) This might look to be a very long question to you I understand,
This might look like a basic question to some of you but I expect
Well this question might look simillar to questions easily findable with google but for
This might look like a beaten question, but could not find an answer at
This might look as a stupid question. But, I have a class with some
This might look a repetition to my earlier question. But I think its not.
this question might look dumb but I need to have a clear idea when
Now this might look like a duplicate thread, but my question is that I
This question might not seem programming related at first, but let me explain. I'm
This question might be kind of elementary, but here goes: I have a SQL

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.