views:

543

answers:

2

Hello,

I am fairly new to nHibernate and DDD, so please bear with me.

I have a requirement to create a new report from my SQL table. The report is read-only and will be bound to a GridView control in an ASP.NET application.

The report contains the following fields Style, Color, Size, LAQty, MTLQty, Status.

I have the entities for Style, Color and Size, which I use in other asp.net pages. I use them via repositories. I am not sure If should use the same entities for my report or not. If I use them, where I am supposed to map the Qty and Status fields?

If I should not use the same entities, should I create a new class for the report?

As said I am new to this and just trying to learn and code properly.

Thank you

+3  A: 

For reports its usually easier to use plain values or special DTO's. Of course you can query for the entity that references all the information, but to put it into the list (eg. using databinding) it's handier to have a single class that contains all the values plain.

To get more specific solutions as the few bellow you need to tell us a little about your domain model. How does the class model look like?

generally, you have at least three options to get "plain" values form the database using NHibernate.

Write HQL that returns an array of values

For instance:

select e1.Style, e1.Color, e1.Size, e2.LAQty, e2.MTLQty 
from entity1 inner join entity2
where (some condition)

the result will be a list of object[]. Every item in the list is a row, every item in the object[] is a column. This is quite like sql, but on a higher level (you describe the query on entity level) and is database independent.

Or you create a DTO (data transfer object) only to hold one row of the result:

select new ReportDto(e1.Style, e1.Color, e1.Size, e2.LAQty, e2.MTLQty)
from entity1 inner join entity2
where (some condition)

ReportDto need to implement a constructor that has all this arguments. The result is a list of ReportDto.

Or you use CriteriaAPI (recommended)

session.CreateCriteria(typeof(Entity1), "e1")
  .CreateCriteria(typeof(Entity2), "e2")
  .Add( /* some condition */ )
  .Add(Projections.Property("e1.Style", "Style"))
  .Add(Projections.Property("e1.Color", "Color"))
  .Add(Projections.Property("e1.Size", "Size"))
  .Add(Projections.Property("e2.LAQty", "LAQty"))
  .Add(Projections.Property("e2.MTLQty", "MTLQty"))
  .SetResultTransformer(AliasToBean(typeof(ReportDto)))
  .List<ReportDto>();

The ReportDto needs to have a proeprty with the name of each alias "Style", "Color" etc. The output is a list of ReportDto.

Stefan Steinegger
This may sound like a stupid question, but what do you mean with "How does the class model look like?"? What is the difference between an Entity and a DTO? Also I would use a repository to get my data, correct?Thanks so much for helping.
vikasde
Sorry, I assumed that you already do DDD. Domain Driven Development means that you create a class model first (the Domain Model), this are the entities (persisted objects). You map them using NHibernate to the database, and therefore define the database model according to your classes. It is NOT DDD to have a database before and define classes according to the database schema.
Stefan Steinegger
I am trying to do DDD, however I don't understand enough of it. Now based on my requirement I would create a simple class (InventoryReport) and create the necessary properties (Style, Color, Size, MtlQty, LAQty and Status), and then map them via nHibernate to my sql view, correct?
vikasde
This is not DDD. DDD you have the domain model (your class) BEFORE you have any database design (your view). If you already have a view which exactly returns the values you need, you probably don't need NHibernate. NHibernate is really powerful if you want to (almost) freely model you domain classes and be able to persist it without much troubles.
Stefan Steinegger
Ah. I guess its better to read more about DDD first. You say that I do not need to use nHibernate for this, however isn't it better to use it, just to keep things consistent?
vikasde
You use NHibernate already in the project? Then I would highly recommend to use it. Or what do you mean with "keep things consistent"?
Stefan Steinegger
Yes, I am already using nHibernate (which is what I meant with "keep things consistent"). What do you now recommend for my requirement? I need to create this report for our CEO. If I were to use ADO.NET to return a datatable, I would be already done, but I wanted to learn how I could do this using DDD.
vikasde
You can't do this using DDD. DDD is the way you develop the domain model. The model is already there, so you can't develop it. You could use NHibernate to use the existing infrastructure, configuration and mapping definitions. "for our CEO" sound like a "write once and throw away" implementation. For that it does not matter. If it is a part you need to maintain, you really should use NHibernate because of the maintainability.
Stefan Steinegger
Makes sense. Thank you so much for helping out.
vikasde
A: 

I'm not schooled in DDD exactly, but I've always modeled my nouns as types and I'm surprised the report itself is an entity. DDD or not, I wouldn't do that, rather I'd have my reports reflect the results of a query, in which quantity is presumably count(*) or sum(lineItem.quantity) and status is also calculated (perhaps, in the page).

You haven't described your domain, but there is a clue on those column headings that you may be doing a pivot over the data to create LAQty, MTLQty which you'll find hard to do in nHibernate as its designed for OLTP and does not even do UNION last I checked. That said, there is nothing wrong with abusing HQL (Hibernate Query Language) for doing lightweight reporting, as long as you understand you are abusing it.

I see Stefan has done a grand job of describing the syntax for that, so I'll stop there :-)

Simon Gibbs
I have a view that I would like to use for my report. The view returns only the required columns. (I am not sure if I were supposed to do it like that). What should I exactly describe about my domain? Sorry still learning about all this :(
vikasde