Calculated property mapping

285 views
Skip to first unread message

Germán Schuager

unread,
Dec 16, 2008, 2:21:51 PM12/16/08
to nhu...@googlegroups.com
Hi,

Is there some mapping strategy to have a calculated property persisted into the database (for querying purposes) but not read back?

Something like this:

public int Total
{
   get { return calculateTotal(); }
}


Regards,

Germán Schuager
http://blog.schuager.com

Fabio Maulo

unread,
Dec 16, 2008, 3:40:26 PM12/16/08
to nhu...@googlegroups.com
;) and why you don't want it read-back ?
Are you sure that you and want it back ?

public Item AddItem()
{
InvalidateTotal();
}

private decimal total?;
public decimal Total
{
get
{
if(!total.HasValue)
total = CalculateTotal();
return total.Value;
}
}

2008/12/16 Germán Schuager <gsch...@gmail.com>



--
Fabio Maulo

Fabio Maulo

unread,
Dec 16, 2008, 3:41:33 PM12/16/08
to nhu...@googlegroups.com
Sorry....
Are you sure that you don't want it back ?
public Item AddItem()
{
InvalidateTotal();
....
return ....
}

etc. etc.

2008/12/16 Fabio Maulo <fabio...@gmail.com>



--
Fabio Maulo

Germán Schuager

unread,
Dec 16, 2008, 5:59:12 PM12/16/08
to nhu...@googlegroups.com
Thanks Fabio, I'm currently doing something like this:

public decimal Total
{
   get { return calculateTotal(); }
   set { }  // I really don't like this
}

Supose that calculateTotal() depends on several other properties and that its result changes because of several other things besides the AddItem method... I'd prefer to calculate it every time rather than clutter the other properties setters with InvalidateTotal calls.

What do you thing about adding a "read-only" access strategy to NHibernate? I could try to do it if it is posible.


Regards,

Germán Schuager
http://blog.schuager.com



Fabio Maulo

unread,
Dec 17, 2008, 6:07:05 AM12/17/08
to nhu...@googlegroups.com
2008/12/16 Germán Schuager <gsch...@gmail.com>

What do you thing about adding a "read-only" access strategy to NHibernate? I could try to do it if it is posible.

Sure, It is. I have a note about it since 2 years (I have deleted it 3 weeks ago).
You can use access field or better a "nosetter" and, in general, a fresh load from DB don't need a re-calculate if nothing change so...
Each time I had start the "read-only" implementation my mind go to real-world usecase and...

--
Fabio Maulo

Germán Schuager

unread,
Dec 18, 2008, 7:28:36 AM12/18/08
to nhu...@googlegroups.com
Hi, I've wrote some code that seems to work, should I open a Jira issue and attach a patch for review?

Fabio Maulo

unread,
Dec 18, 2008, 7:36:57 AM12/18/08
to nhu...@googlegroups.com
2008/12/18 Germán Schuager <gsch...@gmail.com>

Hi, I've wrote some code that seems to work, should I open a Jira issue and attach a patch for review?

The JIRA is there.

--
Fabio Maulo

Germán Schuager

unread,
Dec 18, 2008, 8:14:49 AM12/18/08
to nhu...@googlegroups.com
I've very sorry Fabio, but I couldn't found the issue... do you remember its summary or any text that can help me to find it?

Fabio Maulo

unread,
Dec 18, 2008, 8:19:59 AM12/18/08
to nhu...@googlegroups.com
Germán I mean....
The JIRA is there and when you want you can add your ticket with explication and the test case and, if you want the patch.

By "patch" we mean a patch file created using TortoiseSVN or other SVN client.

2008/12/18 Germán Schuager <gsch...@gmail.com>



--
Fabio Maulo

Germán Schuager

unread,
Dec 18, 2008, 9:11:57 AM12/18/08
to nhu...@googlegroups.com
Thanks Fabio, I've misunderstood you.
I've just created the issue and attached the patch: http://jira.nhibernate.org/browse/NH-1621

Regards.

Broken Pipe

unread,
Jun 15, 2016, 1:41:22 PM6/15/16
to nhusers
It was implemented, long ago, but the implementation is a bit naive IMHO.

Checkout the bug I submitted: https://nhibernate.jira.com/browse/NH-3881
Reply all
Reply to author
Forward
0 new messages