Question

I am creating the security system for the software im creating and I would like to know how i can do the following in NHibernate(if at all possible)

User Account Class:

public class UserAccount
{
    public virtual int UserID { get; set; }
    public virtual String Username { get; set; }
    public virtual Privilege InvoicePrivilege { get; set; }
    public virtual Privilege TradePrivilege { get; set; }
}

Privilege Class:

public class Privilege
{
    public virtual bool Read { get; set; }
    public virtual bool Write { get; set; }
    public virtual bool Delete { get; set; }
    public virtual bool Modify { get; set; }
}

I will be having a large number of these privilege objects (one for each traded entity) so in the database i have the following tables:

UserAccounts ( UserID, Username)
Privileges (UserID, PrivilegeType, Read,Write,Modify,Delete)

How can I map the InvoicePrivielge property from the user account to the (UserID, 'Invoice') Privilege.

I could do this using many-to-one but I don't want a collection of privileges, i'd rather map it to its property.

Was it helpful?

Solution

I think a way to do this is to create two subclasses of the Privilege class:

public class InvoicePrivilege : Privilege 
{
}
public class TradePrivilege : Privilege
{
}

and set the discriminator column and value. In fluent mapping:

public class PrivilegeMap : ClassMap<Privilege>
{
  public PrivilegeMap()
  { 
     // ...
     DiscriminateSubClassesOnColumn("PrivilegeType")
       .SubClass<InvoicePrivilege>("Invoice", x => x.Map( ... ))
       .SubClass<TradePrivilege>("Trade", x => x.Map( ...));
  }
}

and use the subclasses in the UserAccount

public virtual InvoicePrivilege InvoicePrivilege { get; set; }
public virtual TradePrivilege TradePrivilege { get; set; }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top