KEMBAR78
Mef with meta data and lazy loading | PPT
MEF with MetaData And Lazy Loading


 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:krunaltrivedi@live.in
 Contact : 09998472789
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 BinDebug path of the MEFZoo
Project..(bindebugExtensions..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.
Run the solution….




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

Mef with meta data and lazy loading

  • 1.
    MEF with MetaDataAnd Lazy Loading 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:krunaltrivedi@live.in Contact : 09998472789
  • 2.
  • 4.
    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
  • 5.
    Create a NewC# Class library project called MEFZooLibrary and create an Interface called IAnimal
  • 6.
    Create a newclass 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
  • 7.
    Add new Classin the same MEFZoo.Animals project…called Rabit…Which also implements our interface IAnimal
  • 8.
    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…
  • 9.
    Add One MoreConsole 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
  • 10.
    Create a Directorycalled Extensions in to the BinDebug path of the MEFZoo Project..(bindebugExtensions..to your console application)
  • 11.
    Put your MEFZoo.Animalsdll to the newly created directory..So we get Lion and Rabit ….
  • 12.
    Also create onemore Export(Component/Animal) in the Same Zoo.cs class
  • 13.
    As you wouldexpect here we create a collection of an Animal with having ImportMany Attribute.
  • 14.
    Create a helperfunction called LoadAnimals to the Program.cs of our console application.
  • 15.
    Also called thathelper function from Main Method…
  • 16.
    Run the applicationand see the output…
  • 17.
    • 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.
  • 18.
    • First ofall , 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.
  • 19.
    Create an helperfunction called GiveFood in the Zoo.cs class file to your Console Application. Also observe Export Attribute here
  • 20.
    Extend our IAnimalinterface..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….
  • 21.
    Modify your Lion.csas well as Rabit.cs as shown here…Nothing fancy there, we just have a timer there to make the Lion and Rabit hungry.
  • 23.
    Also Modify TigerClass as shown in figure
  • 24.
    Also modify GiveFoodMethod as shown in figure……
  • 25.
    • Run thesolution and observe the output
  • 26.
    MEF with Lazy<T>and MetaData
  • 27.
    • When weuse [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?
  • 28.
    • Lazy classallows you to support for lazy initialization—you can use Lazy<T> to defer the creation of a large or resource-intensive object.
  • 29.
    Create a newConsole Application Project Called MyLazyLion and create an Interface as shown in figure….
  • 30.
    Create a newclass called Lion which implements our interface IAnimal
  • 31.
    Create a newclass called Lion which implements our interface IAnimal
  • 32.
    Create a ClassZoo which will import our animals later at the time of Composition.
  • 34.
    Run the solution…. •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.
  • 35.
  • 38.
    • Run yoursoulution….observe the answer….
  • 39.
  • 40.
    • That looksa 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.
  • 41.
    Our custom metadataattribute to export animal
  • 44.
    • Now letus 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.
  • 46.
    Run the solution…observethe difference…..change the metadata value to true at the class Rabit and run the solution again….