-->
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: Factory class with NHibernate persistence
PostPosted: Mon Feb 19, 2007 12:02 pm 
Beginner
Beginner

Joined: Thu Aug 17, 2006 3:20 pm
Posts: 21
This is a fairly simple question, just can't seem to wrap my head around the answer.

I am using NHibernate 1.2 from the trunk to leverage generics support.

I have a User Table that has 4 different types of users:
Patron
Promoter
Admin

I am using generics in a Reospitory class to wrap NHibernate logic within a Repository assembly. Upon login, I need to load the relevant object depending on the usertype which is an integer column within SQL, i.e. (Patron = UserType 2, Promoter = UserType 3, etc..)

I am having a hard time picturing how this would fit into my repository at the time of selecting the entity from the backend and then returning to my domain layer.

I am thinking maybe creating an abstract UserRepository class, that would be where my factory code would be located and defer instantiation to concrete PatronRepository, PromoterRepository etc.. classes.

Any help/guidance would be greatly appreciated! Thank you!


Top
 Profile  
 
 Post subject: User types
PostPosted: Tue Feb 20, 2007 7:15 pm 
Beginner
Beginner

Joined: Wed Nov 29, 2006 5:33 pm
Posts: 28
Location: Chicago, IL
I'm not sure I understand the exact process that you're going through here of the end goal you are looking for, but I'll try to lay out a few scenarios

In our system, we have three types of users that all inherit from a base user class. The different user objects behave a bit differently in a few situations. We just call...
Code:
session.Get(GetType(UserBase), id)

This instatiates a user of type User, Admin, or Application according to the type of the user in the database, but returns the instance cast as UserBase. Our entire application only has references of type UserBase. The application doesn't care what type of user it is so long as the correct implementation is called when user.CanEdit(document) is invoked. In those rare situation we really really need to know what type the user is, a trycast or typeof work wonders. If I understand why you're asking this question, this is probably the best solution.

The problem with just about any other solution is that your code throughout the application needs to be sensitive to the fact that your repository(ies) might be returning objects cast to different types. This can be difficult to maintain as time goes on, but in case I didn't understand the end goal here's a couple more suggestions.

If I wanted to receive a user from the database typed as Admin, I would probably write a new AdminRepository and add a get call similar to above but with type Admin. Then in the application I would need to change all my code in the places where I specifically want to deal with Admins.

Another method that comes to mind would be to write a generic method into the repository class that asks for the type that you want returned along the same lines as the IQuery.List<of T>() method in the NHibernate framework. The call would then be something like
Code:
Dim user As Admin = UserRepository.ReadUser(Of Admin)(32);

Again, because both the variable and the method call contain a specific type, you need to make this sort of call anywhere you need an Admin typed user.

That's all I got for ya at the moment. Good luck. Let me know if you have any other questions.

_________________
Chuck

Not in the face! Not in the face!


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.