MEF With WCF - Start UP

In this article, I will be creating a Data Access Layer using WCF. I will be using MEF(Managed Extensibility Framework) to export the data from the Data Access Layer class and then import it.
 
MEF(Managed Extensibility Framework) offers a nice way to create Composable applications where I can easily import and export data.
 
Created a ADO.Net Entity Data Model as shown in diagram below:
 
WCF1.gif 
 
Create a new WCF Service as shown below:
 
WCF2.gif 
 
Two files would be added to the solution as DataService and IDataService.cs.
 
Add a method GetArticles() to IDataService.cs:
  1. [ServiceContract]  
  2. public interface IDataService  
  3. {  
  4.     [OperationContract]  
  5.     IEnumerable GetArticles();  
  6. }  
Implement the method in DataService.cs:
  1. public IEnumerable GetArticles()  
  2. {  
  3.     PublishingCompanyEntities context = new PublishingCompanyEntities();  
  4.     var article = from art in context.Articles  
  5.                        select art.Title;  
  6.     return article;  
  7. }  
Creating a Data Class:
  1. Create a class named as Data
    1. class Data  
    2. {  
    3. }
  2. Add a property named Articles.
    1. class Data  
    2. {  
    3.     public IEnumerable Articles { getset; }  
    4. }
  3. Add the Method GetData(). Call the method GetArticles() of DataService .
    1. class Data  
    2. {  
    3.     public IEnumerable Articles { getset; }  
    4.     public  IEnumerable GetData()  
    5.     {  
    6.         DataService ds = new DataService();  
    7.         return  Articles =  ds.GetArticles();  
    8.     }  
    9. }
  4. Add the Export attribute on the property Articles.
    1. class Data  
    2. {  
    3.     [Export]  
    4.     public IEnumerable Articles { getset; }  
    5.     public  IEnumerable GetData()  
    6.     {  
    7.         DataService ds = new DataService();  
    8.         return  ds.GetArticles();  
    9.     }  
    10. }
  5. Also add an Export attribute on the Data Class.
    1. [Export]  
    2. class Data  
    3. {     
    4.     [Export]  
    5.     public IEnumerable Articles { getset; }  
    6.     public  IEnumerable GetData()  
    7.     {  
    8.         DataService ds = new DataService();  
    9.         return   ds.GetArticles();  
    10.     }  
    11. }
  6. I need to set the value of the Articles Property. I will do that in the Contructor of the Data Class. So here is my final class.
    1. [Export]  
    2. class Data  
    3. {  
    4.     public Data()  
    5.     {  
    6.         Articles = GetData();  
    7.     }  
    8.     [Export]  
    9.     public IEnumerable Articles { getset; }  
    10.     public  IEnumerable GetData()  
    11.     {  
    12.         DataService ds = new DataService();  
    13.         return  Articles =  ds.GetArticles();  
    14.     }  
    15. }
Creating another class App
  1. Create a class App.
    1. class App  
    2. {  
    3. }
  2. Add a method Run() and put the code to compose the container.
    1. class App  
    2. {  
    3.     public void Run()  
    4.     {  
    5.         var catalog = new AssemblyCatalog (System.Reflection.Assembly.GetExecutingAssembly());  
    6.         var container = new CompositionContainer(catalog);  
    7.         container.ComposeParts(this);  
    8.     }  
    9. }
  3. Create a property Articles and this time add an import attribute
    1. class App  
    2. {  
    3.     [Import]  
    4.     public IEnumerable Articles { getset;}  
    5.     public void Run()  
    6.     {  
    7.         var catalog = new AssemblyCatalog(System.Reflection.Assembly.GetExecutingAssembly());  
    8.         var container = new CompositionContainer(catalog);  
    9.         container.ComposeParts(this);  
    10.     }  
    11. }
  4. Create an object for the Data Class and Import it.
    1. class App  
    2. {  
    3.     [Import]  
    4.     public IEnumerable Articles { getset;}  
    5.     [Import]  
    6.     Data data;  
    7.     public void Run()  
    8.     {  
    9.         var catalog = new AssemblyCatalog(System.Reflection.Assembly.GetExecutingAssembly());  
    10.         var container = new CompositionContainer(catalog);  
    11.         container.ComposeParts(this);  
    12.     }  
    13. }
  5. Add a foreach to iterate through the articles.
    1. class App  
    2. {  
    3.     [Import]  
    4.     public IEnumerable Articles { getset;}  
    5.     [Import]  
    6.     Data data;  
    7.     public void Run()  
    8.     {  
    9.         var catalog = new AssemblyCatalog(System.Reflection.Assembly.GetExecutingAssembly());  
    10.         var container = new CompositionContainer(catalog);  
    11.         container.ComposeParts(this);  
    12.         foreach (string art in Articles)  
    13.         {  
    14.             Console.WriteLine(art);  
    15.         }  
    16.     }  
    17. }
Please note that to add the Export and Import attributes you need to add a reference to System.ComponentModel.Composition.
 
Call the App class now.
  1. static void Main(string[] args)  
  2. {  
  3.     App a = new App();  
  4.     a.Run();  
  5.     Console.ReadKey();  
  6. }  
It works. Happy Coding.


Similar Articles