Autofac document: Metadata

Source: Internet
Author: User
Autofac document directory
    1. Start
    2. Registering Components
    3. Control scope and lifecycle
    4. Structured autofac with modules
    5. Xml configuration
    6. Integration with. net
    7. Deep understanding of autofac
    8. Guidance
    9. About
    10. 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.