FSharp.Data icon indicating copy to clipboard operation
FSharp.Data copied to clipboard

Support data-binding

Open tpetricek opened this issue 12 years ago • 19 comments

Implement ICustomTypeDescriptor in type providers to support data-binding. I hope that should be sufficient.

tpetricek avatar May 07 '13 17:05 tpetricek

This makes me think - will we have to represent rows (in CSV provider) using our own Row type rather than using tuples, so that the row type can implement the interface too?

tpetricek avatar May 10 '13 01:05 tpetricek

Usually you databind to an intermediate viewmodel, as rarely the data is right in the format you want to display it in, but I do agree that when prototyping, being able to just databind to a type provider would be nice. The tuple gives us a couple of nice properties, but we can keep it, wraping it in a Row type, and still being able to implement ICustomTypeDescriptor, it's perfectly doable.

ovatsus avatar May 10 '13 08:05 ovatsus

Actually, this would be much more useful in the Json provider than in the Csv provider.

ovatsus avatar May 10 '13 09:05 ovatsus

I do agree that this is more important for the other providers than CSV. But having that for CSV would enable some nice scenarios too.

tpetricek avatar May 10 '13 11:05 tpetricek

Yes, CSV is actually the easier one to implement. I was just trying out prototyping it for the JsonProvider, and it's a bit more tricky

ovatsus avatar May 10 '13 11:05 ovatsus

Ah crap, WPF has ICustomTypeDescriptor, Silverlight has ICustomTypeProvider, and Windows Store has neither, so this won't work in the portable version

ovatsus avatar May 10 '13 11:05 ovatsus

I was just trying out prototyping it for the JsonProvider, and it's a bit more tricky

Can you elaborate a bit? What are the tricky things with the JsonProvider?

WPF has ICustomTypeDescriptor, Silverlight has ICustomTypeProvider

Oh, great... that sucks!

tpetricek avatar May 10 '13 11:05 tpetricek

It's just passing all the information hierarchically from compile time to runtime, so we don't have to implement everything twice. Maybe it would be best just to make the JsonValue and CsvFile play nicer with data binding, similar to what can be already done today with XElement, and leave this out of the type providers. You do loose most of the static typing benefits with data binding anyway...

ovatsus avatar May 10 '13 12:05 ovatsus

Yes, I think this could be done just by implementing the interface in the runtime classes (not touching the design time code).

(That's why I thought supporting it for CSV would be tricky as we do not control the runtime type).

tpetricek avatar May 12 '13 20:05 tpetricek

Actually, the CSV is the most C# friendly right now. In code you get Item1 and Item2 with the right types, and can use {Binding Item1} and {Binding Item2}. It's not ideal, but at least it's possible to use. Json is the worst one, as everything is erased, and the JsonValue DU is completely unusable inside a data binding. What if we changed the json provider to use generated types instead of erased ones? What would be the drawbacks of that?

ovatsus avatar Jun 27 '13 08:06 ovatsus

I just tried to use a JsonProvider-created type in a WPF control with data binding. It did not work, as implied by the above discussion. Is this the latest on the topic? If so, I must re-create the JsonProvider-created type and copy all the data over. That seems to defeat the purpose of having a JsonProvider -- at least for a WPF app. If I must define my own type, I might as well just use a JSON deserializer.

WallaceKelly avatar May 12 '14 18:05 WallaceKelly

Yes, JsonProvider still doesn't work with WPF binding and other similar reflection based systems

ovatsus avatar May 12 '14 19:05 ovatsus

@isaacabraham want to take to shot into changing Csv provider into a generated tp? It would play nicer with your Azure tp

ovatsus avatar Jun 12 '14 18:06 ovatsus

Please keep erasure the default, eg. make generative an optional parameter or different provider. Generative providers add significant complexity in a number of ways.

dsyme avatar Jun 12 '14 19:06 dsyme

@dsyme Can you elaborate in what ways do they add complexity (e.g. performance, potential for bugs, difficulty with assembly loading)? (Because the number of types in case of XML, JSON and CSV provider would probably be quite manageable...)

tpetricek avatar Jun 12 '14 19:06 tpetricek

I like the idea of erased type providers more than generative ones, but unfortunately:

  • They don't work with data-binding by default. You can try to workaround it using ICustomTypeDescriptor, but it only works in WPF, and not Xamarin, Silverlight, Windows Phone, or Windows Store
  • They don't work with other libraries that use reflection to get property names like Deedle and the Azure TP
  • They cause problems when they are used trough libraries and not directly in apps, because the TP will be re-evaluated. This causes problem in FSharp.Data.Toolbox and the like

Generative types have these disadvantages:

  • Have to generate the full tree ahead of time, so they might be slower at compile time
  • For the same reason create larger binaries
  • I have the gut feeling it will be harder to debug the output of the TP, but I'm just guessing

I haven't implemented any generative TP yet, so I'm not sure about added complexities in implementing them, but I'm guessing it should be about the same as erased ones

What am I missing?

ovatsus avatar Jun 12 '14 23:06 ovatsus

@ovatsus happy to have a look into this - although might a simpler (hackier?) option of just making the set of key/value pairs available on the erased type so that you can at least programmatically get to the values at runtime?

isaacabraham avatar Jun 14 '14 16:06 isaacabraham

I'd prefer the less hacky version. It will probably take the same time to implement and then we can share that approach more easily on the 3 providers

ovatsus avatar Jun 14 '14 17:06 ovatsus

Wow, any updates on this one? it would be nice to see actual types and properties from C# when referencing an F# library using JsonProvider or others in FSharp.Data :) also making it an optional parameter would be awesome. Sometimes this is a limit for integrating F# types provider with C#, so it might be worth it for "enterprise adopters" to have actual generated types in the final assembly, so that they can use them from C#.

jkone27 avatar Apr 06 '20 18:04 jkone27