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.
Don’t use
Component, that’s for value types and dealing with normalized tables. You need to be creating proper mappings for yourWorkerandTeamWorker, and treat them as proper entities.Create a
WorkerMapandTeamWorkermap, then you can update yourTeamMapto just useHasMany(x => x.Workers).As for
Percentage, you either need to map that with it’s ownClassMap, or useComponentinside of your newTeamWorkermap.