Update on Computed Properties

Published 13 January 09 04:27 AM | efdesign 

A while back I wrote a post that introduced the concept of Computed Properties. Since that time we’ve had a number of conversations with both customers and internal partners and we’ve had some new ideas, that have changed our thinking somewhat. 

  • First off we decided to implement Model Defined Functions, which are significantly more powerful and flexible than Computed Properties, and provide overlapping capabilities.
  • Computed Properties on the other hand, would never have been structurally part of the entity, which lead to some tough choices and probable customer confusion:
    • Do you always pay the cost of materializing the Computed Property even when it is not required?
    • What about if there are lots of Computed Properties on the Entity?
    • Today in order to materialize an Entity and it's properties, computed or otherwise, the Entity Framework needs access to setters. Unfortunately the existence of the Setter, sends an erroneous message that Computed Properties can be altered and persisted.
    • Upon further investigation we discovered that Computed Properties didn't actually address the requirements of Reporting Services, which was one of the original drivers for Computed Properties. Our original design called for computed properties to operate over an instance of an Entity, and it turned out that Reporting Services needed computed properties to operate over both an instance and a set of instances. Model Defined Functions on the other hand are flexible enough to handle both of these scenarios.
  • In the future it may be possible to gain all the benefits of computed properties without any of the above issues, by extending our integration of Model Defined Functions with LINQ so that the [EdmFunction] attribute can be applied to CLR properties too.

    If we did this the first parameter of the specified Model Defined Functions could be mapped to be the CLR type containing the property itself.  This would allow a Computed Property, backed by a Model Defined Function, to be used in a targeted way in LINQ queries, without any of the illusions that this might somehow be set-able or structurally part of the entity.

Given all these points we started to think Computed Properties just weren’t as important.

We think our best course of actions in the short term is 1) to focus on supporting Model Defined Functions and 2) ask for your feedback on the relative importance of Computed Properties given the points listed above.

So what do you think?

Do Model Defined Functions address most of the scenarios for Computed Properties?

Particularly if in the future we can treat CLR properties as stubs for Model Defined Functions?

As always we are very keen to hear what you think.

Alex James
Program Manager
Microsoft

This post is part of the transparent design exercise in the Entity Framework Team. To understand how it works and how your feedback will be used please look at this post.

NB:
For more background on the origins of [EdmFunction] check out this post.

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# Kristofer said on January 13, 2009 12:33 AM:

My 0.7 baht (== ~2 cent):

"<i>Do you always pay the cost of materializing the Computed Property even when it is not required?</i>"

No. Only materialize when called for, otherwise it is not a property or function; always materialized==fields. Always materializing would make computed properties and MDFs banned in many dev shops. (something that _always_ result in [hidden] overhead is a really good way to shoot oneself in the foot)

Even for non-computed members I think you need to re-think how entities are populated today [in EFv1]. Partially populated entities (without needing a new 'partial' entity definition in the CSDL) is something desperately needed. (northwind example: employee details without photo)

"<i>CLR properties as stubs for Model Defined Functions</i>"

No. At least not without a way to flag it as 'not part of the model', e.g. a "[NotInModel()]" or "[EFIgnore()]" attribute.

I think MDFs should be exposed as regular functions, not property gets. CLR properties on entities should be allowed _without_ anything under it in the model.

---

MDFs would already cover 99.5% of the usage scenarios I can imagine for computed properties. 100% if "reverse computation"/"property set" is not supported. That said, computed properties with reverse breakdown/"property set" definitions would be neat. E.g. person name parts <-> person full name.

# efdesign said on January 13, 2009 1:16 AM:

Kristofer,

