Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use MVVMLight SimpleIoc? [closed]

I'm revamping my software which has messy Messenger.Default(...) bits.

Is there any cheat sheet to know MVVMLight SimpleIoc usage (not general IoC description)?

like image 777
Youngjae Avatar asked Dec 10 '12 05:12

Youngjae


People also ask

What is the use of classlocation in MVVM light?

To eliminate the need for projects associated with the backend to have a reference to the MVVM Light projects, this ClassLocation class provides an interface to any methods that are required. Since this class is used for all locator services, it is possible to change from the default SimpleIoc.

What do you like most about MVVM light?

Another useful functionality provided by MVVM Light is Messenger. I had previously worked with Messenger, and I do like the ability to have the decoupling provided by Messenger or the EventAggregator (Prism), but I am sure they come with the performance penalty, and are also harder for somebody to come in cold and work with.

What is the best IoC container for MVVM light?

For instance, Unity (by Microsoft), StructureMap and Castle Windsor (two open source projects) are very popular .NET-based IOC containers and available for multiple platforms. In this article, I’ll use MVVM Light’s SimpleIoc to illustrate the usefulness of an IOC container in MVVM-based applications.

What if a method or property is not listed in mvvmlight?

Where a method or property isn't listed, there is a direct replacement with the same name in the MVVM Toolkit and there is no change required. The first change here will be swapping using directives in your components. MvvmLight uses weak references to establish the link between the command and the action called from the associated class.


1 Answers

SimpleIoc crib sheet:

1) You register all your interfaces and objects in the ViewModelLocator

class ViewModelLocator  {      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>();                           SimpleIoc.Default.Register<SecondViewModel>();      }             public MainViewModel Main      {           get           {                   return ServiceLocator.Current.GetInstance<MainViewModel>();           }      } }  

2) Every object is a singleton by default. To resolve an object so that it's not a singleton you need to pass a unique value to the GetInstance call:

SimpleIoc.Default.GetInstance<MainViewModel>(Guid.NewGuid().ToString()); 

3) To register a class against an interface:

SimpleIoc.Default.Register<IDataService, Design.DesignDataService>();   

4) To register a concrete object against an interface:

SimpleIoc.Default.Register<IDataService>(myObject);      

5) To register a concrete type:

SimpleIoc.Default.Register<MainViewModel>();    

6) To resolve an object from an interface:

SimpleIoc.Default.GetInstance<IDataService>(); 

7) To resolve an object directly (does buildup and dependency resolution):

SimpleIoc.Default.GetInstance<MainViewModel>(); 

8) MVVM makes doing design-time data really easy:

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

If you're in design-time mode it will automatically register your design-time services, making it really easy to have data in your viewmodels and views when working in the VS designer.

Hope this helps.

like image 150
Faster Solutions Avatar answered Sep 20 '22 08:09

Faster Solutions