WorkInProgress
WorkInProgress

Reputation: 393

Multiple Entity Mapping (Not using IDicitionary)--FluentNHibernate

Here are my class structures

public class Group
{
   public virtual int GroupId {get;set;}

   public virtual IDictionary<Resource, Permission> ResourcePermissions { get; set; }
}

public class Resource
{
   public virtual int ResourceId {get;set;}
}

public class Permission
{
   public virtual int PermissionId {get;set;}
}

Here is my default override class for group

public class GroupMappingOverride : IAutoMappingOverride<Group>
{
    public void Override(AutoMapping<Group> mapping)
    {
        mapping.HasManyToMany(x => x.ResourcePermissions)
        .Table("GroupResourcePermission")
        .AsEntityMap("ResourceID", "PermissionID");


    }

}

When I execute,it is creating a table GroupResourcePermission with columns [GroupID,ResourceID,PermissionID] with (GroupID and ResourceID) as combined primary key.

For my purpose, it won't work. I want to store data as [1,1,1], [1,1,2] but the current structure doesn't allow me to store in the format. I found somebody was recommending

  IList<Tuple<Resource,Permission>> 

but couldn't made it work.

Experts, please provide suggestions.

Upvotes: 1

Views: 55

Answers (1)

Radim K&#246;hler
Radim K&#246;hler

Reputation: 123901

I would personally/strongly suggest, do not use mappings like IDictionary<T, V> and many-to-many. While this would be possible, you would later have issues how to query such relations...

I would introduce the pairing object

public class Setting
{
   public virtual int SettingId {get;set;}
   public virtual Group Group {get;set;}
   public virtual Resource Resource {get;set;}
   public virtual Permission Permission {get;set;}
}

And Group would be:

public class Group
{
   public virtual int GroupId {get;set;}
   public virtual IList<Settings> Settomgs { get; set; }
}

Mapping of the Group's collection of settings

HasMany(x => x.Settings)
    .Table("GroupResourcePermission")
    .KeyColumn("GroupID")
    .Cascade.AllDeleteOrphan()
    .Inverse()
    ;

And Setting mapping would be like

public class SettingMap : ClassMap<Setting>
{
    public SettingMap()
    {
        References(x => x.Group);
        References(x => x.Resource);
        References(x => x.Permission);
    }
}

This kind of mapping will later support filtering of the Groups via any Resource or Permission of its IList<Setting> Settings

Please, also take a look at this Q & A:

Upvotes: 1

Related Questions