0
votes

In my code I have an three main entities: 1. Company 2. Staff 3. Position

A staff can have several positions in various companies.

I want to retrieve all staff associated to a specific company.

In code I would do something like:

public partial class Company
{
    public virtual IEnumerable<Position> Positions { get; protected set; }
    public virtual IEnumerable<Staff> Staffs
    {
        get { return Positions.Select(x => x.Staff); }
    }
}

class CompanyMap : ClassMap<Company>
{
    public CompanyMap()
    {
        Id(x => x.Id)
            .Column("CompanyId")
            .GeneratedBy.Identity();
        Map(x => x.Name)
            .Not.Nullable();

        HasMany(x => x.Positions)
            .KeyColumn("CompanyId")
            .AsBag();
    }
}

Pb: In this solutionm I will load all positions associated to a company and then all staff associated to each position... In terms of performance it's not very good i guess...

I'm pretty sure there is a better way to perform this join directly in the CompanyMap class.

Could you help me to do that?

Thank you, Sebastien

1

1 Answers

0
votes

How about altering data model ? It seems that Staff is always in a given Position collection at the given Company level. This suggest following model

public partial class Staff
{
   public virtual IEnumerable<CompanyPosition> Positions { get; protected set; }
}

public class Position
{
  //...
}

public class Company
{
 //...
}

public class CompanyPosition
{
  public virtual Company Company   {get;set;}
  public virtual IEnumerable<Position> {get;set;}
}

With this you can do straightforward mapping and your query ends up similar to this

var data = session.Query<Staff>().Where(s=>s.CompanyPosition.Company == company);