NHibernate Objects and Multiple Associations

I do not know how to correctly formulate the question, so sorry in advance.

Using FluentNHibernate in particular, how would you like your entities to be for some tables that many other tables refer to?

For example, an Employee object. Employees are typically used almost universally, and for some programs it would be wise for their employees to have multiple roles, multiple tasks, or multiple benefits. Right now I imagine how it looks like this:

public class Employee
{
    public virtual Employee()
    {
        Tasks = new List<Task>();
        Roles = new List<Role>();
        Benefits = new List<Benefit>();
    }

    public virtual Id { get; set; }
    public virtual Name { get; set; }

    public virtual IList<Task> Tasks { get; protected set; }
    public virtual IList<Role> Roles { get; protected set; }
    public virtual IList<Benefit> Benefits { get; protected set; }

    public virtual void AddTask(Task task)
    {
        task.Employee = this;
        Tasks.Add(task);
    }
    public virtual void AddBenefit(Benefit benefit)
    {
        benefit.Employee = this;
        Benefits.Add(benefit);
    }
    public virtual void AddBenefit(Benefit benefit)
    {
        benefit.Employee = this;
        Benefits.Add(benefit);
    }
}

public class EmployeeMapper : ClassMap<Employee>
{
    public EmployeeMapper()
    {
        id( x => x.Id );
        Map( x => x.Name );
        HasMany( x => x.Tasks );
        HasMany( x => x.Roles );
        HasMany( x => x.Benefits );
    }
}

3 , , , . , : , , , .. , , ?

, NHibernate, , , ?

+3
1

fk . , .

, NHibernate, .

, IF , ,

parent.children.Add(child); 
child.parent = parent;
+2

Source: https://habr.com/ru/post/1758934/


All Articles