Search code examples
c#winformsentity-frameworkentity-framework-5wcf-data-services

Entity Framework 5 with n-Tier Architecture


I have an application that uses Entity Framework 5 and POCO classes using a code-first approach.

This way I have every database table modeled in a Poco class so that way I can have a better control over the model (instead of EDMX stuff).

The application is working fine, but now I want to add a WCF layer, so it works under a n-tier architecture.

What is the best approach to do this?

I'm very familiar with WCF but with ADO.NET returning Datasets. I was reading that for best performance you can return JSON instead of XML so would like to know from experts if there is any example I can look at on how to implement a WCF Layer using Entity Framework that returns JSON objects to the client and viceversa.

Thanks and appreciate any guide.


Solution

  • There is no such change, if you create your custom class or use entity framework classes. Only you have to put [DataContract] above the class which needs to be serialized and [DataMember] which needs to be serialized in the service.

    For eg.

    [DataContract]
    public class Test
    {
       [DataMember]
       public string field1 {get; set;}
    
       [DataMember]
       public string field2 {get; set;}
    }
    

    And to create WCF service you just needs to put the below code:

    it will be in service interface and you just needs to implement in the class.

        [OperationContract]
        [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
        List<test> GetTest();
    

    I hope it will help you. :)