Oh I forgot to mention, if you want to run that test you'll need to get the
latest from the trunk, I had to make a small change to the sqlite
configuration class to ensure that the in memory db played nicely.

On Mon, Sep 15, 2008 at 11:30 AM, Paul Batum <[EMAIL PROTECTED]> wrote:

> Hi Chris,
>
> Here is your example implemented using two different tables for the
> different cat relationships. First the classes:
>
>     public class Cat
>     {
>         public int Id { get; set; }
>         public string Name { get; set; }
>     }
>     public class Rope
>     {
>         public int Id { get; set; }
>         public string Name { get; set; }
>     }
>     public class Person
>     {
>         public int Id { get; set; }
>         public string Name { get; set; }
>         public IList<Cat> OwnedCats { get; set; }
>         public IList<Cat> LikedCats { get; set; }
>
>         public Rope LeftHand { get; set; }
>         public Rope RightHand { get; set; }
>     }
>
> Next, the mapping:
>
>     class CatsPersistenceModel : PersistenceModel
>     {
>         public override void Configure(Configuration configuration)
>         {
>             addMapping(new CatMap());
>             addMapping(new RopeMap());
>             addMapping(new PersonMap());
>             base.Configure(configuration);
>         }
>
>         private class CatMap : ClassMap<Cat>
>         {
>             public CatMap()
>             {
>                 Id(x => x.Id);
>                 Map(x => x.Name);
>             }
>         }
>
>         private class RopeMap : ClassMap<Rope>
>         {
>             public RopeMap()
>             {
>                 Id(x => x.Id);
>                 Map(x => x.Name);
>             }
>         }
>
>         private class PersonMap : ClassMap<Person>
>         {
>             public PersonMap()
>             {
>                 Id(x => x.Id);
>                 Map(x => x.Name);
>                 HasManyToMany<Cat>(x => x.OwnedCats)
>                     .WithTableName("OwnedCats")
>                     .AsBag()
>                     .Cascade.All();
>                 HasManyToMany<Cat>(x => x.LikedCats)
>                     .WithTableName("LikedCats")
>                     .AsBag()
>                     .Cascade.All();
>
>                 References(x => x.LeftHand);
>                 References(x => x.RightHand);
>             }
>         }
>     }
>
> Finally, a demonstration test:
>
>         [Test]
>         public void CatsExample()
>         {
>             var cfg = new SQLiteConfiguration()
>                 .ShowSql()
>                 .InMemory()
>                 .ConfigureProperties(new Configuration());
>
>             new CatsPersistenceModel().Configure(cfg);
>             var factory = cfg.BuildSessionFactory();
>
>             using (var session = factory.OpenSession())
>             {
>                 using (var tx = session.BeginTransaction())
>                 {
>                     new
> NHibernate.Tool.hbm2ddl.SchemaExport(cfg).Execute(false, true, false, false,
> session.Connection, null);
>                     tx.Commit();
>                 }
>
>                 int id;
>
>                 using (var tx = session.BeginTransaction())
>                 {
>                     var paul = new Person();
>                     paul.OwnedCats = new List<Cat> { new Cat { Name = "Cat
> 1" }, new Cat { Name = "Cat 2" } };
>                     paul.LikedCats = new List<Cat> { paul.OwnedCats[0] };
>                     paul.LeftHand = new Rope { Name = "left" };
>                     paul.RightHand = new Rope { Name = "right" };
>
>                     session.Save(paul.LeftHand);
>                     session.Save(paul.RightHand);
>                     session.Save(paul);
>                     id = paul.Id;
>                     tx.Commit();
>                 }
>
>                 session.Clear();
>
>                 using (var tx = session.BeginTransaction())
>                 {
>                     var fromDB = session.Load<Person>(id);
>                     Assert.AreEqual(2, fromDB.OwnedCats.Count);
>                     Assert.AreEqual(1, fromDB.LikedCats.Count);
>                     Assert.AreEqual("left", fromDB.LeftHand.Name);
>                     Assert.AreEqual("right", fromDB.RightHand.Name);
>                 }
>             }
>         }
>     }
>
> It could probably be shorter by using conventions or automapping but I
> thought a verbose example might be helpful.
> Is this what you were after?
>
> Paul Batum
>
>
> On Mon, Sep 15, 2008 at 7:33 AM, Gabriel Schenker <[EMAIL PROTECTED]>wrote:
>
>> you can find my articles about the fluent interface now on the new
>> NHibernate site www.nhforge.org/blogs/nhibernate
>> this IS the new central place for any information related to nhibernate
>> and the contribution projects
>> Enjoy
>> Gabriel
>>
>> By the way: in pure xml mapping you could put a where filter to
>> distinguish the two case OwnCats and LikeCats
>> Not sure whether it's already implemented in fluent interface
>>
>>
>> On Sun, Sep 14, 2008 at 9:44 PM, <[EMAIL PROTECTED]>wrote:
>>
>>>
>>> I don't think it is so off-topic...
>>>
>>> I dont't want to start a general discussion about design approach, but
>>> there should be at least an example a little bit more complex than the
>>> current at http://code.google.com/p/fluent-nhibernate/wiki/QuickStart
>>>
>>> I really liked these posts here:
>>>
>>> http://blog.jagregory.com/2008/08/08/introducing-fluent-nhibernate/
>>>
>>> http://www.iamnotmyself.com/2008/08/07/SkinningTheCatWithFluentNHibernate.aspx
>>>
>>> http://blogs.hibernatingrhinos.com/nhibernate/archive/2008/08/15/a-fluent-interface-to-nhibernate---part-3---mapping.aspx
>>>
>>> Do you know the people who wrote them. I would suggest we move them in
>>> a sctructered way inside the WIKI. I would spend some time on this, as
>>> I need to get a hold of this topic anyway. We just need to check A)
>>> which kind of "draft account" I need for the (or a) wiki, and if the
>>> authors agree with this..
>>>
>>> But perhaps you are right, we should move this topic to mail or to the
>>> Wiki...
>>> Chris
>>>
>>>
>>>
>>>
>>
>> >>
>>
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Fluent NHibernate" group.
To post to this group, send email to fluent-nhibernate@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/fluent-nhibernate?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to