0

I have an application with user-login which representates a "lifetime" object till the user logs out. I'm using MVVM Light but don't know how to use the SimpleIoC (with the Messenger) correctly.
Currently I save the object in the MainViewModel as CurrentUser Property of my custom User object and pass it to other ViewModels via constructor and save it there as a Property too. But with that I have the issue, that at some places the object gets overwritten / created new.

This is probably the wrong way to do it as i figured out that the SimpleIoC can simply return the registered instances with GetInstance<>() and if needed a Key.
So the right way is it to create a User object with Key to get it in needed classes, modify and save it so the other classes can get the updated value when calling (or PropertyChanged possible?), right?

I tried to create it like that, but the object is obviously not updated:

public MainViewModel()
{
    RegisterMessenger();

    CurrentUser = SimpleIoc.Default.GetInstance<User>("123");
    _dataService = SimpleIoc.Default.GetInstance<IDataService>();

    Messenger.Default.Send(new NotificationMessage("GoToLoginPage"));
}

public User CurrentUser
{
    get { return _currentUser; }
    set { Set(ref _currentUser, value); }
}

public User CurrentPageViewModel
{
    get { return _currenPageViewModel; }
    set { Set(ref _currentPageViewModel, value); }
}

public void RegisterMessenger()
{
    Messenger.Default.Register<NotificationMessage>(this, message =>
    {
        switch (message.Notification)
        {
            case "GoToLoginPage":
                if (_loginViewModel == null)
                    _loginViewModel = new LoginViewModel(_dataService);

                CurrentPageViewModel = _loginViewModel;
                break;

            // Other cases...
        });
    }
}

// -------------------------------------------------------------------------

public LoginViewModel(IDataService dataService)
{
    UserObj = SimpleIoc.Default.GetInstance<User>("123");
    _dataService = dataService;

    LoginCommand = new RelayCommand(LoginExecute, LoginCanExecute);
}

Would it also be better, if I call the ViewModels with GetInstance<SomeViewModel>().. but then, how I pass paramters to them and let them stay alive / recreate / destroy?

Would be nice if someone can point me out the right way, thanks!

  • Mind checking [this](http://stackoverflow.com/a/13805067/3881866) with regards to MVVM Light SImpleIoC – John Ephraim Tugado Feb 19 '16 at 09:57
  • @JohnEphraimTugado I read this already, but doesn't get the context of it how I could use it correclty. When getting an Instance and save it to a Property, only a copy get's saved, but how I save / change the registered one then? `...GetInstance(Key).SomeProperty = value` doesn't seem correctly to call it every time. –  Feb 19 '16 at 10:02
  • You're doing a few things wrong, you should read about [Dependency Injction](http://www.martinfowler.com/articles/injection.html) and [Inversion of Control](http://www.hanselman.com/blog/ListOfNETDependencyInjectionContainersIOC.aspx) (IoC) containers – Lionel Pire Feb 19 '16 at 10:19
  • @LionelPire Thanks for the articles, I'll read them. –  Feb 19 '16 at 10:29
  • 1
    I am not sure about MVVM Light SimpleIoC but I use a similar one from MVVM Cross framework. You just modify the value. EX: var a = xx.GetInstance(); a.PropA = "cc"; When you get the same instance on another part of your project you will notice that PropA has become "cc". Try and see if this is the same with SimpleIoC – John Ephraim Tugado Feb 19 '16 at 11:21

0 Answers0