My favorites | Sign in
Logo
                
Search
for
Updated Aug 01, 2009 by nicholas.blumhardt
ResolveParameters  
Parameters can be used when registering and resolving components.

Passing Parameters to Resolve()

Resolve accepts parameters using a variable-length argument list:

var fred = Resolve<Person>(new NamedParameter("name", "Fred"));

This will map automatically to the named constructor parameters on the implementation class, if it was registered using Reflection, e.g.:

class Person
{
  public Person(string name)
  ...

Available Parameter Types

Autofac offers several different parameter matching strategies:

NamedParameter and TypedParameter can supply constant values only.

ResolvedParameter can be used as a way to supply values dynamically retrieved from the container, e.g. by resolving a service by name.

Utilising Parameters from an Expression

If Person is to be registered using an expression, the parameters can be accessed from a second available delegate parameter of type IEnumerable<Parameter>:

builder.Register((c, p) => new Person(p.Named<string>("name")));

Delegate Factories

Check out the delegate factory feature for a cleaner way to create components that require parameters.


Sign in to add a comment
Hosted by Google Code