Mef with meta data and lazy loading

46
Krunal Trivedi Corporate Trainer For DotNet,Silverlight,SPS 2010 MCT S For .NET Web Technology MCTS For SharePoint Server 2010 Development Co-Founder at Aavid Technologies Email:[email protected] Contact : 09998472789 MEF with MetaData And Lazy Loading

Transcript of Mef with meta data and lazy loading

Page 1: 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

Page 2: Mef with meta data and lazy loading

Client Details

Page 3: Mef with meta data and lazy loading
Page 4: Mef with meta data and lazy loading

• 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

Page 5: Mef with meta data and lazy loading

Create a New C# Class library project called MEFZooLibrary and create an Interface called IAnimal

Page 6: Mef with meta data and lazy loading

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

Page 7: Mef with meta data and lazy loading

Add new Class in the same MEFZoo.Animals project…called Rabit…Which also implements our interface IAnimal

Page 8: Mef with meta data and lazy loading

• 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…

Page 9: Mef with meta data and lazy loading

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

Page 10: Mef with meta data and lazy loading

Create a Directory called Extensions in to the Bin\Debug path of the MEFZoo Project..(bin\debug\Extensions..to your console application)

Page 11: Mef with meta data and lazy loading

Put your MEFZoo.Animals dll to the newly created directory..So we get Lion and Rabit ….

Page 12: Mef with meta data and lazy loading

Also create one more Export(Component/Animal) in the Same Zoo.cs class

Page 13: Mef with meta data and lazy loading

As you would expect here we create a collection of an Animal with having ImportMany Attribute.

Page 14: Mef with meta data and lazy loading

Create a helper function called LoadAnimals to the Program.cs of our console application.

Page 15: Mef with meta data and lazy loading

Also called that helper function from Main Method…

Page 16: Mef with meta data and lazy loading

Run the application and see the output…

Page 17: Mef with meta data and lazy loading

• 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.

Page 18: Mef with meta data and lazy loading

• 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.

Page 19: Mef with meta data and lazy loading

Create an helper function called GiveFood in the Zoo.cs class file to your Console Application. Also observe Export Attribute here

Page 20: Mef with meta data and lazy loading

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….

Page 21: Mef with meta data and lazy loading

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.

Page 22: Mef with meta data and lazy loading
Page 23: Mef with meta data and lazy loading

Also Modify Tiger Class as shown in figure

Page 24: Mef with meta data and lazy loading

Also modify GiveFood Method as shown in figure……

Page 25: Mef with meta data and lazy loading

• Run the solution and observe the output

Page 26: Mef with meta data and lazy loading

MEF with Lazy<T> and MetaData

Page 27: Mef with meta data and lazy loading

• 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?

Page 28: Mef with meta data and lazy loading

• 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.

Page 29: Mef with meta data and lazy loading

Create a new Console Application Project Called MyLazyLion and create an Interface as shown in figure….

Page 30: Mef with meta data and lazy loading

Create a new class called Lion which implements our interface IAnimal

Page 31: Mef with meta data and lazy loading

Create a new class called Lion which implements our interface IAnimal

Page 32: Mef with meta data and lazy loading

Create a Class Zoo which will import our animals later at the time of Composition.

Page 33: Mef with meta data and lazy loading
Page 34: Mef with meta data and lazy loading

• 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….

Page 35: Mef with meta data and lazy loading

• Now,Let’s implement MEF with LAZY.

Page 36: Mef with meta data and lazy loading
Page 37: Mef with meta data and lazy loading
Page 38: Mef with meta data and lazy loading

• Run your soulution….observe the answer….

Page 39: Mef with meta data and lazy loading

MEF with MetaData

Page 40: Mef with meta data and lazy loading

• 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.

Page 41: Mef with meta data and lazy loading

Our custom metadata attribute to export animal

Page 42: Mef with meta data and lazy loading
Page 43: Mef with meta data and lazy loading
Page 44: Mef with meta data and lazy loading

• 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.

Page 45: Mef with meta data and lazy loading
Page 46: Mef with meta data and lazy loading

Run the solution…observe the difference…..change the metadata value to true at the class Rabit and run the solution again….