andrepena wrote:
Hi all,
I'm just starting my .NET studies and I got 2 questions:
1) Let's consider a product class. A product has an instance of a supplier object. OK. But when I list products, I don't want to see a supplier object, I want to see the supplier name. Because I can't render an object in a DataGrid for instance. Do you get it? But then I get a problem, because my product class would have to be adapted to bear a supplier name instead of a supplier object. So I guess I would need to write 2 classes. One full-OO, and other completely scalar so that I can render it. How can I do that automatically?
2) Now consider I have 450 queries in a very large system. the "format" of these queries will vary a lot. I Mean, sometimes I'll get a product, but other times I'll get a product with other columns embeded, all depending on the particular need of a query. It's not yet completely clear to me, how Hibernate deals with differences in query results. What if I need a Customer with an additional column? Do I need to create another BO to bear it? Can I return a query in a loosily-typed object? how does that work?
thanks a lot
I think i can answer some of this:
1) I don't think you will want 2 seperate classes as this would cause a lot of duplicate code for these types of situations. I think what you want is to be able to do a join between the product objects and suppliers. In HQL (Hibernate's query language) you want do do a fetch join. What this means is that you query for the products, with their supplier's property already pre-populated with supplier objects (as opposed to lazy loading it or retrieving it seperately.) Your query would look something like this:
Select p
from Product p
left join fetch p.Supplier
This tells NHibernate to get all the products and also populate their supplier properties in one query.
see this documentation:
http://www.hibernate.org/hib_docs/refer ... ryhql.html
After that, you can simply traverse your object model by doing something like: Product.Supplier.SupplierName or in asp.net you can render it to a page like this:
<asp:repeater runat="server" id="prodRepeater">
<itemTemplate>
Product Name: <%# Eval("ProductName") %><BR>
Supplier Name: <%# Eval("Supplier.SupplierName") %>
</itemTemplate>
</asp:repeater>
This works for most cases, the only downside is that when you do a join fetch, you are retrieving ALL the suppliers columns, because you're fetching the entire supplier object, even though you just want the supplier's name. In most cases though, it doesn't matter. I think there are other approaches as well, as HQL allows you to pull back scalars.
2) The short answer is going to be "it depends". You can use approaches like the above for simple things. For non-simple things, then you'll have to research HQL more, say for aggregates and so forth so you can get a mix of objects and scalar values, or maybe just scalars. I haven't done very much of this yet, so i'm not the best person to answer it. But you should check out the link and any documentation on HQL. It will probably do most of the things you need.