In this blog post we are going to introduce how to use Bing Maps REST services by using the Bing Maps REST toolkit and Dependency Injection, an object-oriented design pattern that allows the development of loosely coupled code and makes it maintainable and easy to update. This example is focused on the APIs development. For simplicity we are only going to use the distance matrix request but you can use any other service offered by Bing Maps with this same structure.
Dependency Injection pattern involves four basic roles: a service to be used, an interface that defines how the client may use the service, a client that is depending on the service it uses and an injector responsible to constructing the services and injecting them to the client.
First create a service that receives as parameter a DistanceMatrixRequest Bing Maps object with its corresponding interface.
Then create a controller as client with constructor injection and a factory for requests.
We have used a factory pattern for creating any Bing Maps request without the need of passing the API key as parameter. With this example we can deal with the problem of creating any Bing Maps request objects with the corresponding key without having to specify the exact class of the object that will be created.
In addition, components have be registered with Autofac by creating a ContainerBuilder and informing the builder which components expose which services. In this case we just add the distance matrix service. It is also included the Bing Maps api key from a settings file.
In this way we are capable of create our own basic routing algorithms by using the Bing Maps distance matrix. However, the possibilities are extended if we add any other service offered.