Functional Responsibilities with nHibernate

18 views
Skip to first unread message

Stacey Thornton

unread,
Aug 25, 2011, 7:53:20 AM8/25/11
to nhusers
I was wondering if anyone had some examples of any ways they have
solved functional programming issues with nHibernate (or Fluent). To
be clear, by functional I am referring to using anonymous functions
and method deconstruction as part of the serialized data, similar to
the way Javascript and (to my [EXTREMELY LIMITED] understanding) F#
works.

I have a situation where I need to call upon complicated anonymous
methods, however the context that they are called in is impossible to
'know' at database save time. I need to store the logic to 'unwrap'
references, as it were, where the references are nhibernate backed
DTOs. So for my example..

class Aspect { // ... }

class Observable {
virtual Aspect Aspect { get; set; }
virtual double __latest_value { get; set; }
}

class Mutation {
virtual Aspect Aspect { get; set; }
virtual double Value { get; set; }
}

__latest_value does not come from the "Aspect" class, but instead it
comes from taking Mutations of data relative to the context of what is
being observed. So then, a User might have 50 mutations to a single
Aspect. The Observable is used in a mathematical function, and is to
represent the value in relationship to the current User, but because
the Aspect itself is a late-bound object (not hard-coded by name/id),
I cannot just pass the exact object through the formula. So, in this
instance I am coding for a game. I cannot say ..

var Health = ( Character["Stamina"] * 3 );

because "Stamina" (an Aspect) doesn't exist. The "Character" has a
list of Mutations, each of which might look like this.

Character.Mutations[0] = { Aspect = (stamina aspect), Value = 1.0 };
Character.Mutations[1] = { Aspect = (stamina aspect), Value = 3.0 };

(( this is NOT how my muations are structured, I am just using this as
the absolute most simplistic, basic example ))

Because "Stamina" is nothing but a string literal for the name of an
aspect, I cannot use it in computation. I would have to have the
appropriate Aspect Id. That is not known until the Aspect is saved to
the database. And even if I knew the ID, Hard-coding math against a
database Id by string literal is bad practice. So my solution is to
wrap things that need to be 'unwrapped' into this "Observable" class.

I am wondering if there is any way that nHibernate can help me with
this logic. If there is anything I can do to make it a simpler
process. I have tried the following;

MetaLinq - Serializing Lambda Expressions to XML
- This worked, but it was too slow and proved to yield massive
record sizes.
ExpressionSerialization
- Couldn't get it to work with .NET 4.0, ended up defaulting back to
MetaLinq
Hard-Coded Anonymous Method Dictionary
- Methods were coded and given a string literal name, and the
Parameters stored in the database. This is extremely unruly and hard
to maintain.

I am open to using HQL, but I do not know it, and I find it to be a
very confusing way of doing things. If anyone has any ideas/examples,
I would be very, very open to them. I am not against implementing new
libraries to solve it, if it will actually solve it.

Jason Meckley

unread,
Aug 25, 2011, 11:21:37 AM8/25/11
to nhu...@googlegroups.com
I don't know about storing the anonymous functions directly in the database. a RDBMS store data, not objects. you could store a value that represents the function and map the value to the function using an IUserType or ICompositeUserType. This "forumla" object could use double dispatch to pull the specific values required to make the computation.

all that said, running a game that has an ORM/database on the backend seems odd. with game data being so dynamic, and latency needing to be low, it would seem a non-relational persistent storage option would make more sense.

Stacey Thornton

unread,
Aug 25, 2011, 12:02:23 PM8/25/11
to nhusers
I am not familiar with any non-relational persistent storage systems.

Well, the data is only saved in the database, most computation is done
by functional procedures via javascript and C# at runtime, but it runs
over a browser and it is not a point of access style system. There is
not an on-demand calculation, it is more of a 'storage' for game data
that is used elsewhere, so latency is not the concern.

Jason Meckley

unread,
Aug 25, 2011, 12:55:52 PM8/25/11
to nhu...@googlegroups.com
if your interested google "no-sql" and you will get an plethora of information. there is also a good overview of the various types at http://ayende.com/blog/tags/nosql
if the formulas are computed in memory, then implementing an IUserType/ICompositeUserType for the formulas is probably the way to go.
Reply all
Reply to author
Forward
0 new messages