Hlavní menu
Nástroje |
UvodDoKomponent.MEFExample HistoryHide minor edits - Show changes to markup 28 January 2015, 15:05
by
- Added lines 14-15:
![]() 28 January 2015, 13:47
by
- Changed lines 1-2 from:
Original site of this example more. to:
Original site of this example. 28 January 2015, 13:47
by
- Changed line 362 from:
So in this first part of the MEF series, we learnt about the basics of MEF, its applicability, importance of Export, Import, ImportMany? attributes, Assembly and Directory catalogs, dynamic component discovery, etc. But there is a lot more stuff to learn about MEF. to:
So in this first part of the MEF series, we learnt about the basics of MEF, its applicability, importance of Export, Import, ImportMany attributes, Assembly and Directory catalogs, dynamic component discovery, etc. But there is a lot more stuff to learn about MEF ;) 28 January 2015, 13:36
by
- Changed lines 155-156 from:
Well by this time, we have learnt something about MEF, how it works and also we have successfully added one component in our application. Now let us enhance our application so that it can handle all the other components like Subtraction, Multiplication and Division. to:
Well by this time, we have learnt something about MEF, how it works and also we have successfully added one component in our application. Now let us enhance our application so that it can handle all the other components like Subtraction, Multiplication and Division. 28 January 2015, 13:32
by
- Added lines 43-46:
First of all, we need to add reference to System.ComponentModel.Composition assembly into the project. Also a reference of the CalculatorContract.dll is added. Deleted lines 67-70:
First of all, we need to add reference to System.ComponentModel.Composition assembly into the project. Also a reference of the CalculatorContract.dll is added. 28 January 2015, 13:29
by
- Changed lines 15-16 from:
Let us create a simple calculator for the demonstration. Our sample application will have four basic arithmetic operations, viz. Add, Subtract, Multiply and Division. With that in mind, let us create our project structure as under: to:
Let us create a simple calculator for the demonstration. Our sample application will have four basic arithmetic operations, viz. Add, Subtract, Multiply and Division. With that in mind, let us create our project structure as under: 28 January 2015, 13:28
by
- Changed lines 4-5 from:
Let's repeat terminologie again :) to:
Let's repeat terminology again :) 28 January 2015, 13:27
by
- Added lines 1-2:
Original site of this example more. Added lines 17-18:
![]() Added lines 152-153:
![]() Added lines 159-160:
![]() Changed lines 259-260 from:
5.jpg to:
![]() Changed lines 263-264 from:
Correspondingly, the GetResult? function has been changed a little bit. It now accepts a third parameter which specifies the operation type and based on that, the Exported parts will be invoked. to:
Correspondingly, the GetResult function has been changed a little bit. It now accepts a third parameter which specifies the operation type and based on that, the Exported parts will be invoked. Changed lines 276-277 from:
6.jpg to:
![]() Changed lines 287-288 from:
7.jpg to:
![]() Changed lines 291-292 from:
We have added only two DLLs? there, viz., Add and Subtraction and removed the Add.cs and Subtract.cs files from CompositionHelper project. to:
We have added only two DLLs there, viz., Add and Subtraction and removed the Add.cs and Subtract.cs files from CompositionHelper project. Changed lines 340-345 from:
to:
![]() And the AssemblyCatalog has two parts (Divide and Multiply): ![]() Changed lines 350-351 from:
10.jpg to:
![]() Changed lines 355-360 from:
MEF breaks the tightly coupled dependencies across the application but respects the type checking of the loosely coupled parts. Applications can be extended. Components can be added at runtime. Dynamic discovery of the components. Great piece of reusability. to:
Changed lines 362-367 from:
So in this first part of the MEF series, we learnt about the basics of MEF, its applicability, importance of Export, Import, ImportMany? attributes, Assembly and Directory catalogs, dynamic component discovery, etc. But there is a lot more stuff to learn about MEF. In the next part or subsequent parts, we will look into some attributes, catalogs, more practical examples with different kind of applications using MEF. Thanks for reading. Longer version of this example more. to:
So in this first part of the MEF series, we learnt about the basics of MEF, its applicability, importance of Export, Import, ImportMany? attributes, Assembly and Directory catalogs, dynamic component discovery, etc. But there is a lot more stuff to learn about MEF. 28 January 2015, 13:09
by
- Changed lines 1-61 from:
Evil Wizard ExampleThis is a very simple example, nevertheless eligible to understand the basis of MEF. ![]() Step 1. Add MEF to our Visual Studio solutionAdding MEF to your VS solution could not be easier. MEF is part of the .NET 4 Framework, so you can just Add a Reference as you would do with any .NET assembly: Into class EvilWizard add a reference to System.ComponentModel.Composition Step 2. Define the EvilWizard as an Import PartThe next step is to tell MEF that our EvilWizard class is a part. Furthermore, it is an import that is going to consume a series of services, that is, is going to use a 'Hat service', a 'Robe service' and a collection of 'ISpell services'. In order to do this, we will use MEF attribute based programming model, and we will decorate the EvilWizard components with the [Import] and [ImportMany] attributes as shown below: ![]() If you have not guessed it yet, we use the [Import] attribute to represent a single service and [ImportMany] to represent a multiplicity of services. These attributes also represent default contracts, but we will deal with contracts in upcoming articles so do not worry for now. Step 3. Define the Hat, Robe and Spells as Export PartsWe continue by defining the Hat, Robe and Spells as parts that provide services: ![]() Notice how we used the [Export] attribute to decorate a class that provides a service and the [InheritedExport] attribute to decorate an interface and ensure that anything that implements that interface becomes a part. Step 4. Discover The Parts and Setup The ContainerNow that we have all the parts of our system ready, the next step consists in instantiating a catalog that will discover all parts within our assembly: ![]() And passing this catalog to our MEF container so it will know which parts are available in our system: ![]() Step 5. Let the magic happenFinally, composing our evil wizard with other parts (hat, robe and spells) is as easy as: ![]() And there we have our evil wizard kicking ass: ![]() Step 6. Extending our RPGExtending our RPG at this moment (for instance, adding new spells) would be as easy as adding new parts in a new assembly. We can use a [DirectoryCatalog ] instead of a [AssemblyCatalog ] to point to a path where we can drop all these new assemblies packed with new spells of destruction and mayhem. Author of this example (original site) Jaime González García. As a final note, you can find the code sample at GitHub. to:
MEF Related TerminologiesLet's repeat terminologie again :)
A Simple Working Example with Only a Single Export Part in Current AssemblyLet us create a simple calculator for the demonstration. Our sample application will have four basic arithmetic operations, viz. Add, Subtract, Multiply and Division. With that in mind, let us create our project structure as under: The solution file CalculatorDemoUsingMEF has three projects:
a) Description of
|