views:

32

answers:

1

Hi, I am new to all the OOP and ORM stuff, so i would appreciate your help...

I have a Team Class:

public class Team : IEntity<Team>
{
    public virtual int ID { get; private set; }

    public virtual string Code{ get; private set; }

    public virtual TeamWorker TeamLeader { get; private set; }

    public virtual IEnumerable<TeamWorker> Workers {get; private set;}
    //etc

}

A Worker Class:

public class Worker: IEntity<Worker>
{
    public virtual int ID { get; private set; }

    public virtual string Name { get; set; }
    //etc
}

A Team Worker Class, that "glues" a Worker to a percentage of the total value that will be given to a Team:

   public class TeamWorker
   {
        public virtual Worker Worker{ get; private set; }
        public virtual Percentage Comission{ get; private set; }
   }

And the Percentage class from wich i just need the public decimal Value property

So far i was able to map the Leader in the Team:

public class TeamMap : ClassMap<Team>
{

    public TeamMap()
    {
        Table("Team");

        Id(e => e.ID, "ID").GeneratedBy.Identity();

        Map(e => e.Code, "Code").Unique().Not.Nullable();

        Component(team => team.Leader,
                  m =>
                  {
                      m.References(teamWorker => teamWorker.Worker, "IDTeamWorker").Cascade.SaveUpdate();
                      m.Component(teamWorker  => teamWorker.Commission,
                                  p =>
                                  p.Map(commission=> commission.Value, "LeaderCommission").
                                      CustomType(typeof(decimal)).Nullable());
                  }
            );

  }

}

this maps the Leader and his commision in the team table, the problem i'm having is mapping the Workers...

i tried this:

    HasMany(team => team.Workers).Table("TeamWorkers").Component(m =>
                                            {
                                                m.References(twk => twk.Worker).Cascade.SaveUpdate();
                                                m.Map(twm => twk.Commission, "Commission");
                                            }
            );

But then i got this: Could not determine type for: Percentage

this happens because Commission is a Percentage and i didn't map it because its a value object, if only i could do this:

 HasMany(team => team.Workers).Table("TeamWorkers").Component(m =>
                                            {
                                                m.References(twk => twk.Worker).Cascade.SaveUpdate();
                                                m.Component(twk => twk.Commission,
                                      m2 => m2.Map(commission=> commission.Value, "Comission").CustomType(typeof(decimal)).Nullable());
                                            }
                );

but Component its not an option in this context...

i need to find a way to have

TeamWorkers table with TeamId WorkerId and Commission

TeamID and WorkerID are foreign keys from Team and Worker table, and Commission is the value property in Percentage class.

A: 

Don't use Component, that's for value types and dealing with normalized tables. You need to be creating proper mappings for your Worker and TeamWorker, and treat them as proper entities.

Create a WorkerMap and TeamWorker map, then you can update your TeamMap to just use HasMany(x => x.Workers).

As for Percentage, you either need to map that with it's own ClassMap, or use Component inside of your new TeamWorker map.

James Gregory
"You need to be creating proper mappings for your Worker and TeamWorker, and treat them as proper entities" yes, we were treating worker as proper entity, but not TeamWorker, that was the problem. Thank you very much!!
Carlos Decas
I am exposing directly the Workers IList in Team, if i wanted to use the demeter law, how could i map this Wrokers List with fluent nhibernate? (the same problem as this: http://weblogs.asp.net/aaguiar/archive/2004/07/02/172100.aspx)
Carlos Decas