IUnityContainer.Resolve<T> and runtime parameters

Topics: Prism v2 - WPF 4, Prism v4 - WPF 4
Jun 4, 2010 at 5:50 AM

Hello!

Is it possible to pass additional parameters to IUnityContainer.Resolve<T> method?

 

Let's say I've got UserEditViewMovel with the following constructor:


public class UserEditViewModel : IUserEditViewModel{

public UserEditViewModel(IUnityContainer container, IEventAggregator eventAggregator, IDataService dataService, User currentUser)
{
            this.dataService = dataService;
            this.currentUser = currentUser;
            ...
}

...
}

Inside another view model (lets call it IUserListViewModel) which operates on users list I'd like to invoke user edit functionality:

public class UserListViewModel : IUserListViewModel
{
  public void EditCurrentUser(){
    IUserEditViewModel model = container.Resolve();
    model.EditCurrentUser();
  }
}

Is there any way to provide parameter for Resolve() method?


 

Jun 4, 2010 at 5:55 AM

If there is no such a possibility, maybe there exists some extensions?

The key point is that I have to inject currentUser argument to UserEditViewModel. And this argument is dynamically changed during runtime. 

For example there is list with users. When I double click on some user inside list, new IUserEditModel must be resolved with currentUser passed to contructor. 

Jun 12, 2010 at 7:08 PM

up thread, Still actual

Developer
Jun 14, 2010 at 5:08 PM

Hi,

One of the benefits of using a dependency injection container is that it gives the possibility for the caller (in this case, the UserListViewModel) to be unaware of the dependencies of the object you are resolving, since the container is responsible of injecting those dependencies. With that in mind, it wouldn’t be appropriate to pass a dependency as an argument to the Resolve<T> method. The correct approach for your scenario is annotating those dependencies for constructor injection, like you’ve done, so that the dependencies are injected to the object when it is created.

When you call the Resolve or Resolve<T> method, you need to specify the type or the interface your object inherits from. You might find this article useful. If you are looking for the Resolve<T> method, it is located in the Unity extensions, as explained on this thread. If you used the generic one, the code for resolving IUserEditViewModel would look like this:

IUserEditViewModel model = container.Resolve<IUserEditViewModel>();

Take into account that, in order for the container to be able to resolve an object of certain type, you need to have the type or type mapping registered beforehand. If you haven’t specified a value for the Lifetime Manager, a new instance is created each time you resolve it, which is the behavior you are asking for. You can read more about this in this thread.

For other unity specific question, you might find better support in the Unity forum.

I hope you find this helpful.

Guido Leandro Maliandi
http://blogs.southworks.net/gmaliandi