Registration Builder Class in .NET 4.5

Introduction

 
In this article, you will learn about the newly introduced Registration Builder Class in .NET 4.5.
 
This class is introduced through the MEF.
 
The Registration Builder Class comes with three different types of the For() Method.
 
1. ForType(): This selects only a single type.
 
Example
  1. ///1: This Will Show You Export type. This Will Export Only One Type.  
  2. BuildRegistration.ForType<NewUser>().Export<NewUser>(); 
2. ForTypeDerivedFrom(): This selects those types that are assignable to a Base Class or an interface.
 
Example
  1. ///2: This Will Show You How To Export all types.            
  2. BuildRegistration.ForTypesDerivedFrom(typeof(WishName)).SelectConstructor(cInfo => cInfo[0]).Export<WishName>(); 
3. ForTypeMatching(): This selects those types that match a Boolean Selector.
 
Example
  1. ///3: This Will Show You How To Export types matching  
  2. BuildRegistration.ForTypesMatching(x => x.Name.Equals("WelcomeToHome")).Export(); 
I had shown all these examples in my Application whose code is as follows.
 
Step 1
 
Add a class for wishing a user a happy birthday.
 
Add this code in that class:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.    
  7. namespace MEF2Example  
  8. {  
  9.     class Birthday : WishName  
  10.     {  
  11.         public string Greet(string User)  
  12.         {  
  13.             return string.Format("Hey Dude It's Your B'day Today:- {0}", User);  
  14.         }  
  15.     }  

Step 2
 
Add a class that will be used to greet the user daily.
 
The code is as follows:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel.Composition;  
  4. using System.Linq;  
  5. using System.Text;  
  6. using System.Threading.Tasks;  
  7.    
  8. namespace MEF2Example  
  9. {  
  10.     public class Morning : WishName  
  11.     {  
  12.         public string wishes { getset; }  
  13.    
  14.         public string Greet(string User)  
  15.         {  
  16.             return string.Format("Wake Up Some 1 Is Waiting For You (Your BOSS, hihihihi) {0}", User);  
  17.         }  
  18.     }  

Step 3
 
Similarly add classes to wish various kind of things and then add a class to pass the name of the user of which all these wishes are to be made:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel.Composition;  
  4. using System.ComponentModel.Composition.Hosting;  
  5. using System.ComponentModel.Composition.Registration;  
  6. using System.Linq;  
  7. using System.Reflection;  
  8. using System.Text;  
  9. using System.Threading.Tasks;  
  10.    
  11. namespace MEF2Example  
  12. {  
  13.     public class StartMFL  
  14.     {  
  15.         static RegistrationBuilder BuildRegistration = default(RegistrationBuilder);  
  16.         static AssemblyCatalog AssamCat = default(AssemblyCatalog);  
  17.         static AggregateCatalog AggCat = default(AggregateCatalog);  
  18.         static CompositionContainer Compocont = default(CompositionContainer);  
  19.    
  20.         public static CompositionContainer Container()  
  21.         {  
  22.             try  
  23.             {  
  24.                 BuildRegistration = new RegistrationBuilder();  
  25.    
  26.    
  27.                 ///1: This Will Show You Export type. This Will Export Only One Type.  
  28.                 BuildRegistration.ForType<NewUser>().Export<NewUser>();  
  29.    
  30.    
  31.                 ///2: This Will Show You How To Export all types.               
  32.                 BuildRegistration.ForTypesDerivedFrom(typeof(WishName)).SelectConstructor(cInfo => cInfo[0]).Export<WishName>();  
  33.    
  34.                 ///3: This Will Show You How To Export types matching  
  35.                 BuildRegistration.ForTypesMatching(x => x.Name.Equals("WelcomeToHome")).Export();  
  36.    
  37.    
  38.                 // 4. This is Complete ExportFactory  
  39.                 BuildRegistration.ForType<Export>().Export<Export>();  
  40.    
  41.                 AssamCat = new AssemblyCatalog(Assembly.GetExecutingAssembly(), BuildRegistration);  
  42.                 AggCat = new AggregateCatalog(AssamCat);  
  43.    
  44.                 Compocont = new CompositionContainer(AggCat, CompositionOptions.DisableSilentRejection);  
  45.                 Compocont.ComposeParts();  
  46.             }  
  47.             catch (System.ComponentModel.Composition.CompositionException ex)  
  48.             {  
  49.                 Console.WriteLine(ex.Message);  
  50.             }  
  51.    
  52.             return Compocont;  
  53.         }  
  54.     }  

You can download the files provided to see the application running.


Similar Articles