Autofac official document translation - a registered component --2 registration parameters passed

Official documents: http: //docs.autofac.org/en/latest/register/parameters.html

Two, Autofac pass registration parameters

When you register components to provide a set of parameters, you can use the service-based component in the analysis. (If you prefer to provide parameters when parsing, you can do so).

1, valid parameter type (Available Parameter Types)

Autofac provide a number of different parameters matching strategies:
  • NamedParameter - matching target parameters by name
  • TypedParameter - by type matches the target parameters (exact match is required type)
  • ResolvedParameter - Flexible parameters match
NamedParameter And TypedParameter value provided only constant
ResolvedParameter A method for providing dynamic can be used as a value retrieved from a container, such as by name resolution services.

2, a reflection component parameters ( the Parameters with the Reflection Components )

When registering the reflection component, this type of constructor may require a parameter that can not be resolved from the container, you need to use a parameter value provided at the time of registration.
 
Copy the code
ConfigReader class public: IConfigReader 
{ 
  public ConfigReader (String configSectionName) 
  { 
    node name storage configuration // 
  } 

  // ... read the configuration based on the node name 
}
Copy the code
You can register to use lambda expressions:
 
builder.Register(c => new ConfigReader("sectionName")).As<IConfigReader>();
Or you can pass in a reflective component registration parameters:
Copy the code
// use a named parameter: 
builder.RegisterType <ConfigReader> () 
       .as <IConfigReader> () 
       .WithParameter ( "configSectionName", "sectionName"); 

// use a type parameter: 
builder.RegisterType <ConfigReader> () 
       . AS <IConfigReader> () 
       .WithParameter (new new TypedParameter (typeof (String), "sectionName")); 

// use a resolution parameters: 
builder.RegisterType <ConfigReader> () 
       .as <IConfigReader> () 
       .WithParameter ( 
         new new ResolvedParameter ( 
           (PI, CTX) => pi.ParameterType == typeof (String) && pi.Name == "configSectionName", 
           (PI, CTX) => "sectionName"));
Copy the code

3, Lambda Expression component parameters ( the Parameters with the Lambda Expression Components )

Component registration using the expression, instead of passing parameters at the time of registration, you can pass parameters when parsing service.
In the component registration expressions, you can use the delegate signature passed parameters by changing the registration, instead of just receiving a IComponentContext parameter, a IComponentContext and IEnumerable <Parameter> parameters:
 
// 使用两个参数来注册委托
// c = The current IComponentContext to dynamically resolve dependencies
// p = An IEnumerable<Parameter> with the incoming parameter set
builder.Register((c, p) =>
                 new ConfigReader(p.Named<string>("configSectionName")))
       .As<IConfigReader>();

 When you parse the parameters, you lambda will use these parameters to pass values:

var reader = scope.Resolve<IConfigReader>(new NamedParameter("configSectionName", "sectionName"));
 
 

Guess you like

Origin www.cnblogs.com/nimorl/p/12626467.html