Skip to content

Resolve By Parameter Name

Onur EKER edited this page Mar 6, 2017 · 3 revisions

This feature provides to ability to resolve any class, if only constructor parameter is matched with registration name

This is the one the most needed feature in registration too. However there is a legal way registration with DependencyKey, it is hard to re-factor and register.

Apply resolver

Extension library solves this need just by adding ResolveByNameConvention resolver.

container.Kernel.Resolver.AddSubResolver(new ResolveByNameConvention(container));

Registration

Component can be registered in many ways;

  • Single component registration with explicit naming
Component
    .For<ILogger>()
    .ImplementedBy<ConsoleLogger>()
    .Named("consoleLogger")
  • Single component registration with auto-naming extension (NamedAsParameter)
Component
    .For<ILogger>()
    .ImplementedBy<TraceLogger>()
    .NamedAsParameter()
  • Multiple component registration (BasedOnDescriptor) with auto-naming extension (NamedAsParameter)
Classes
	.FromAssemblyInThisApplication()
	.BasedOn(typeof(ILogger))
	.WithService
	.FromInterface()
	.NamedAsParameter()

Usage

Any class can be resolved if constructor parameter is matched with registration name;

public ResolveByNameDemo(ILogger consoleLogger, ILogger traceLogger)
{
    this.logger = logger;
    this.consoleLogger = consoleLogger;
    this.traceLogger = traceLogger;
}

Default implementation

It is also supported to fallback default implementation for service usage;

  • Single component registration: Implementation can be set as usual way (IsDefault)
Component
    .For<ILogger>()
    .ImplementedBy<ConsoleLogger>()
    .Named("consoleLogger")
    .IsDefault()
  • Multiple component registration (BasedOnDescriptor): Can be set with supported extension method by this library (DefaultIs)
Classes
    .FromAssemblyInThisApplication()
    .BasedOn(typeof(ILogger))
    .WithService
    .FromInterface()
    .NamedAsParameter()
    .DefaultIs<ConsoleLogger>()

Sample registrations is provided in Demo application

Clone this wiki locally