Custom Dependency Resolver
Plumier uses global factory method to create instance of framework components such as Controller, Middleware, Custom Validator, Custom Authorizer etc. This factory method can be used as the composition root when using custom dependency injection framework.
#
Custom Dependency ResolverBy default Plumier doesn't have a proper dependency injection functionality, but Plumier has an extension point to possibly extend its functionality by using Custom Dependency Resolver.
#
SignatureThe signature of Plumier Dependency Resolver is very simple and straightforward, it simply like below
#
Example UsageThis example is a show case how you can create custom DependencyResolver
and use an IoC container library to resolve controller's dependency.
This example uses My Own IoC Container, its a light weight zero dependency IoC container library. The source code can be copy pasted to your project and become the part of your project.
By default Plumier will need a parameterless constructor because it doesn't have dependency injection capability. The controller is like below:
AnimalsController
dependent to AnimalRepository
from its constructor parameter. Note that the repository
parameter decorated with @inject.name("repository")
means its will automatically injected with instance of object registered as repository
. the AnimalRepository
is an interface with contract like below
Somewhere inside the project, implementation of AnimalRepository
is like below, furthermore we will register this repository by name as repository
so it will injected properly into the AnimalsController
.
Next we need to glue the object and its dependency by using the Inversion of Control container, here we will glue them together inside the custom dependency resolver.
Thats all the configuration you need, next you need to register the custom resolver into the Plumier application.