Does unity just combine with something that needs to be registered?

I watch Mike Tutley's amazing entry into Prism. It all made sense, but I was a little embarrassed in the sixth video.

It has a constructor for the view class that takes a ViewModel parameter. He then says that unity will fill it for us (i.e., Build it).

In the past, he had to register such things (i.e. IMyClass registered in MyClass). But he did not do this for the ViewModel.

The only difference I see is that the dependency on the ViewModel was not an interface, but rather a direct one. Unity sees this and is just trying to build a ViewModel or am I missing a step? (And if the ViewModel had parameters, would he try to make them too?)

+1
source share
2 answers

When a type can be created (i.e. not a base class or interface), unity can determine how this type is created without having to register type mapping.

Mappings are useful when the input type is not instantiable.

Hope this helps.

Thanks Damian

+2
source

Unity is trying to build a class into which allowed dependencies are embedded in the largest constructor (with the maximum number of parameters).

So, if you have a model like this:

public class ViewModel { public ViewModel(IMyDependency dependency) { .. } ... } 

you need to register IMyDependency .

+1
source

All Articles