none
Extending WCF Entity object on the client side RRS feed

  • Question

  • Hi,

    I'm after a little advice on best practice with some code I am writing. I have a simple WCF service which exposes items generated from my entity model on the server. I am calling the service methods from a windows phone app that I am writing. When I get a collection of the classes returned from a service call I bind it to a list type control on the phone app. This all works great and is pretty straightforward.

    I now want to have an image property on the class which I'll bind to the source of an image control in the item template on the list. I can think of three ways of doing this that spring to mind;

    1) Create a class in the client project that I create an observable collection of that has the same properties as the entity class plus the image property.

    2) Create a class in the client project that I create an observable collection of that has the entity class as one property and the image property as a second.

    3) Create a partial class with the same namespace and name as the entity class and add the image property to this.

    All three options will work although I am favouring option 3. With option 1 I would have to add new properties to my new class if and when my table/entity changes which is extra work. With option 2 I would have to refer to the class properties in the following way MyClientClass.ImageProperty and MyClientClass.MyEntityClass.StringProperty rather than just MyEntityClass.ImageProperty and MyEntityClass.StringProperty which weirds me out.

    The only thing that has been flagged as weird in peer review about option 3 is that I have to use the service namespace in the partial class that I am using on the client side to extend the entity class. I've looked around the web and it looks like it's ok to extend the class on the client side but nobody really suggests that having a class defined with the service namespace in the client project is acceptable or a hack ... or suggests another way of doing it.

    Does anybody have any opinion on this, and if possible, if there is any official material (MSDN, tutorial, etc) that shows what the best practice is for this, I'd be very interested in reading it. I've quite a tight deadline on the project I'm working on at the moment so will probably implement one of these sooner rather than later, but would be interested nonetheless in how it 'should be done'.

    Thanks in advance,

    Alan

    Wednesday, February 19, 2014 8:59 PM

Answers

  • Hi,

    Generated classes actually have the modifier 'partial', so it is possible to extend them. For more information, please try to check the following:

    For example, this DataContract:

    [DataContract]
    public class Test
    {
        [DataMember]
        public int Id { get; set; }
        [DataMember]
        public string Name { get; set; }
    }

    Can be extended on the client's side in this way:

    namespace WPAPPTest.ServiceReference1
    {
        public partial class Test
        {
            public string ExtendedName
            {
                get { return this.Id+" "+this.Name; }
            }
        }
    }

    And please try to take notice of the name of namespace, it must be the same as the namespace of a service reference.

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Thursday, February 20, 2014 2:21 AM
    Moderator
  • Hi,

    >>it was reasonable to have a a class in the client project declared with the namespace from the service project or is that weird in some way

    There a number of reasons to extend the client system, including:

    Monday, March 3, 2014 4:57 AM

All replies

  • Hi,

    Generated classes actually have the modifier 'partial', so it is possible to extend them. For more information, please try to check the following:

    For example, this DataContract:

    [DataContract]
    public class Test
    {
        [DataMember]
        public int Id { get; set; }
        [DataMember]
        public string Name { get; set; }
    }

    Can be extended on the client's side in this way:

    namespace WPAPPTest.ServiceReference1
    {
        public partial class Test
        {
            public string ExtendedName
            {
                get { return this.Id+" "+this.Name; }
            }
        }
    }

    And please try to take notice of the name of namespace, it must be the same as the namespace of a service reference.

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Thursday, February 20, 2014 2:21 AM
    Moderator
  • Thanks for the reply Amy. I have got the extended class in place and it is working fine. The thing I wanted to check with it was that it was reasonable to have a a class in the client project declared with the namespace from the service project or is that weird in some way?

    Best regards,

    Alan

    Thursday, February 20, 2014 9:15 AM
  • Hi,

    >>it was reasonable to have a a class in the client project declared with the namespace from the service project or is that weird in some way

    There a number of reasons to extend the client system, including:

    Monday, March 3, 2014 4:57 AM