Try fast search NHibernate

07 July 2009

Dynamic entities

Waiting the release of .NET4 here is a proof of concept (for real a passing test) of what you will see in NHibernate4.0.0.

<class entity-name="Model">
<
id name="Id" type="int">
<
generator class="hilo"/>
</
id>

<
property name="Name" not-null="true" length="25" type="string"/>
<
property name="Description" not-null="true" length="200" type="string"/>
<
many-to-one name="ProductLine"
column="productId"
not-null="true"
class="ProductLine"
cascade="save-update"/>
</
class>
using (ISession s = sessions.OpenSession())
using (ITransaction t = s.BeginTransaction())
{
s.SaveDynamic("Model", new
{
Name = "Locus",
Description = "Audi Locus",
ProductLine = new
{
Description = "concept car"
}
});
t.Commit();
}

using (ISession s = sessions.OpenSession())
{
s.CreateQuery("from ProductLine pl order by pl.Description").List()
.Count.Should().Be.Equals(1);
}

NHibernate… waiting the future.

UPDATE: Was tested using NH2.1.0

7 comments:

  1. awesome! I suppose that Model could be an anoymous type or any type that expose those public properties.

    ReplyDelete
  2. Model and ProductLine are both anonymous types.

    ReplyDelete
  3. As far as i understand, under the hood "SaveDynamic" just iterates over all mappings and finds the right using the passed string. Is that correct?

    ReplyDelete
  4. @Darius
    No is not. I'm using NH's dynamic entity-mode

    ReplyDelete
  5. @Darius
    As you can see there isn't a real class in the mapping (only entity-name, no class nor interface).

    ReplyDelete
  6. Could you provide an scenario where this feature may be useful?

    ReplyDelete
  7. In the same place where will be useful work with dynamic objects (.NET4) ?
    or persist a mapping created on the fly without implement concrete classes nor interfaces?
    or use directly a DTO to persist something using DuckTyping ?

    ReplyDelete