.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> WCF >> Post New Resource Bookmark and Share   

 Subscribe to Articles

MEF with WCF Startup

Posted By:Mahadesh Mahalingappa       Posted Date: August 06, 2011    Points: 200    Category: WCF    URL: http://www.dotnetspark.com  

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.
 

In this article I will be creating a Data Access Layer using WCF. I will be using MEF to export the data from the Data Access Layer class and then import it.

MEF 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:

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

Implement the method in DataService.cs:

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

    class Data
    {
    }

  2. Add a property named Articles.

    class Data
    {
       public IEnumerable Articles { get; set; }
    }

  3. Add the Method GetData(). Call the method GetArticles() of DataService .

    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.

    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.

    [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 the Articles Property. I will do that in the Contructor of the Data Class. So here is my final class.

    [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

  7. Create a class App.

    class App
    {
    }

  8. Add a method Run() and put the code to compose the container.

    class App
    {
       public void Run()
        {
           var catalog = new AssemblyCatalog (System.Reflection.Assembly.GetExecutingAssembly());
           var container = new CompositionContainer(catalog);
            container.ComposeParts(this);
        }
    }

  9. Create a property Articles and this time add an import attribute

    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);
        }
    }
    
  10. Create an object for the Data Class and Import it.

    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);
        }
    }

  11. Add a foreach to iterate through the articles.

    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 to add the Export and Import attributes you need to add a reference to System.ComponentModel.Composition. 

    Call the App class now.

    static void Main(string[] args)
    {
       App a = new App();
        a.Run();
       Console.ReadKey();
    }
    It works. Happy Coding.

 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend