How to link with custom entities?

Topics: Developer Forum, User Forum
Jul 4, 2007 at 9:03 AM
Hi Guys,

Just trying to get NHibernateProvider to work, but can't see how you can link a user to a custom enttity (ie if you have an object that belongs to a user, how do you map this in NHibernate) looking in the source all of the entities classes seem to be internal and so can't be referenced in my custom entity.

The sample provided is very simplistic and only shows how to hook in the .NET login controls.

Can someone please shed any light on how to go about this?

Many thanks

Jul 4, 2007 at 11:47 PM
Edited Jul 4, 2007 at 11:49 PM
The classes are not exposed by design. Rather than coupling to these internal classes the intent of the design was for you to use the equivalent classes exposed by the ASP.NET 2.0 System.Web.Security namespace. Namely, in the case of the User entity you would want to use MembershipUser. That way, you bind to the Provider model and not to the specific provider. Also, you may not want to be loading or manipulating everything that the internal class uses.

I recommend you make a smaller subset mapping using what you see done internally as an example and use it for your own purpose. That is, if you do need to have class level references in your code.

You can get to the user's information simply by getting an instance of the MembershipUser class for the logged in user by calling Membership.GetUser(User.Identity.Name). However, I don't know all the details of what you would like to do so I am simply speculating. But if all you need to do get to the details the MembershipUser class will give it to you.
Jul 5, 2007 at 7:34 AM
Hi lvildosola,

Would you recomend then, that in my custom entities i just keep a reference to the username and when i need that users details, just use Membership.GetUser?

Alternativley, i'm not sure how you would write a NHibernate map file for an entity that had a MembershipUser property.


Jul 5, 2007 at 11:36 AM
Yes, if you only need to get details about a user at runtime what you would do is keep a reference to the user key (in the example above it is the NT UserName). Then all you have to do is make the call to the Membership class to get the details. It will in turn call the Provider to do the work (in this case it will call the NHibernateProvider to fetch the information). That way you don't have to worry about the underlying details.

If you choose to create a mapping for the User entity you can do what I did (see the code and mapping for the User class). Essentially, what you want to keep in mind is that whatever you do you don't want to expose any details about the user that should not be exposed. If you look at the MembershipUser table you can decide what to expose and simply create a mapping to do just that.