Coalesce icon indicating copy to clipboard operation
Coalesce copied to clipboard

Calculated fields should auto generate knockout computed properties (read only)

Open finlaysonc opened this issue 7 years ago • 3 comments

Request: A [Computed] attribute on a property model which would make the field a knockout computed property on the client side with the proper computation automatically generated ala the way knockout mvc does (http://knockoutmvc.com/InnerComputed.)

Justification: This way the client immediately reflects the changes when the dependent properties are changed; as of now, the computed properties only update when the dependent values are saved. Also, in the case of a "true" DB computed property set up as described in https://docs.microsoft.com/en-us/ef/core/modeling/relational/computed-columns , a setter does need to be defined, so Coalesce will incorrectly make the computed property editable in the admin views. This can be worked around by attributing the property with [Read].

Implementation Considerations: Since a calculated field can accomplished in at least two ways: 1) [NotMapped] property getter, and 2) by setting up as a "true" computed column in the DB, we can't infer a calculated field from the [NotMapped] property - perhaps a new attribute (e.g. [Computed]). To auto-generate the computed knockout formula may get a bit tricky when the field is defined as a computed column in the DB - but perhaps (?) that info could be obtained through reflection. [Computed] would also make the property not editable on the client side.

finlaysonc avatar Jun 24 '17 23:06 finlaysonc

This seems like a good idea in theory at least. I see that knockoutmvc is doing it. It would be cool to be able to fully convert the c# to ts. Not sure exactly what they are doing there. The cases shown were pretty simple. We may want to open another issue for the bug with the existing editable read only properties.

GrantErickson avatar Jun 25 '17 12:06 GrantErickson

In response to: "We may want to open another issue for the bug with the existing editable read only properties." - I don't think I was clear in my explanation. Read only properties are correctly not editable. e.g. with the current canonical way to handle calculated fields in Coalesce, one has a [NotMapped] get-only property. This correctly generates a not editable field in coalesce. What doesn't work, however, is if you create a DB computed column via EF as descrbied in https://docs.microsoft.com/en-us/ef/core/modeling/relational/computed-columns. These types of properties, while read only still require a setter (even if it is a private setter). It's in this case coalesce incorrectly generates an editable field. Attributing these properties with [Read] does work. It's just that ideally Coalesce would "know" this is a computed property because of its definition in the model builder and make it read only and knockout computed.

finlaysonc avatar Jun 25 '17 18:06 finlaysonc

Knockout MVC uses https://github.com/hazzik/DelegateDecompiler to decompile the bodies of getters into expressions at runtime. It then uses a quite naive expression visitor to translate the expression into knockouty javascript. https://github.com/AndreyAkinshin/knockout-mvc/blob/master/PerpetuumSoft.Knockout/Utilities/KnockoutExpressionConverter.cs - lots of NotSupportedException. Also has the limitation of only working on method bodies that can actually be represented as expressions.

ascott18 avatar Jul 30 '20 19:07 ascott18