-
Notifications
You must be signed in to change notification settings - Fork 5
The dependency resolver
In MGR.CommandLineParser, all extensibility points are resolved through the dependency resolver.
The dependency resolver is built around two interfaces and two classes:
-
MGR.CommandLineParser.IDependencyResolverScope
: contains two method to resolve dependencies: ResolveDependency (which resolve a dependency as unique) and ResolveDependencies (which resolve a dependency as a collection). -
MGR.CommandLineParser.IDependencyResolver
: extendsIDependencyResolverScope
to add a method to create a new scope. -
MGR.CommandLineParser.DependencyResolver
: Provides a property to gets the currentIDependencyResolver
and a method to sets it. -
MGR.CommandLineParser.DefaultDependencyResolver
: provides a default implementation ofIDependencyResolver
andIDependencyResolverScope
.
The default dependency resolver allows you to register a custom implementation by calling the static method RegisterDependency, RegisterDependencies, one of the overload of RegisterConverter or DeleteConverter.
DeleteConverter allows you to remove a converter from the list of registred converter.
RegisterConverter allows you to register a new converter. The default dependency resolver allows only one converter for a TargetType
. You can overwrite a converter via the parameter overwriteExisting
.
The RegisterDependenc(y|ies)
methods take as parameter a Func<Func<IDependencyResolverScope, T>>
. The outer Func
will be called everytime a new scope is created, and the inner Func
will be called everytime the specified dependency should be resolved. The inner Func
takes the current IDependencyResolverScope
as parameter and should resolve the dependency for this scope. These methods are generic and the generic parameter must be the type of the interface of the dependency and not the type of the implementation.
You can change the current dependency resolver by calling DependencyResolver.SetDependencyResolver
and passing your own instance of IDependencyResolver
to it. A new scope is created by the parser (by calling IDependencyResolver.CreateScope
) everytime the IParse.Parse
method is called.