it's possible to set a custom strategy to get the discriminator-value?

5 views
Skip to first unread message

kor

unread,
Nov 13, 2009, 8:44:50 AM11/13/09
to nhusers
hi, i'm using a table-per-hierarchy mapping.

when i don't specifiy a discriminator-value in the sublasses nh will
used the specific entity type.

i want know if i can use a custom strategy (for example to use only
the class name or taking the value from a custom attribute on that
type).

is it possible?

Fabio Maulo

unread,
Nov 13, 2009, 9:14:05 AM11/13/09
to nhu...@googlegroups.com
yes, it is.

2009/11/13 kor <kork...@yahoo.it>



--
Fabio Maulo

kor

unread,
Nov 13, 2009, 9:16:01 AM11/13/09
to nhusers
.. and where? i try to search in the code but i didn't found where i
can inject it?

Fabio Maulo

unread,
Nov 13, 2009, 9:25:47 AM11/13/09
to nhu...@googlegroups.com
the discriminator has <type> and has <formula>

2009/11/13 kor <kork...@yahoo.it>


.. and where? i try to search in the code but i didn't found where i
can inject it?




--
Fabio Maulo

kor

unread,
Nov 13, 2009, 9:39:40 AM11/13/09
to nhusers
thanks for your reply, sorry but i continue to don't understand :(
with <type> you mean to use and iusertype or samething similar?

<formula> i think that is useless becouse it works at sql level and i
need to work at .net level (for example to get the .net attribute for
a type), i'm right?

Fabio Maulo

unread,
Nov 13, 2009, 10:11:50 AM11/13/09
to nhu...@googlegroups.com
I don't know...
perhaps if you try to explain what you thinking is your needs, we can help you in some way.

The entityName is probably the best candidate for a discriminator (so true that it is used even in OO-DB).
The other possibility is a more simple info as a number.
Any other solution is to solve some problem with legacy DB.

Which is your need ?

2009/11/13 kor <kork...@yahoo.it>



--
Fabio Maulo

kor

unread,
Nov 13, 2009, 10:46:16 AM11/13/09
to nhusers
my is not a real need, my target is to don't explicity set the
dicriminator-value on every xml node of subclasses becouse i'm
lazy :)

in my case my subclasses are in the form

[MyAttribute('NameOfA")]
class SubClassA : Base { }

[MyAttribute('NameOfB")]
class SubClassB : Base { }

where MyAttribute is an attribute that i put on Base implementation
for other purpose but wich has a property Name.

anyhow nhibernate is great also becouse it has hundred of place where
we can inject custom logic so i'm asking if exist also a point where i
can inject a custom strategy to get the discriminator-value when is
not set in the xml file (
(i think to samething as

interface IDiscriminatorValueGenerator
{
object GetDiscriminatorValue(object entity)
}
)

note: the motivation is not only to write less xml; at the moment we
are already choosing if we will use as discriminator-value the class
name or the Name property taken from the attribute MyAtt., so if i can
make 2 implementation of IDiscriminatorValueGenerator and in the
future we change the strategy we must only change the type of the
implementation without rewriting all the discriminator-value
attributes)

Fabio Maulo

unread,
Nov 13, 2009, 11:43:42 AM11/13/09
to nhu...@googlegroups.com
and what is "MyAttribute" other than write the same thing in the class instead in the mapping ?

If you like it you can use NHMA (Mapping Attribute) or Fluent-NHibernate

2009/11/13 kor <kork...@yahoo.it>



--
Fabio Maulo

kor

unread,
Nov 13, 2009, 12:10:10 PM11/13/09
to nhusers
i simplify too much :(
MyAttribute is used to add additional info to my subclasses type but
that data is not related to nhibernate/ db persistance.

The only importat thing is that MyAttribute contains a property called
name that is 100% sure to never change (i mean that the class name/
type in future can change after same code rafactoring,
MyAttribute.Name no) so we think to use that as discriminator so we
must no change the db if we make same code rafactoring)
Reply all
Reply to author
Forward
0 new messages