Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic Properties won't work because of Json.Net #53

Closed
KeilFelix opened this issue Oct 24, 2017 · 8 comments
Closed

Dynamic Properties won't work because of Json.Net #53

KeilFelix opened this issue Oct 24, 2017 · 8 comments

Comments

@KeilFelix
Copy link

I use dynamic Properties in my ViewModels.

See this for further info:
http://putridparrot.com/blog/dynamically-extending-an-objects-properties-using-typedescriptor/

These prop work well in WPF but get lost in the JSON Serializer.

I have a temporary workaround in fork and I am currently trying to find a better solution.

https://stackoverflow.com/questions/46888732/how-to-serialize-runtime-added-properties-to-json

Btw. there is no dotnetify tag on stackoverflow yet and I couldn't add it with <1500 rep. Maybe you can add it?

PS: dotnetify is awesome! Thank you!

@dsuryd
Copy link
Owner

dsuryd commented Oct 24, 2017

Hi, what's your use case for having dynamic properties in a view model?

I'm afraid I have even less stackoverflow reputation; @tw-bert, can you help?

@tw-bert
Copy link

tw-bert commented Oct 24, 2017

Sure, tag is added.

@KeilFelix
Copy link
Author

KeilFelix commented Oct 26, 2017

@dsuryd Writing out the properties and different viewmodels that don't contain any or just minor extra logic often feels really dumb and repetitive for me.

Thats why I have this in my ViewModel Base Class:

public void RegisterDynamicProp<T>(string name, IObservable<T> property)

and some other variants of it.
I use these methods sometimes inside the ViewModels constructor or outside to completely remove the need to create the class.

Example:

            ViewModelBase example = new ViewModelBase();
            example.RegisterDynamicProp<int>("Value", exampleObservable);
            etc...

@KeilFelix
Copy link
Author

I have progress on this. Thanks to dbc at Stackoverflow I could implement a TypeDescritorContractResolver that does the serialization now without my workaround.

While I think my special problem has low priority for dotnetify, I still think that dotnetify should approach the same resolution strategy and possibilities with properties like WPF.

I will update the fork soon.

@tw-bert
Copy link

tw-bert commented Oct 29, 2017

@KeilFelix : I wouldn't say this problem is so special. At our company (not related to dotnetify at all, but starting to use it, since it's great) we use dynamic properties and Expando objects and the likes as well, and we would benefit a lot from this addition. Thank you for your input!

@dsuryd
Copy link
Owner

dsuryd commented Oct 30, 2017

The stackoverlow solution looks good! Is there a way to check beforehand whether an object is imbued with TypeDescriptor properties, in order to apply the appropriate contract resolver?

@dsuryd
Copy link
Owner

dsuryd commented Dec 3, 2017

The latest in master branch has full Rx support, including dynamic properties and view model types.

Example with dynamic type + properties:

VMController.Register("HelloWorldVM", _ =>
{
   var vm = new BaseVM();
   var firstName = vm.AddProperty("FirstName", "Hello");
   var lastName = vm.AddProperty("LastName", "World");

   vm.AddProperty<string>("FullName").SubscribeTo(
      Observable.CombineLatest(firstName, lastName, (fn, ln) => $"{fn} {ln}"));
   return vm;
 });

Example without BaseVM:

public class HelloWorldVM : INotifyPropertyChanged, IReactiveProperties
{
   public event PropertyChangedEventHandler PropertyChanged = delegate { };
   public IList<IReactiveProperty> RuntimeProperties { get; } = new List<IReactiveProperty>();

   public HelloWorldVM()
   {
      var firstName = this.AddProperty("FirstName", "Hello");
      var lastName = this.AddProperty("LastName", "World");

      this.AddProperty<string>("FullName").SubscribeTo(
         Observable.CombineLatest(firstName, lastName, (fn, ln) => $"{fn} {ln}"));
   }
}

I don't use TypeDescriptor,and don't see the need for dotNetify to support its serialization, now that the motivation for it has been realized through other means. But if you still want to use it, you can implement your own view model serializer.

@KeilFelix
Copy link
Author

This looks really great! I think that this issue can be closed. The BaseVm class has really improved. Very good idea with the wrapper constructor too! Kudos

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants