none
Add WCF Service to existing project RRS feed

  • Question

  • I have a project which i believe is using the Repository Pattern.

    When i say Respository Pattern, by that i mean:

    Project 1 - Contains the Data Access Layer in this case a Linq to SQL file. Project 2 - Contains the Business Logic, in this case classes to get the data etc (basically all the CRUD operations).

    Project 2 is broken down into two sections. Section 1 is the contract (all Interfaces) and section 2 are the classes implementing the Interfaces.

    I now would like to create a WCF service so other applications could use the service to get the data.

    I did WCF a while ago and i remember that it also had a similar structure..... So now im a little confused that if i create a new WCF Service Application i would need to repeat all the logic from project 2 but most likely i could be missing the correct way to do this.

    How could i avoid copy and pasting the code from project 2 and use project 2 as the Service for the WCF service?

    Thanks
    Friday, July 26, 2013 10:32 AM

Answers

  • You might be able to use the classes and interfaces in project 2 to implement the WCF service, that depends on what kind of an application design is now at project 2.

    For example if your business layer classes look something like

    public interface IPeopleService
    {
        List<Person> GetPeopleByFirstName(string firstName);
    }
    
    public class Person
    {
        public int Id { get; set; }
    
        public string FirstName { get; set; }
    
        public string LastName { get; set; }
    }
    
    public class PeopleService : IPeopleService
    {
        public List<Person> GetPeopleByFirstName(string firstName)
        {
            throw new NotImplementedException();
        }
    }

    Then you can reference System.ServiceModel assembly and modify the them to be used as service.

    using System.ServiceModel;
    using System.Runtime.Serialization;
    
    [ServiceOperation]
    public interface IPeopleService
    {
        [OperationContract]
        List<Person> GetPeopleByFirstName(string firstName);
    }
    
    [DataContract]
    public class Person
    {
        [DataMember]
        public int Id { get; set; }
    
        [DataMember]
        public string FirstName { get; set; }
    
        [DataMember]
        public string LastName { get; set; }
    }
    
    public class PeopleService : IPeopleService
    {
        public List<Person> GetPeopleByFirstName(string firstName)
        {
            throw new NotImplementedException();
        }
    }
    Of course this might need more designing than just decorating interface and data contract classes with appropriate attributes like security etc., but as said you might be able to use your business classes as services. And of course you need to make some other application like Windows service or IIS WCF host application to actually host your services.

    Friday, July 26, 2013 1:02 PM
  • I added them manually because that is just an sample.

    You could do so that you add them with code generation, but you might also want to consider creating another data transfer object for the WCF. This because sometimes your entities directly matching the database tables do have a lot data that might not be needed to go from service to the clients so you do another object to transfer only the data client and service need to communicate so that operation works. But that might also make it so that you can not use your interfaces as services as they would return Linq to Sql entities and your service needs to operate with data transfer objects.


    Monday, July 29, 2013 5:08 AM

All replies

  • You might be able to use the classes and interfaces in project 2 to implement the WCF service, that depends on what kind of an application design is now at project 2.

    For example if your business layer classes look something like

    public interface IPeopleService
    {
        List<Person> GetPeopleByFirstName(string firstName);
    }
    
    public class Person
    {
        public int Id { get; set; }
    
        public string FirstName { get; set; }
    
        public string LastName { get; set; }
    }
    
    public class PeopleService : IPeopleService
    {
        public List<Person> GetPeopleByFirstName(string firstName)
        {
            throw new NotImplementedException();
        }
    }

    Then you can reference System.ServiceModel assembly and modify the them to be used as service.

    using System.ServiceModel;
    using System.Runtime.Serialization;
    
    [ServiceOperation]
    public interface IPeopleService
    {
        [OperationContract]
        List<Person> GetPeopleByFirstName(string firstName);
    }
    
    [DataContract]
    public class Person
    {
        [DataMember]
        public int Id { get; set; }
    
        [DataMember]
        public string FirstName { get; set; }
    
        [DataMember]
        public string LastName { get; set; }
    }
    
    public class PeopleService : IPeopleService
    {
        public List<Person> GetPeopleByFirstName(string firstName)
        {
            throw new NotImplementedException();
        }
    }
    Of course this might need more designing than just decorating interface and data contract classes with appropriate attributes like security etc., but as said you might be able to use your business classes as services. And of course you need to make some other application like Windows service or IIS WCF host application to actually host your services.

    Friday, July 26, 2013 1:02 PM
  • Thanks for your help. What confuses me is how in your example have you added the attributes to the Person class? Did you add them manually?

    The reason why i ask is when i create my DAL and add the Linq to Sql class it automatically creates the Person object, so i dont have to create it manually. Attempting to add the DataMember attribute to all fields could be a pain unless theres another way around?

    Saturday, July 27, 2013 7:54 AM
  • I added them manually because that is just an sample.

    You could do so that you add them with code generation, but you might also want to consider creating another data transfer object for the WCF. This because sometimes your entities directly matching the database tables do have a lot data that might not be needed to go from service to the clients so you do another object to transfer only the data client and service need to communicate so that operation works. But that might also make it so that you can not use your interfaces as services as they would return Linq to Sql entities and your service needs to operate with data transfer objects.


    Monday, July 29, 2013 5:08 AM