locked
Sharing classes between server and client. RRS feed

  • Question

  • I am trying to shared data between the client and the server. The only solutions i have found so far are to put the shared objects in a library that is referenced both by the WCF service and by the client consuming the WCF services. This is Option 1 but the problem is i would be mixing client and server behavouir. IE the save method in the class may make database calls. If this was to be called on the client it would fail because the database connection would not be established.

    So i thought of option 2 which is to have a Client and Server version of the object inherit the object it self EG ClientUser inherits User and ServerUser unherits User. Then
    User it self would inherit UserDTO. Look at the examples at the bottom.

    I think this is good method to share client and server objects but im sure there is a better one.
    How have others solved this problem?
    Can anyone see any ways to improve on my idea?

    EG Option 1

    User
    --------------
    Name
    Password
    ---------------
    ResetPassword()
    Validate()
    SaveToDatabase()

    Option 2
    UserDTO       ------------ only contains the data (state) of the user object
    ---------------
    Name
    Password

    User:UserDTO  ------------ contains behaviours that could be used on the server and the client
    ----------------
    Validate()

    ClientUser:User    -------------------------- contains behaviours that would be used on the client
    ---------------------                                    could even add a save method to the client server which called the web services save user method
    ResetPassword                                      this way the logic would be moved away from the ui. This could be over kill though.

    ServerUser:User  -------------------------- contains behaviours that would be used on the Server
    ----------------------
    SaveToDatabase()

    Monday, December 14, 2009 8:42 AM

Answers

  • Hi ,

    In WCF service, you can use serialization between Service and client to transfer the data. Some primitive built-in .NET types can be serialized/deserialized between the service and client. Some custom data type can be serialized/deserialized by using attribute in the design. We don't need to share the type between the service and client. Just expose the service operation in service contract. Please have a look the following link about Data Transfer and Serialization in WCF.
    http://msdn.microsoft.com/en-us/library/ms730035.aspx
    In a connected system, services and clients depend on the exchange of data to accomplish any task. As a developer of a service or client, you must also understand how Windows Communication Foundation (WCF) handles data and data serialization in order to create applications that are efficient and easy to maintain.

    Best regards,
    Riquel
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Riquel_Dong Monday, December 21, 2009 1:29 AM
    Thursday, December 17, 2009 8:36 AM