0

I have an application using raw MVVM (without a framework). My ViewModels are a list that the MainViewModel manages, but there are some view models that need specific information (like user account, name etc...) that I get from a web service, so I've created a service that does this for me, I get the user account, username and other stuff and since my view models need this information I've created a factory that return the ViewModels object and insert it in the collection that the MainViewModel holds and in the constructor I pass the service as an interface.

Example:

SelectionViewModel.cs

public SelectionViewModel(IClient provider)
{
    _provider = provider;
    _configurationRepository = new ConfigurationRepository();
}

public SelectionViewModel(IClient provider, IPageFactory factory)
{
    _provider = provider;
    _factory = factory;
    _configurationRepository = new ConfigurationRepository();
}

Factory.cs

IPageViewModel IPageFactory.Create(Type type) 
{
    var page = Activator.CreateInstance(type) as IPageViewModel;
    if (page != null)
        return page;
    else
        return (IPageViewModel)Activator.CreateInstance(typeof(OutOfServiceViewModel));
}

IPageViewModel IPageFactory.Create(object [] parameters, Type type)
{
    IPageViewModel page;
    if (type.GetConstructor(Type.EmptyTypes) != null)
        page = Activator.CreateInstance(type) as IPageViewModel;
    else
        page = Activator.CreateInstance(type, parameters) as IPageViewModel;

    if (page != null)
        return page;

    return (IPageViewModel)Activator.CreateInstance(typeof(OutOfServiceViewModel));
}

My question is if there's a better way to send the parameters to the constructors or a better way at all to solve my problem (pass the service with user information between ViewModels)

Miguel
  • 1,157
  • 1
  • 15
  • 28
  • 1
    Unity Container (part of Prism) can be used on its own to do this. That's what it was made for, so it's worth having a look at... – Gayot Fow May 15 '14 at 01:00
  • 1
    Thanks, totally forgot the basics of DI. You answer and this question http://stackoverflow.com/questions/14301389/why-does-one-use-dependency-injection put me back on track. – Rene Durazo May 15 '14 at 16:23

0 Answers0