Import and export of WPF prism classes, properties, and methods

Source: Internet
Author: User

Learning Prism must master the application of dependency injection, only understanding the prism of dependency injection to better use prism to enhance the application development architecture.

First, there are two ways of prism dependency injection, as well as MEF and unity, which are two DLLs that are not associated in prism. I tend to use MEF to learn the specific implementation of MEF in Silverlight below. See the MEF implementation diagram first

1, Catalog: in order to find the components that can be used to assemble the container, the composite container will use "Catalog". a directory is an object through which the available parts are found, whichMEF provides to create a catalog from the provided type, assembly, or disk path

2. Compose (combination): In MEF, the container imports and exports match the process that we call a combination, the part is composed of MEF, MEF instantiates the part, and then the exporter matches the importer.

3, part (parts): Through MEF, an application can discover and inspect a part through its metadata, without instantiating the part or even the assembly that loads the part. You can specify 1 or more export and import in a part.

4. Export: In MEF by adding the Export property label to a class or attribute, the object can be introduced by other parts.

5. Import: The object instance that satisfies the condition is imported by applying to container.

The export contract is required for import, otherwise it will fail.

According to the MEF Convention, any class or interface implementation can use the [System.ComponentModel.Composition.Export] property to define other assembly parts (Composable Parts). Any place where a composition part needs to be imported can be communicated through a contract (contracts) by using [System.ComponentModel.Composition.Import] to implement a combination of parts on a particular composition object property.

All combinations in the MEF require a matching contract, which can be a string or a type, and each export needs to declare a contract, and each import can define the same contract to match. By default, a match is made by type. If you specify a name in export, it is matched by name. It is highly recommended to use contract names for matching, and contract names can be added to namespaces, which makes matching more accurate and convenient.

See below for examples from http://mef.codeplex.com/

1. Object Injection

[Export]  Public class Somecomposablepart {  ...}

2. Attribute Injection

 Public classConfiguration {[Export ("Timeout")]     Public intTimeout {Get{return int. Parse (configurationmanager.appsettings["Timeout"]); }}} [Export] Public classusestimeout {[Import ("Timeout")]     Public intTimeout {Get;Set; } }

3. Method Injection "name distinguishing"

 Public classMessagesender {[Export (typeof(action<string>))]     Public voidSend (stringmessage)    {Console.WriteLine (message); }} [Export] Public classProcessor {[Import (typeof(action<string>))]     Publicaction<string> Messagesender {Get;Set; }  Public voidSend () {Messagesender ("processed"); }  }

4. The contract can be annotated with a string

 Public classMessagesender {[Export ("Messagesender")]     Public voidSend (stringmessage)    {Console.WriteLine (message); }} [Export] Public classProcessor {[Import ("Messagesender")]     Publicaction<string> Messagesender {Get;Set; }  Public voidSend () {Messagesender ("processed"); }  }

5, inheritance injection, that is, in the base class or an excuse to define the contract, its subclasses automatically apply its contract

[InheritedExport]  Public Interface ILogger {  void Log (string  message);}  Public class Logger:ilogger {  publicvoid Log (string  message);

6. Construction Injection

class program  {    [importingconstructor]    public program  (imessagesender messagesender)     {       ...    }  }

7. Option Injection

[Export]  Public class Ordercontroller {  private  ILogger _logger;  [Importingconstructor]    Public Ordercontroller ([Import (allowdefault=true)] ILogger logger) {    ifnull  )      new  Defaultlogger ();     = logger;  }}

8. Set Injection

 Public class Notifier  {    [ImportMany (allowrecomposition=true)]    public ienumerable< imessagesender> Senders {getset;}      Public void Notify (string  message)     {      foreach in senders)      {        sender. Send (message);    }}}

9, in order to ensure that some properties of an object can be instantiated in time, you can use IPartImportsSatisfiedNotification

 Public class program:ipartimportssatisfiednotification {    [ImportMany]    public ienumerable< imessagesender> Senders {getset;}      Public void onimportssatisfied ()     {      // when the is called, the all imports of that could are satisfied have been satisfied.     }   }

10, the MEF injection sharing mode is divided into noshared,shared,any three kinds, simply speaking is noshared means that each caller will create a new instance, and GKFX will provide each caller with a unique static instance (that is, a single state)

[Export ("usermanageviewmodel")][partcreationpolicy (creationpolicy.shared)]  Public class usermanageviewmodel:myviewmodelbase{....}

Through the above introduction to the basic description of the MEF injection pattern, in practical applications the most important also often overlooked is the definition of the contract, if the class is marked [Export (typeof (...))] The use of [import[...] at the time of reference may result in an inability to match, and it also means that all parts must be loaded into the container, otherwise an unmatched exception will occur.

Import and export of WPF prism classes, properties, and methods

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.