Click here to Skip to main content
15,883,982 members
Articles / Programming Languages / C#
Article

MEF with WCF – Start Up

Rate me:
Please Sign up or sign in to vote.
1.63/5 (8 votes)
24 Jul 2011CPOL1 min read 22.7K   271   9   4
MEF with WCF

Introduction

In this article, I would be creating a Data Access Layer using WCF. I would be using MEF to Export the Data from Data Access Layer class and then Import it.

MEF offers a nice way to create Composable apps where I can so easily Import and Export Data.

Create an ADO.NET Entity Data Model as shown in the diagram below:

image001.jpg

Create a new WCF Service as shown below:

image002.jpg

Two files would be added to the solution as DataService and IDataService.cs.

Add a method GetArticles() to IDataService.cs.

C#
[ServiceContract]
public interface IDataService
{
    [OperationContract]
    IEnumerable GetArticles();
}

Implement the method in DataService.cs:

C#
public IEnumerable GetArticles()
     {
         PublishingCompanyEntities context = new PublishingCompanyEntities();
         var article = from art in context.Articles
                       select art.Title;

         return article;
     }

Creating a Data Class

  1. Create a class named as Data.
    C#
    class Data
        {
    
        }
  2. Add a property named as Articles.
    C#
    class Data
        {
            public IEnumerable Articles { get; set; }
        }
  3. Add the Method GetData(). Call the method GetArticles() of DataService.
    C#
    class Data
        {
            public IEnumerable Articles { get; set; }
    
            public  IEnumerable GetData()
            {
               DataService ds = new DataService();
               return  Articles =  ds.GetArticles();
            }
        }
  4. Add the Export attribute on the property Articles.
    C#
    class Data
        {
            [Export]
            public IEnumerable Articles { get; set; }
    
            public  IEnumerable GetData()
            {
               DataService ds = new DataService();
               return  ds.GetArticles();
            }
        }
  5. Also add an Export attribute on the Data Class.
    C#
    [Export]
        class Data
        {
            [Export]
            public IEnumerable Articles { get; set; }
    
            public  IEnumerable GetData()
            {
               DataService ds = new DataService();
               return   ds.GetArticles();
            }
        }
  6. I need to set the value of Articles property. I will do that in the constructor of Data class. So here is my final class:
    C#
    [Export]
        class Data
        {
            public Data()
            {
                Articles = GetData();
            }
    
            [Export]
            public IEnumerable Articles { get; set; }
    
            public  IEnumerable GetData()
            {
               DataService ds = new DataService();
               return  Articles =  ds.GetArticles();
            }
        }

Creating Another Class App

  1. Create a class App.
    C#
    class App
        {
        }
  2. Add a method Run() and put the code to compose the container.
    C#
    class App
        {
            public void Run()
            {
                var catalog = new AssemblyCatalog
       (System.Reflection.Assembly.GetExecutingAssembly());
    
                var container = new CompositionContainer(catalog);
                container.ComposeParts(this);
    
            }
        }
  3. Create a property Articles and this time add an import attribute.
    C#
    class App
        {
            [Import]
            public IEnumerable Articles { get; set;}
    
            public void Run()
            {
                var catalog = new AssemblyCatalog
       (System.Reflection.Assembly.GetExecutingAssembly());
    
                var container = new CompositionContainer(catalog);
                container.ComposeParts(this);
            }
        }
  4. Create an object for the Data Class and Import it.
    C#
    class App
        {
            [Import]
            public IEnumerable Articles { get; set;}
    
            [Import]
            Data data;
    
            public void Run()
            {
                var catalog = new AssemblyCatalog
       (System.Reflection.Assembly.GetExecutingAssembly());
    
                var container = new CompositionContainer(catalog);
                container.ComposeParts(this);
            }
        }
  5. Add a foreach through the articles.
    C#
    class App
        {
            [Import]
            public IEnumerable Articles { get; set;}
    
            [Import]
            Data data;
    
            public void Run()
            {
                var catalog = new AssemblyCatalog
       (System.Reflection.Assembly.GetExecutingAssembly());
    
                var container = new CompositionContainer(catalog);
                container.ComposeParts(this);
    
    
                foreach (string art in Articles)
                {
                    Console.WriteLine(art);
                }
            }
        }

Please note that adding Export and Import attributes would need you to include the System.ComponentModel.Composition.

Call the App class now.

C#
static void Main(string[] args)
     {
       App a = new App();
       a.Run();
       Console.ReadKey();
     }

It works! Happy coding.

History

  • 23rd July, 2011: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions

 
GeneralMy vote of 1 Pin
Paul K T29-Dec-11 6:41
Paul K T29-Dec-11 6:41 
GeneralMy vote of 1 Pin
Amiram Korach27-Nov-11 23:12
Amiram Korach27-Nov-11 23:12 
GeneralMy vote of 1 Pin
voloda225-Jul-11 9:59
voloda225-Jul-11 9:59 
GeneralMy vote of 2 Pin
maq_rohit24-Jul-11 18:53
professionalmaq_rohit24-Jul-11 18:53 
To short article with only bunch of code

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.