2

I have a WCF web service that is consumed in an MVC4 application that returns an object. I want to add another property to the object after it is loaded. Essentially I want a clone of the object plus my new property.

How would I do that?

Would it be best to deserialize to JSON, then add the new property and then serialize it into my new object with the extra property or is there another way to do it?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
MB34
  • 4,210
  • 12
  • 59
  • 110
  • Plenty of info on the subject here: http://stackoverflow.com/questions/6329489/dynamically-add-properties-to-a-existing-object – HaukurHaf Sep 25 '15 at 18:15
  • WPF is an UI framework, WCF is a web services framework, don't get confused. – abatishchev Sep 25 '15 at 18:16
  • Create your own Type including all properties of given object and your desired new property, then fill your new class and do what do you want with it. – Reza Aghaei Sep 25 '15 at 18:17

3 Answers3

4

If you want to keep things simple, you can simply create your new Type including all properties of given object and your desired new property, then fill your new class and do what do you want with it.

Also consider reading Note part.

For complicated cases and large applications, you can consider solutions like what abatishchev mentioned in his answer.

class Foo
{
    public int Id { get; set; }
    public string Name { get; set; }
}


class FooViewModel
{
    public FooViewModel()
    {
    }

    public FooViewModel(Foo foo)
    {
        this.Id= foo.Id;
        this.Name= foo.Name;
    }

    public int Id { get; set; }
    public string Name { get; set; }

    public string NewProperty{ get; set; }
}

And use it this way:

var foo = Service.GetFoo();
var fooViewModel= new FooViewModel(foo);
fooViewModel.NewProperty = "new value";

Note:

  • You can use a Foo instance in FooViewModel and all getters and setters act on that instance to keep thing synchronized.
  • Sometimes you can enhance the solution using inheritance from Foo, this way you don't need too create each properties again.
  • For complicated cases and large applications, you can consider solutions like what abatishchev mentioned in his answer.
Community
  • 1
  • 1
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • I agree it keeps things simple but further you go, more properties you add and remove - more hassle it brings. You can forget to specify a property and get a weird error. So you create a separate class to map and unit tests it. What is almost the same as AutoMapper but everything is manual without the need to be it. You answer is what i was doing previously and my answer is what I'm doing now days. – abatishchev Sep 25 '15 at 18:26
  • @abatishchev Thank you for your comment. Even though we can use inheritance to decrease such pain, But in general you are right. This option is available for simple cases. – Reza Aghaei Sep 25 '15 at 18:33
  • 1
    Yeah, inheritance looks a good solution. But without multiple inheritance in C# you need to choose once and choose wisely :) If you have multiple layers (data access, data transfer, business logic, presentation) you can't go with inheritance. Here [Composition over inheritance](https://en.wikipedia.org/wiki/Composition_over_inheritance) will help so you end up with copying multiзду properties multiple times. And later - look how to automate this. – abatishchev Sep 25 '15 at 18:36
3

I think what you're looking for is AutoMapper. You can map one object into another. Say, you have a DTO entity with X properties. Then you have a business logic entity with the same X properties + Y additional. AutoMapper will handle it easily.

class OrderDto
{
    public int OrderId { get; set; }
}

class OrderViewModel
{
    public int OrderId { get; set; }

    public int DisplayOrder { get; set; }
}

Mapping:

var dto = new OrderDto { OrderId = 2 };
var vm = mapper.Map<OrderViewModel>(dto);
vm.DisplayOrder = 3;
abatishchev
  • 98,240
  • 88
  • 296
  • 433
0

Would it be best to deserialize to JSON, then add the new property and then serialize it into my new object with the extra property or is there another way to do it?

It's definitely would be easier and cost less efforts to use JSON.

But if you want more control over the generation process see that answer how to generate classes on the fly.

Community
  • 1
  • 1
rnort
  • 107
  • 1
  • 4