Summer of NHibernate Session 07: Exploring m:n Relationships, Views, and Components

I am pleased to announce that I have now posted for download the latest installment of the Summer of NHibernate screencast series.  In this series we explore NHibernate’s support for many-to-many relationships in both our data and our object model, understand how to interact with views, and investigate some ideas about how to leverage NHibernate’s ‘Component’ modeling to help us avoid the ‘primitive obsession’ anti-pattern in our object model.

I also refactor our tests and our data-access-layer to reflect the introduction of the new Name component to our Customer class.

For the record, I am indeed aware that the results of this refactoring as shown in the screencast (that leads to code like Customer.Name.Firstname) represents a clear violation of the Law of Demeter, but I didn’t want to complicate the object model during the screencast.  In a real-world implementation of the Customer class bolstered by the Name class as an NHibernate ‘component’, its likely that this would instead result in a Customer class that looked more like…

public class Customer
{

    //...

    public virtual string Firstname
    {
        get { return _name.Firstname; }
        set
        {
            _name.Firstname = value;
        }
    }

    public virtual string Lastname
    {
        get { return _name.Lastname; }
        set
        {
            _name.Lastname = value;
        }
    }

    public virtual string Fullname
    {
        get { return _name.Fullname; }
        
    }
    //...
}

…so that accessing the Customer.Firstname and Customer.Lastname properties would pass-thru to the Customer class’ internal Name instance and the Customer.Fullname property would be a read-only pass-thru to the same-named property of the Name object maintained by the Customer.  In this way, consumers of the Customer class need not be aware of the presence of the Name class within the Customer class at all but would still benefit from the ‘richness’ provided by the Name class’ behavior.

Of course, it also now dawns on me that if I had proceeded that way in the screencast, none of the NHibernateDataProvider or NHibernateDataProviderTest methods would have had to have been touched since in a sense the Customer interface wouldn’t have actually changed smile_embaressed.  Just some thoughts to consider~!

Technical Changes

This installment also represents the first that incorporates a few of the technical changes that have been requested by various people in prior feedback:

  • the file format has been changed to wmv (windows media, video) so that (as requested by some) it can be watched @ 1.5x speed in Windows Media Player (hey, whatever works for you –! smile_sniff)
  • I’ve changed my VS IDE font selection from Courier New to Lucida Console; this change should result in the code in the editor being a bit easier to read without my needing to increase the point size at all (and take up too much more screen real estate)

As always, comments, feedback, etc. are appreciated.