From my understanding, prism’s unity container can resolve types event if they have not been registered, does this make _container.RegisterType kinda useless ?
Thanks!
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If I am understanding your question correctly, what you are seeing is that Unity can (try to) make instances of classes directly, which is different from resolving types. It is perfectly reasonable to ask Unity to create a class “directly”, however, in order to leverage “Inversion of Control”, you would normally ask a container to resolve an interface, where you have mapped an interface to a class via
RegisterType. This way you can map different implementations of classes to interfaces, without having to change your code, that is “Inversion of Control” and “Interfaced-based Programming” at work.This process involves you asking to resolve an interface, followed by Prism finding what is bound to the interface i.e. resolution, and then making an instance for you i.e. factory capabilities. The factory capabilities of Unity will ensure that any other dependencies are resolved that are required to make an instance of the resolved class e.g. using dependency injection on class constructor parameters. This whole process is recursive until all dependencies are resolved.
For Example
If you ask for an
IFooand it is boundFoo, Unity will try and make an instance ofFoo. IfFoohas a constructor which takes anIBar, Unity will try and resolveIBarand create an instance of this to use in the constructor forIFoo.So in the following code:
We can resolve
IFooas described above.We can make an instance of class
Bardirectly, as it has no dependencies.We can make an instance of class
Foodirectly, as it has a dependency onIFoo, but we have registered it.We cannot make an instance of
Woodirectly as there is no registration forIYay.In the example above
RegisterTypeis used to map a concrete implementation to an interface. It is at this point that we can map any implementation we want and this will ripple throughout our program as long as the container is always used to resolve types.For example, if we change what
IBaris mapped to, then any timeIFoois resolved it will be created with that different implementation ofIBar. This gives us a substantial way of altering a program’s behaviour by just changing a single line of code i.e.RegisterType.