0

I'm currently working on and ASP.NET MVC application in which I have a User entity like follows:

public class User
{
    public virtual int Id { get; protected set; }
    public virtual string Name { get; protected set; }
    public virtual string Role { get; protected set; }
    public virtual Location Location { get; protected set; }
}

Where location is just as straightforward:

public class Location
{
    public virtual string Id { get; protected set; }
    public virtual string Building { get; protected set; }
    public virtual string City { get; protected set; }
    public virtual string Region { get; protected set; }
}

My complication arises because I want to populate the User from Active Directory and not the database. Additionally, several classes persisted to the database reference a user as a property. I've got an ADUserRepository for retrieval, but I don't know how to integrate these Users into my object graph when the rest is managed by NHibernate.

Is there a way for NHibernate to persist just an id for a User without it being a foreign key to a Users table? Can I map it as a component to accomplish this? I've also looked at implementing IUserType to make the translation. That way it would map to a simple field and ADUserRepository could be put in the chain to resolve the stored Id. Or am I trying to hack something that's not really feasible? This is my first time around with NHibernate so I appreciate any insight or solutions you can give. Thanks.

Update

It appears my best solution on this will be to map the User with an IUserType and inject (preferably with StructureMap) a service for populating the object before its returned. Framed in that light there are a couple of questions here that deal with the topic mostly suggesting the need for a custom ByteCodeProvider. Will I still need to do this in order for IUserType to take a parameterized constructor or do the comments here: NHibernate.ByteCode.LinFu.dll For NHibernate 3.2 make a difference?

Community
  • 1
  • 1
  • If user entity has a corresponding table with information being hydrated from active directory for certain properties (i.e. unmapped), this definitely possible via NHibernate's EventListeners framework {Ns : NHibernate.Event} or IInterceptor {Ns : NHibernate}. – kalki Jun 08 '12 at 06:18
  • As of now users do not have their own table. I did experiment with this approach, implementing a PostLoadEventListener, but it would require logic for each object that references a User. I'd rather hide how a user is returned from those entities. – user1325699 Jun 11 '12 at 20:21
  • you would not need to provided you use an interface to identify the entity with User as a member. IUserType is definitely an option my qualms about it were always the explicit specification needed in the hbm's (not well versed with fluent so no idea about that), i suppose each to his poison ! – kalki Jun 12 '12 at 09:59

1 Answers1

0

using a Usertype to convert user to id and back

public class SomeClass
{
    public virtual string Id { get; protected set; }
    public virtual User User { get; protected set; }
}

// in FluentMapping (you have to translate if you want to use mapping by code)

public SomeClassMap()
{
    Map(x => x.User).Column("user_id").CustomType<UserType>();
}

public class UserType : IUserType
{
    void NullSafeSet(...)
    {
        NHibernateUtil.Int32.NullSafeSet(cmd, ((User)value).Id, index);
    }
    void NullSafeGet(...)
    {
        int id = (int)NHibernateUtil.Int32.NullSafeGet(cmd, ((User)value).Id, index);
        var userrepository = GetItFromSomeWhere();
        return userrepository.FindById(id);
    }
}
Firo
  • 30,626
  • 4
  • 55
  • 94