I've been reading the following guide on Unity and dependency injections: https://msdn.microsoft.com/en-us/library/dn178463(v=pandp.30).aspx
and I've come across something that confused me:
Using the Unity container, you can register a set of mappings that determine what concrete type you require when a constructor (or property or method) identifies the type to be injected by an interface type or base class type. As a reminder, here is a copy of the constructor in the ManagementController class showing that it requires an injection of an object that implements the ITenantStore interface.
public ManagementController(ITenantStore tenantStore) { this.tenantStore = tenantStore; }The following code sample shows how you could create a new Unity container and then register the concrete type to use when a ManagementController instance requires an ITenantStore instance.
var container = new UnityContainer(); container.RegisterType<ITenantStore, TenantStore>();
The RegisterType method shown here tells the container to instantiate a TenantStore object when it instantiates an object that requires an injection of an ITenantStore instance through a constructor, or method, or property.
and finally:
var controller = container.Resolve<ManagementController>();
To instantiate the ManagementController and TenantStore objects, you must invoke the Resolve method.
This is one example they give. Howeever, soon after they give another example that seems completely different from this one.
container.RegisterType<ISurveyStore, SurveyStore>();
Here, the register type method is telling the container to instantiate a SurveyStore object when it instantiates an object that requires an injection of an ISurveyStore instance through a constructor, or method, or property.
It is the Resolve() method that confuses me:
var surveyStore = container.Resolve<ISurveyStore>();
In the first example, resolving meant that an object would be instantiated (the ManagementController class), and that a TentantStore instance implementing the ITenantStore interface would be passed to the ManagementController. However, here, instead of a concrete class being "resolved", it is ISurveyStore being resolved. What does this mean? A ISurveyStore instance can't be instantiated because it is an interface, so what is the concrete SurveyStore object being passed into? Does this look to be some sort of mistake? The two examples seem similar in terms of they both RegisterType() and Resolve(), but the actual thing that is being resolved seems completely different in the second example and makes no sense when comparing it to the first.