Ayende @ Rahien

Refunds available at head office

Answering Mats' Challenge

Mats Helander has a challenge for OR/M developers, and Mats should know, since he is behind NPersist.

Go for the post for details, but basically it is loading all Customer->Orders->OrderLines graph in 3 statements or less.

Because I am a sucker for challenges, I implemented it with ActiveRecord. In Mats' terms, the code is so simple it hurts, and yes, I cheated :-)

internal static IList<Customer> LoadCustomersOrdersAndOrderLines()

{

    Customer[] customers = Customer.FindAll();

    Order[] orders = Order.FindAll();

    OrderLine[] orderLines = OrderLine.FindAll();

    foreach (Customer customer in customers)

    {

        customer.Orders = new List<Order>();//avoid lazy load when adding

    }

 

    foreach (Order order in orders)

    {

        order.OrderLines = new List<OrderLine>();//avoid lazy load when adding

        order.Customer.Orders.Add(order);

    }

 

    foreach (OrderLine line in orderLines)

    {

        line.Order.OrderLines.Add(line);

    }

    return customers;

}

 

Comments

Casey
06/25/2007 04:53 PM by
Casey

OK, I'm a little confused ... why was this so hard?

If the problem was basically to be expressed as "when you try and access the Orders for the Customer you will force a lazy load of the Orders" ... then I'm sure there are many solutions to the problem in many OR mappers ...

Not having looked at NPersist since Mats stopped work on it a few years ago, I can't understand why it cannot achieve the same result ...

Patrik L&#246;wendahl
06/25/2007 05:24 PM by
Patrik Löwendahl

"Not having looked at NPersist since Mats stopped work on it a few years ago"

That is really not true. I know for a fact that he and Roger Johansson has made numerous changes in NP the last couple of years.

Mats Helander
06/25/2007 05:35 PM by
Mats Helander

@ Ayende

LOL, well yes, I should have stated that the mapper should take care of resolving the references, not the client code! ;-)

But fair's fair! I'll add your entry to the list as it does fulfill the requirements in a very nice way, that I think certainly serves as a very useful, practical example of how to go about solving this yourself when the mapper won't do it for you! Well done, thanks for the contribution! :-)

However, I still contend that /NHibernate/ doesn't actually meet the challenge!

Ayende Rahien
06/25/2007 05:35 PM by
Ayende Rahien

Casey,

Mats challenge was to be able to do this with just

SELECT * FROM Customers

SELECT * FROM Orders

SELECT * FROM OrderLines

It is not hard, it is just something that most OR/Ms do with joins, for reasons that are detialed in Mats' previous post.

Casey
06/25/2007 05:35 PM by
Casey

Mats had some personal problems a while back and let those take priority. I know he has worked on it since, and I hope he has his personal life sorted out now ... his stuff is always great to read ... http://www.npersist.com/Forum/ShowThread.aspx?ThreadID=96

Casey
06/25/2007 05:40 PM by
Casey

Ahhhhh ... now I see why it was a challenge ... with Mats clarification it wasn't to happen in client code, only within the ORM or the DB.

From the blogs Oren posted recently, it isn't surprising that he chose to resolve the problem in the client code (it is after all a business problem, not a persistence problem) - it seemed so logical to resolve the issue there, I didn't see that it was the generated SQL he wanted to see simplified :)

Mats Helander
06/25/2007 05:47 PM by
Mats Helander

@ Casey, Patrik

Indeed we're still working on NPersist ! I'm developing new features for it all the time, trying to stabilize old ones and actualy documenting (!) a thing or two (check the new "Inside NPersist", free download, on the website...http://www.puzzleframework.com).

However, as you say, a few years ago I did realize that I'm just not able to reliably offer support over the forums to the extent that most people would actually need, and indeed sometimes there may be six months when I just don't have enugh free time to develop NPersist at all. I'd say that info is probably more relevant to your desicions about whether to use NPersist than what new features it may have gotten since last you looked?

@ Ayende, sorry for the link mongering in your blog. Tried to keep it relevant to my response.

/Mats

GLM
07/03/2007 03:37 AM by
GLM

I really like this one.

Today at an hispanic hibernate forum which I periodically post humble opinions we were trying to figure out how we could load a tree object graph with n-level depth.

We found no answer tweaking map files or nhib cache management :(

I still guess Ayendes' cheat will do it.

Comments have been closed on this topic.