locked
WCF Data Contract to DAL RRS feed

  • Question

  • User-193169305 posted

    I have a WCF serivice with Data Contract defined at service level. I do not have bussiness object/entities, as that is duplication of data between WCF and BO and also my proj. also doesnot allows that.

    So, my requirement is UI will send the data to WCF in the form of WCF (Data Contracts), It is being accepted at WCF level. Till here no problem.

    Below is the flow of data for my proj.

    UI --> WCF --> BLL --> DAL and DB Component.

    I am able to get the data from DB to WCF and then UI, but unable to send the data from WCF to DAL as there is no BE involved in between them.

    Note: I am able to send UI to WCF (Data Contract), but cannot send it further to DAL and hence save it into DB.

    Monday, June 24, 2013 10:16 AM

Answers

  • User-488622176 posted

    I'm afraid you're messing up some concepts:

    • WCF is a transport technology to expose "Services" (with methods taking "in" messages and translating them to "out" messages) to remote layers/systems. It is especially usefull if you want to access the services from a remote machine
    • DataContracts are to be set on data objects. These are a way to transport data (consisting of multiple fields) from one application layer to the other. 
    • If you expose services through WCF, the in & out messages must either be primitive types (such as String, bool, int, ...) or data objects that have DataContract and DataMember attributes (allowing serialization of the data, required to transfer between layers)

    So in your case : you need some notion of data objects (business entities, poco's, DTO's, whatever) to group & transport data between your different layers. 

    A typical project structure for this purpose:

    • VS.NET project 1 : <myproject>.Interfaces(.dll) : contains the data objects and service interfaces (service contracts)
    • VS.NET project 2 : <myproject>.Services(.dll) : contains the service implementations (includes project 1), as classes
    • VS.NET project 3 : <myproject>.webapp : exposes the services from project 2 as SVC projects. Can contain the webapp too..
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 25, 2013 9:25 AM
  • User-488622176 posted

    Why wouldn't you pass the Customer object to you DAL? If you put DTO's (such as the Customer object) in a shared assembly (shared between service assembly and DAL assembly) this will work out just fine.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 2, 2013 3:36 AM

All replies

  • User503812343 posted

    one way to do is create a seprate wcf library for only to have all data contracts and refer it where ever you required.

    click here to see how to implement WCF data contracts

    Monday, June 24, 2013 10:53 AM
  • User-488622176 posted

    I'm afraid you're messing up some concepts:

    • WCF is a transport technology to expose "Services" (with methods taking "in" messages and translating them to "out" messages) to remote layers/systems. It is especially usefull if you want to access the services from a remote machine
    • DataContracts are to be set on data objects. These are a way to transport data (consisting of multiple fields) from one application layer to the other. 
    • If you expose services through WCF, the in & out messages must either be primitive types (such as String, bool, int, ...) or data objects that have DataContract and DataMember attributes (allowing serialization of the data, required to transfer between layers)

    So in your case : you need some notion of data objects (business entities, poco's, DTO's, whatever) to group & transport data between your different layers. 

    A typical project structure for this purpose:

    • VS.NET project 1 : <myproject>.Interfaces(.dll) : contains the data objects and service interfaces (service contracts)
    • VS.NET project 2 : <myproject>.Services(.dll) : contains the service implementations (includes project 1), as classes
    • VS.NET project 3 : <myproject>.webapp : exposes the services from project 2 as SVC projects. Can contain the webapp too..
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 25, 2013 9:25 AM
  • User-193169305 posted

    I am new to WCF and exploring different options.

    It will be great, if you can provide some sample code as a solution for this.

    Wednesday, June 26, 2013 11:14 AM
  • User503812343 posted

    Sample code is avilable here

    Wednesday, June 26, 2013 12:40 PM
  • User-193169305 posted

    I already did that.

    From UI to Service, it's all ok. How will I send this to DAL as parametes, as there is not intermediate Entity, I cannot convert this to the entity and I am stuck here.

     

    Thursday, June 27, 2013 10:49 AM
  • User-488622176 posted

    could you post part of the service inteface (method) code? 

    Either you use entities (and I really do not see any reason why you can/should not do this !), either you pass every piece of data as a separate method argument.

    Sample entity:

    public class Customer
    {
      public String FirstName {get;set;}
      public String LastName {get;set;}
    }



    Example using entities:

    public void SaveCustomer(Customer c)
    {}

    Example using arguments:

    public void SaveCustomer(String firstName, String surName)
    {
    }




    Friday, June 28, 2013 7:32 AM
  • User-193169305 posted

    Below is written in svc.cs file. Here I need to access the objCustomer and send to DAL.

    public bool UpdateCustomer(Customer objCustomer)
    {

    }

    Now, I need something like this

    public bool UpdateCustomer(Customer objCustomer)
    {

    customerDAL.UpdateCustomer(<<someobject>>)

    or any other mode to pass to DAL. Please note, I have Entities defined only at Service level and not anywhere else.

    }

    DAL Layer

    public bool UpdateCustomer(<<passed object>>)
    {

    //call SP and later save to DB

    }

    Saturday, June 29, 2013 12:11 PM
  • User-488622176 posted

    Why wouldn't you pass the Customer object to you DAL? If you put DTO's (such as the Customer object) in a shared assembly (shared between service assembly and DAL assembly) this will work out just fine.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 2, 2013 3:36 AM