Skip to content

MVVM ViewModelFactory

Jan M edited this page Mar 14, 2021 · 8 revisions

As the name suggests, the ViewModelFactory is used to create ViewModel instances.
For this purpose it provides a number of registration methods, which are presented on this page.
Internally, the factory uses the IDependencyContainer interface to register types, create instances and resolve dependencies.
That's why an instance of this type must be passed to the ViewModelFactory via the Configure method before it can be used:

IDependencyContainer container;
ILogService log;

// providing an ILogService instance is optional
ViewModelFactory.Configure(
    container,
    log);

Registering

There are two methods to register ViewModel types in the factory:

  1. Register<TInterface, TImplementation>
    Use this method if you have an interface for your ViewModel type and want to resolve it later using the interface.
    TInterface must implement CodeMonkeys.MVVM.IViewModel, TImplementation must implement TInterface

  2. Register<TImplementation> where TImplementation : class, IViewModel
    With this method you can just register a ViewModel type without having to create interfaces.
    TImplementation must implement CodeMonkeys.MVVM.IViewModel

ViewModelFactory.Register<IMainViewModel, MainViewModel>();
ViewModelFactory.Register<LoginViewModel>();
ViewModelFactory.Register<UserProfileViewModel>();

By default, registering a ViewModel via the factory will result in InitializeAsync being called when resolving it.
In case you want to disable it for certain types, you can use the DontInitialize() extension method in fluent style:
ViewModelFactory.Register<UninizializedViewModel>().DontInitialize();

It is also possible to register a specific model type for a ViewModel to later resolve the matching ViewModel by passing the model instance:

ViewModelFactory.Register<UserProfileViewModel>().WithModel<User>();

var userProfileViewModel = await ViewModelFactory.ResolveFromModelAsync<User>(userObject);

Please be aware that in this case, IUserProfileViewModel must implement IViewModel<User> and InitializeAsync<User> is called!

Resolving

There are different possibilities to resolve a ViewModel instance:

  1. Resolve<T> where T : class, IViewModel --> return T
  2. Resolve(Type) --> return IViewModel
  3. ResolveAsync<T> where T : class, IViewModel --> return Task<T>
  4. ResolveAsync(Type) --> return Task<IViewModel>
  5. Resolve<TViewModel, TData>(TData) where TViewModel : class, IViewModel<TData> --> return TViewModel
  6. Resolve<TData>(Type, TData) --> return IViewModel<TData>
  7. ResolveAsync<TViewModel, TData>(TData) where TViewModel : class, IViewModel --> return Task<TViewModel>
  8. ResolveAsync<TData>(Type, TData) --> return Task<IViewModel<TData>>
  9. ResolveFromModelAsync<TViewModel, TModel>(TModel) where TViewModel : class, IViewModel<TModel> --> return Task<TViewModel>
  10. ResolveFromModelAsync<TModel>(TModel) --> return Task<IViewModel<TModel>>
  11. ResolveFromModelAsync<TViewModel>(object) where TViewModel : class, IViewModel --> return Task<TViewModel>
Clone this wiki locally