[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 552
  • Last Modified:

Create a WCF object on client side

Hi,
Could someone please let me know what is needed in order to be able to create objects which were defined on the server side of WCF, in the client code.

So, basically, if I have an object "Reservation", in WCF (let's say it is hosted on a website's App_Code folder), I would want the calling code (let's say an ASPX page which is NOT in the same website) to be able to do:

Dim reservation as new RestaurantService.Reservation() ' This object is defined on the server, but I would like it to be available to me for declaration on the client side, just as methods of the service contract are.

reservation.FirstName = "John"
reservation.LastName = "Smith"
....
RestaurantService.CreateNewReservation(reservation)

This would be so much nicer than having to do:

RestaurantService.CreateNewReservation("john", "smith",... )

0
luminas
Asked:
luminas
  • 3
  • 3
1 Solution
 
lenordisteCommented:
actually, the method RestaurantService.CreateNewReservation("john", "smith",... ) follows SOA guidelines and is a much better design choice in my ownest opinion.
0
 
luminasAuthor Commented:
I don't like it. It means duplication of method signatures since inside RestaurantService.CreateNewReservation() there will need to be another reference to a business object's respective method, for example BusinessObjects.Reservation.Create("john", "smith"...), instead of taking that Reservation object in the server side and writing Reservation.Create().

It's much more maintainable. Not to mention, you have the ability on the client side to "play" with the Reservation object (receive it from server, manipulate, and send back to server).
0
 
lenordisteCommented:
WCF is designed for SOA so I am not even sure that you can do what you are asking for. And again, I really recommend you to use a service based approach and not an OO one. SOA is much easier to maintain actually and allows for a separation of logic between server and client. You'll probably need to rethink your project a bit but I think it is worth it. I am sure a lot of experts here will tell you the same thing, SOA is the best solution for vertical and horizontal scalability.

Also, what you can do is the following for instance:
RestaurantService.AddReservation(ReservationInfo reservation)
where reservation info is a container object with the information required to make a reservation.

To mark an object to be exposed use "DataContract" and "DataMember" in your attributes:
 [DataContract]
    public class ReservationInfo
    {
        private int reservationId;
        private string firstName;

        [DataMember]
        public int FirstName
        {
get;set;
        }
    }
0
Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

 
lenordisteCommented:
by the way, if you don't put "DataMember" on a property than it won't be exposed. In my above example it helps you to have private property (reservationId) that your client side needs to use without exposing them to the client.
0
 
luminasAuthor Commented:
Actually that's what I was doing - putting "DataContract" attribute on the class - but forgot to update the web service reference in VS... After doing that, I got my object on the client side, and could do exactly what I wanted.

Even if this somehow conflicts SOA guidelines (it's still hard for me to understand why, this is not about a specific architecture - it's general programming concepts) I still prefer this. It means if many clients are consuming the service, any change in the reservation structure would not break the service for them. And the method signature stays the same.
0
 
luminasAuthor Commented:
I prefer exposing the object and working with it on client side than just exposing methods with many parameters.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now