views:

588

answers:

2

What is the best way to handle this I have an application where I constantly have to "pivot" 12 month records into one year record/object to be able to display a single year per row in a DataGrid. I have been doing this work up until this point in DataSets with some ugly T-SQL to get the pivot to happen.

I'm upgrading this application to NHibernate now and this is the one trouble-spot I have.

As an example, lets say I have a test that I give a group of students once a month. My DDL might look like:

CREATE TABLE [Score](
    [ScoreId] [int] IDENTITY(1,1) NOT NULL,
    [StudentId] [int] NOT NULL,
    [Year] [int] NOT NULL, -- 4 digit year as an int
    [Month] [int] NOT NULL, -- 1 through 12 month value as int
    [TestScore] [int] NOT NULL
)

I'd like to display in a DataGrid (one record per student/year) these fields:
Student Name, Year, Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec

What is the normal way to approach this kind of task with NHibernate? How would you handle it?

+1  A: 

In my opinion it's better to use NHibernate as a pure ORM and leave the pivoting to your code. This means using NHibernate to reading the row in the table as it is into a class with a one-to-one relationship with the table columns, and then using a method on the class (or a property with some logic in the getter) to do the pivoting.

In a pure DDD, you should use a service to do this (instead of putting this logic into the domain object).

After this, you can bind the results to a grid for presentation.

Khash
So if I'm understanding you correctly pull the parent object and the 12 child objects (months) and use something like a Service Layer to return back a more friendly set of "Facade" objects for binding?
tyndall
I realized after I asked this question ... what if I didn't have this month issue, but still had every "Measure" related to one Student object. Is there a easy way to Bind that to a DataGrid? Haven't done a parent/child or 1-to-1 Grid like this before. Been in that DataTable/DataSet world too long.
tyndall
The native data grid is not very flexible in that regard. I'd say if you want to do something like master/detail grids, go for a more complex data grid control like Infragistics or DevExpress.
Khash
I don't want to do Master/Detail per se. Does the dot syntax work? Can I instead of binding to a StudentId bind to the Student.DisplayName? I guess I can write a simple test project to try that out.
tyndall
A: 

I thought I would add this to the conversation.

This thread talks about overiding the ToString() of a class. I think I could do this for my MonthValue objects... just have them return the Score via ToString();

However, this doesn't solve the mapping issue. Per other comments on this question, the dot syntax didn't work for me in the DataPropertyName.

tyndall