Partial loading as you call it is definitely something we have been considering (i.e. the don't load the big blob unless I really need it scenario). It isn't looking likely in .NET 4.0 though.

CLR Properties without anything under them in the model are already supported. The caveat of course is the value isn't persisted and it can't be used in a LINQ Query. So I think this is a somewhat orthogonal idea.

Note: not adding the [EdmFunction] attribute to the CLR property would be the equivalent of adding your [NotInModel()] attribute.

So I see little point in the [NotInModel()] attribute, essentially it is the default state of a Property that has no corresponding Property in the EntityType.

Cheers

Alex

# Kristofer said on January 13, 2009 4:38 AM:

'Partial loading' should be really high up on the feature list IMO - it is very important from a performance perspective...

Ok, my bad re the attributes. I assumed that "CLR properties as stubs for Model Defined Functions" meant that CLR properties with no mapping would [by default] attempt to find/call a matching function.

Either way, separating properties and functions in the model so they map to properties and functions in the generated classes is probably a good idea. Very little overhead implementation-wise but allows users the flexibility to choose which one to use.

And as I mentioned - for properties, an optional definition for 'setter' logic (again defined in ESQL, RegExp, or a method call to an assembly or webservice) would open up a bunch of possibilities:

<Property Name="FullName" Type="String" Nullable="false" MaxLength="30" Unicode="true" FixedLength="false" Computed="true" Ordinal="4">

   <DefiningExpression Type="Get" Language="ESQL">FirstName + ' ' + LastName</DefiningExpression>

   <DefiningExpression Type="Set" Language="RegExp">^(?<Title>[ ][A-Z]+)*(?<FirstName>[A-Z]+)(?<MiddleName>([ ][A-Z]+)?)[ ](?<LastName>([A-Z]+[ ])*([A-Z]+))$</DefiningExpression>

</Property>

...or the appropriate extensibility interfaces allowing third-party implementations of the same. :)

T4 for code generation goes a long way towards the latter, but some additional extensibility points in the visual designer to complement that would be neat; allowing third-party extensions/add-ins to add new properties [mapped to their own 'structural annotation' members] in the designer etc

# shawn said on January 19, 2009 5:11 PM:

I think having some way to define Computed Properties, either directly or backed by Model Defined Functions, is critical for being able to fully express entities in an entity/object-oriented way.  It allows you to define and query data as part of the entity/model itself.  Often times this is the preferable way for computed data to be consumed.  It certainly is in our scenarios.  This is one of the features we are most excited about.

# Pedro said on January 20, 2009 8:16 AM:

Sorry to go a little of topic here, but I was wondering if I could download a preview version of EF v2 just to play with it.

Thanks.

# dsimmons@microsoft.com said on January 20, 2009 4:10 PM:

EF v2 preview is not available yet.  We're working on that and will make something available as soon as we reasonably can.

- Danny

# Pedro Brown said on January 21, 2009 8:06 AM:

Thank you for your answer Danny.

I have to say that I'm really excited about what is beeing done in the EF. What I want to know is: Can we make suggestions to make EF more a MDD framework, or it's purpose is just to be an O/R M and leave the MDD area to Oslo?

Thanks.

# Pedro Brown said on January 21, 2009 8:17 AM:

Sorry, MDD is (Model Driven Desing), but I think that what you guys use is DDD (Domain Driven Desing).

Thanks again.

# Ike said on February 17, 2009 2:55 PM:

One great benefit of computed properties would be to add non-model data to entites being delivered via REST (ADO.NET Data Services).  Interjecting "computed" data into my entites has become absolutely necessary and since I...

- can't do that with EF (computed properties would be nice?) so ADO.NET Data Services is out

- can't return a JSON-serialized object that includes a EF entity item from a WCF operation if that EF entity includes a reference to another EF entity (known issue...very specific error occurs if you try to do that)

...I have had to resort to creating separate contract types for each EF entity type I want to return as JSON.

If EF allowed me to add computed properties, I could scrap most of my contract types.

Leave a Comment

(required) 
(optional)
(required) 

  
Enter Code Here: Required

Search

This Blog

Syndication

Page view tracker