Mef with meta data and lazy loading
-
Upload
krunal-trivedi -
Category
Technology
-
view
734 -
download
2
Transcript of Mef with meta data and lazy loading
Krunal TrivediCorporate Trainer For DotNet,Silverlight,SPS 2010MCT S For .NET Web TechnologyMCTS For SharePoint Server 2010 DevelopmentCo-Founder at Aavid TechnologiesEmail:[email protected] : 09998472789
MEF with MetaData And Lazy Loading
Client Details
• 2 benefits of MEF– It allows you to decouple your components– Supports , various components discovery scenario– MEF Classes resides in the assembly called System.ComponentModel.Composition.dll– The Main Application can Import plug-ins– Plug-ins are marked with an Export attribute– Container is required to do Composition based on matching import and export– Container fire query on Catalogs-Which hold Exports– 3 Types of Catalogs are available– AssemblyCatalog– DerectoryCatalog– AggregateCatalog
Create a New C# Class library project called MEFZooLibrary and create an Interface called IAnimal
Create a new class library project in the same solution name it like MefZoo.Animals……..Add reference of MEFZoo.Library class library to MEFZoo.Anials project….Create a new Class called Lion which implements our interface IAnimal
Add new Class in the same MEFZoo.Animals project…called Rabit…Which also implements our interface IAnimal
• Please make sure that we are using Export attribute on our Class(Animals)..so that they can be Imported to our Zoo…
• We are getting this Import and Export attribute because of System.ComponentModel.Composition Namespace…
Add One More Console Application in the Same Solution called MEFZoo and add one class file to that Console Application called Zoo.cs as well as Add Reference of our MEFZoo.Library class library to get our contract(Interface)…also Add Reference of System.ComponentModel.Composition.dll to this project
Create a Directory called Extensions in to the Bin\Debug path of the MEFZoo Project..(bin\debug\Extensions..to your console application)
Put your MEFZoo.Animals dll to the newly created directory..So we get Lion and Rabit ….
Also create one more Export(Component/Animal) in the Same Zoo.cs class
As you would expect here we create a collection of an Animal with having ImportMany Attribute.
Create a helper function called LoadAnimals to the Program.cs of our console application.
Also called that helper function from Main Method…
Run the application and see the output…
• Now , you have zoo…you have animals…• Its time to feed your animals…For this , we’ll need our animals
to make some grumble sound---A Call Back to call the attention of the Zoo Keeper.
• So , Now we will see how to inject Call Back Mechanism with MEF.
• First of all , Let us define a GiveFood method in our Zoo Class.• Only one thing you might notice here is , we are exporting
GiveFood method as a part.• However,we are using “AnimalFood” as the Contract Name ,
instead of a type.• This is allowed and valid in MEF-You can either use a string or
a Type or both togather as a contract.
Create an helper function called GiveFood in the Zoo.cs class file to your Console Application. Also observe Export Attribute here
Extend our IAnimal interface..Add a deleagate property synonymous of GiveFood method so that MEF can hook up the GiveFood method later, when importing the exported GiveFood. After that Build your project again….
Modify your Lion.cs as well as Rabit.cs as shown here…Nothing fancy there, we just have a timer there to make the Lion and Rabit hungry.
Also Modify Tiger Class as shown in figure
Also modify GiveFood Method as shown in figure……
• Run the solution and observe the output
MEF with Lazy<T> and MetaData
• When we use [Import] or [ImportMany] attributes we are telling MEF to fill our variable with an instance of our desired type.
• The approach is great if we are dealing with smaller types or we are certain that we need this instance ,but what if the type we want to import is quite heavy and it would be a waste to load it right on the start?
• What if we want to import the types based on some user settings or a plug-in systems?
• Lazy class allows you to support for lazy initialization—you can use Lazy<T> to defer the creation of a large or resource-intensive object.
Create a new Console Application Project Called MyLazyLion and create an Interface as shown in figure….
Create a new class called Lion which implements our interface IAnimal
Create a new class called Lion which implements our interface IAnimal
Create a Class Zoo which will import our animals later at the time of Composition.
• you’ll see that MEF has created an instance of Lion and Rabbit at the time of composition – i.e, when we callcontainer.ComposeParts. See the messages we are writing from the constructor.
Run the solution….
• Now,Let’s implement MEF with LAZY.
• Run your soulution….observe the answer….
MEF with MetaData
• That looks a bit clumsy, because of those strings. Right? A better way is to create your own custom Export attribute that includes the metadata information as well.
• That is pretty simple. The code below is equivalent to what we have just done above.
Our custom metadata attribute to export animal
• Now let us come to the real question. How to import the metadata information in a Lazy way? Fortunately, MEF has an overload of Lazy, Lazy that supports importing Metadata information.
• So, all we need to do is create a metadata import interface that matches our export definition, and use it. Like this.
Run the solution…observe the difference…..change the metadata value to true at the class Rabit and run the solution again….