catel 4.0 CustomViewModelFactory

Topics: Questions
Jun 12, 2014 at 8:34 AM
Instead of GetViewModelInstance I registered my own ViewModelFactory.
 public class CustomViewModelFactory : ViewModelFactory
        public CustomViewModelFactory(ITypeFactory typeFactory)
            : base(typeFactory)

       public override IViewModel CreateViewModel(Type viewModelType, object dataContext)
             // create viewmodels and return
              var vm = CreateCustomViewModel();

              if vm != null
                   return vm;

            // Fall back to default behavior
            return base.CreateViewModel(viewModelType, dataContext);
and register:
ServiceLocator.Default.RegisterType<IViewModelFactory, CustomViewModelFactory>();
But neither the constructor nor the method CreateViewModel() of my CustomViewModelFactory is ever called.
Jun 12, 2014 at 1:38 PM
The constructor of LogicBase is static. Its called bevor I can register any CustomViewModelFactory in the ServiceLocator.
The IViewModelFactory is resolved in this constructor. So _viewModelFactory is always ViewModelFactory of catel.
static LogicBase()
       IDependencyResolver resolver = IoCConfiguration.get_DefaultDependencyResolver();
      _viewModelFactory = DependencyResolverExtensions.Resolve<IViewModelFactory>(resolver, null);
      _viewManager = DependencyResolverExtensions.Resolve<IViewManager>(resolver, null);
      _dependencyPropertySelector = DependencyResolverExtensions.Resolve<IDependencyPropertySelector>(resolver, null);
Jun 13, 2014 at 7:09 AM
Add a issue?
Jun 13, 2014 at 9:06 AM
Yes, and a PR after that ;-)
Jun 13, 2014 at 10:54 AM
Edited Jun 13, 2014 at 10:58 AM
Pull Request?! You can not delete GetViewModelInstance() method in view but don't provide a working alternative. :-(
Jun 13, 2014 at 10:59 AM
"You can not delete GetViewModelInstance() method in view but don't provide a working alternative. :-("

I am very sorry to hear that you feel this as a breaking change. I do everything in my power to support everybody, but paying customers have more priority to me, and they requested a simpler model. And we do provide working alternatives, the IViewModelLocator.
Jun 13, 2014 at 2:00 PM
Yes I understand that paying customers have more priority.

Its only not understandable for me why delete GetViewModelInstance bevor the CustomViewModelFactory is tested.

To the IViewModelLocator.

I have two views DetailView and GeneralView and One ViewModel.

But i have a lots of Instances of this views and viewmodel.

So it depends on the model if a new viewmodel should be created or a existing viewmodel should be used.

Thats my code in View:
        protected override IViewModel GetViewModelInstance(object dataContext)
            if (dataContext is PlaceInstance)
                var model = dataContext as PlaceInstance;

                var manager = ServiceLocator.Default.ResolveType<IViewModelManager>();

                var vm = manager.GetViewModelsOfModel(model).SingleOrDefault();

                return vm;

            return null;
Thats also easy to handle in ViewModelFactory. But how to handle in ViewModelLocator??
Jun 13, 2014 at 2:24 PM
If you need custom lifetime management, you will indeed need to manage this through the ViewModelFactory (you were doing it "wrong" before, but I introduced the possibility to "hack" it into your views, so you can partly blame it on me).


1) You have located the issue
2) You can thought of a fix
3) You need this feature

Why don't you contribute to the project by creating a pull request? Is it too much to ask?
Jun 16, 2014 at 12:15 PM

Ok. Never do that before. What I have to do?
  1. Fix the bug like I think its correct.
  2. Commit it on Github
  3. Set the issue to resolved. Do I have permissions to do that?
Jun 16, 2014 at 12:22 PM
1) Fork
2) Clone it to your disk
3) Customize code
4) Commit & push
5) Create pull request
6) The build server will build & run all. Then I can review and accept it.
Jun 16, 2014 at 1:34 PM
Edited Jun 16, 2014 at 1:35 PM

1) Forked
2) Cloned,
3) Customized
4) Commited (to early). I do not typed in the whole comment. :-( Sorry.
4) Pushed.
5) I it possible to edit the commend before I create the pull request?
Jun 16, 2014 at 1:42 PM
Not that I am aware of, but shouldn't matter much. Now you can go to this page:

Then create pull request, make sure to use the "compare across forks" link so you can compare your code to the Catel codebase.
Jun 16, 2014 at 2:00 PM

Created a pull request. Hope everything was correct.

And next time its a little bit easier for me to create a new pull request.
Jun 16, 2014 at 2:17 PM
Pull request accepted, thanks! And see, once you are through it, it ain't that hard ;-)