60

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)?

Youngjae
  • 24,352
  • 18
  • 113
  • 198
  • What problem exactly are you facing? What you are unable to do in SimpleIOC? – Haris Hasan Dec 10 '12 at 05:55
  • 1
    @HarisHasan // Messenger in MVVMLight has a lot of tutorials out there and I used handy, but I hardly find how to adapt and use MVVMLight SimpleIoC. I already know what IoC is, but just need to know MVVMLight SimpleIoC grammatically with code example. – Youngjae Dec 10 '12 at 06:06
  • While the OP literally asks for an off-site resource in reality the answer to the question does not require one as we can see below. Not sure how to re-word but I am sure it is re-wordable as to not sound off-topic. Voting to reopen. – Shafik Yaghmour Apr 13 '15 at 02:25
  • @ShafikYaghmour // agree. my question is not about programming book or library recommendation. I was asking about a class usage of MVVM-Light project which is little documented. – Youngjae Apr 13 '15 at 02:28
  • I would suggest re-wording to avoid the mention of tutorials. – Shafik Yaghmour Apr 13 '15 at 02:53
  • @ShafikYaghmour // did re-wording as you suggested. thanks. – Youngjae Apr 13 '15 at 02:55
  • @Pang // title editted. thanks. – Youngjae Apr 15 '15 at 02:30

1 Answers1

145

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.

starball
  • 20,030
  • 7
  • 43
  • 238
Faster Solutions
  • 7,005
  • 3
  • 29
  • 45
  • Re: 1) I found SimpleIoc throws design time exception because service is already registered. If I place check for IsRegistered, and then reference SimpleIoc in my view model then design time data won't show (no exception thrown, just doesn't show). If I remove SimpleIoc and manually create data then it shows up in design time. So, no I don't thinks it's "really easy" :) Note that data service works fine outside of design time. – si618 Dec 14 '12 at 06:32
  • 2
    Ahh...changing to static ctor in ViewModelLocator fixes "service already registered" exception. – si618 Dec 14 '12 at 06:40
  • I would like to know if order of registering Services and ViewModels matter? – komizo Sep 16 '14 at 19:46
  • No, it doesn't. The SimpleIoc will attempt to resolve the object graph when you request an object from it. as long as all that object' dependencies are in place it doesn't matter in what order they were added to the SimpleIoc, they'll be resolved correctly – Faster Solutions Sep 17 '14 at 07:58
  • 2
    Method 4) does not seem to be supported anymore...Is there any option to do that? Currently I'm enclosuring the singleton instance in a lambda function and passing it to the SimpleIoc factory register function. – Arthur Nunes May 24 '16 at 15:57
  • 1
    @ArthurNunes check this out. http://stackoverflow.com/questions/35586715/how-to-register-a-class-instance-including-a-parameter-in-simpleioc – Terrance Mar 01 '17 at 23:02