(from the StockTraderRIBootstrapper.cs file in the Prism V2 StockTrader example app)
What is the difference between this:
ShellPresenter presenter = new ShellPresenter();
and this:
ShellPresenter presenter = Container.Resolve<ShellPresenter>();
something like this:
Container.RegisterType<IShellPresenter, ShellPresenter>();
but found it nowhere. So how does the container get to know about these types so it can .Resolve them? I rebuilt this in its own project and get a "Resolution of the dependency failed" error, where do I need to register this dependency then?
Any direction/discussion here would be helpful.
So, in the bootstrapper, when I register the Shell itself:
protected override void ConfigureContainer()
{
Container.RegisterType<IShellView, Shell>();
base.ConfigureContainer();
}
then the Container can resolve the ShellPresenter type. So how is the ShellPresenter type registered when I register the Shell type?
Ok, so it turns out that you don't have to register the type you are trying to resolve but you do have to register the parameter (interface) types passed to the constructor of the type you are trying to resolve, i.e. since I inject the IShellView interface into my ShellPresenter's constructor, I needed to register the IShellView type and not the IShellPresenter type:
public ShellPresenter(IShellView view) ...
I tested this by trying to resolve the type Tester:
Tester tester = Container.Resolve<Tester>();
As long as I inject SomeClass into its constructor:
public Tester(ISomeClass someClass)
I get unresolved dependency errors until I register SomeClass with the container:
Container.RegisterType<ISomeClass, SomeClass>();
Then it works. This is as surprising as it is educational. Needs to sink in. I'm going to go get a coffee and think about this for awhile.
If anyone can elaborate on why this is the case, it would be much appreciated.
If you try to resolve a concrete class and have not registered an instance or sub-class to satisfy it, then Unity will construct an instance of the concrete class for you, resolving any dependencies that it has.
So when you ask for ShellPresenter, and haven't registered it, Unity just new's up a ShellPresenter for you with the ShellView as a parameter.