Should I register ViewModels in Container and resolve from there?
Benefits:
Drawbacks:
What's the right answer? I'd prefer to register if I could mitigate lifetime drawback.
I'm using Caliburn and Autofac if it matters.
A container is an ecosystem inhabited by the objects it creates. View models interact with those inhabitants and thus are also part of the ecosystem. To accurately reflect that relationship, you should register view models in the container.
You should always use InstancePerDependency
with view models. A view model represents the state and behavior of a particular piece of UI - it is the non-framework-specific analogue of a control. Just as you can't generally place the same control instance in two locations in a UI tree, you can't also reuse the same view model instance.
If you could, we'd call it a ViewsModel
:-)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With