These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: NHibernate and dictionaries
PostPosted: Fri Oct 17, 2008 9:24 am 
Beginner
Beginner

Joined: Wed Aug 01, 2007 4:28 pm
Posts: 23
Not sure if NH will do this but I thought I'd run it by you all. I'm using the 1.2 version w/ sql server.

I have a class called Spec (for specification) and a class called SpecValue that holds the specification values. Each SpecValue has a many-to-one to its Spec. I have another class called Floorplan. I would like the floorplan to have a property thats a generic IDicionary of SpecValues. But i'd like the key to be that SpecValue's Spec.Name which is a string. So I could do things like this:

var specName = someSpec.Name;
myFloorplan.SpecValues[specName].Value;

//these should be equal
myFloorplan.SpecValue[specName].Spec.Name == specName;

In the database there would just be a table called, floorplan, then one called Spec with name and ID, and finally SpecValue with ID, Value and foreign key SpecID, foreign key FloorplanID.

I don't really care about adding the specvalues to the dictionary to be saved, but when I get them out of the database, it would be nice if i could look the values up in floorplan using the Spec names.

Is this possible in NH, or am I getting too complicated?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 17, 2008 2:31 pm 
Newbie

Joined: Mon May 05, 2008 10:34 am
Posts: 11
Location: Houston, TX
I don't believe it supports this out of the box. What I would probably do is have something like this

class CustomerDAO
public IList<Customer> FindAll()


class CustomerMapping
public IDictionary<string, Customer> MapToDictionary(IList<Customer> customers)

Then here you can just loop through it. Not too bad, you can always cache it in a Session or Application object. Another option is just to use the List.Find method if you are looking for one object it should be faster actually.

Here's an example, you would use Find instead of Exists but the logic is the same:

if (!((List<AppSection>)Navigation).Exists(
appSection => Server.MapPath(pathToken + appSection.RelativePath) == requestedFilePath))
{
throw new AuthorizationFailure(_user, requestedUrl, "User does not have access to requested page.");
}

Notice I'm casting my IList to a List so that I can use the Exists method, you would do the same for the Find method.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.