Autofac document directory
- Start
- Registering Components
- Control scope and lifecycle
- Structured autofac with modules
- Xml configuration
- Integration with. net
- Deep understanding of autofac
- Guidance
- About
- Vocabulary
Metadata
Autofac provides a mechanism to create and use component metadata
If you are familiarManaged extensibility framework (MEF)You may have seen the example of component metadata.
Metadata stores information about this component in component and can be accessed without creating an instance.
Add metadata during registration
The metadata of the value description is associated with component in the registration phase. Each metadata is a key-Value Pair:
Builder. Register (C => New screenappender (). As <ilogappender> (). withmetadata ("appendername", "screen ");
Can be expressed
<Component type = "MyApp. components. logging. screenappender, MyApp "service =" MyApp. services. logging. ilogappender, MyApp "> <metadata> <item name =" appendername "value =" screen "type =" system. string "/> </metadata> </component>
Use metadata
It is not used for general attributes. Metadata and component are independent quotas.
This makes it very useful to select one from many components under running conditions, or when metadata is not an inherent attribute of the component instance. Metadata can be expressedItaskExecution time,OrIcommandButton title.
You can also useMetaUse metadata
Public class log {readonly ienumerable <meta <ilogappender> _ appenders; Public log (ienumerable <meta <ilogappender> appenders) {_ appenders = appenders ;} public void write (string destination, string message) {var appender = _ appenders. first (A =>. metadata ["appendername"]. equals (destination); appender. value. write (Message );}}
You can useMeta <lazy>OrThe. NET 4 lazy <t, tmetadata>You can also access metadata without creating a component.
Strongly typed metadata
To prevent the use of strings to describe the metadata, you can specify a readable attribute for each metadata.Iappendermetadata:
Public interface iappendermetadata {string appendername {Get ;}}
You can useWithmetadataTo associate values with the overload method:
Builder. register (C => New screenappender ()). as <ilogappender> (). withmetadata <iappendermetadata> (M => M. for (AM => AM. appendername, "screen "));
(Here is a more advanced example)
InNet4.0, Autofac support Meta <t, tmetadata>AndSystem. Lazy <t, tmetadata>The read value of the punch type interface.
Public class log {readonly ienumerable <lazy <ilogappender, ilogappendermetadata> _ appenders; Public log (ienumerable <lazy <ilogappender, Handler> appenders) {_ appenders = appenders ;} public void write (string destination, string message) {var appender = _ appenders. first (A =>. metadata. appendername = destination); appender. value. write (Message );}}
Note the use of the strong type attribute appendername
Registration and use of metadata are separated, so you can use strong-type metadata through the weak type technology, and vice versa.
Note:: In autofac1.4, "extended properties" is metadata, which has been replaced by metadata.