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
awesome! I suppose that Model could be an anoymous type or any type that expose those public properties.
ReplyDeleteModel and ProductLine are both anonymous types.
ReplyDeleteAs 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@Darius
ReplyDeleteNo is not. I'm using NH's dynamic entity-mode
@Darius
ReplyDeleteAs you can see there isn't a real class in the mapping (only entity-name, no class nor interface).
Could you provide an scenario where this feature may be useful?
ReplyDeleteIn the same place where will be useful work with dynamic objects (.NET4) ?
ReplyDeleteor persist a mapping created on the fly without implement concrete classes nor interfaces?
or use directly a DTO to persist something using DuckTyping ?