Best practice for Handling NHibernate parent-child collections

后端 未结 7 975
逝去的感伤
逝去的感伤 2020-12-15 10:35

So in a typical model where you have a parent that can have many children and a child that can have only one parent, how do you manage the adding of children. I have been u

相关标签:
7条回答
  • 2020-12-15 11:01

    I don't like all the extra AddXXX() and RemoveXXX() methods cluttering my entity interfaces. Instead, I have a custom list that raises events when the Add() and Remove() methods are called.

    The linking then happens in the event handlers:

    public class Course()
    {
       public Course()
       {
         this.Topics = new EntityList<Topic>();
         this.Topics.AddItem += new AddItemEventHandler<Topic>(Topic_AddItem);
         this.Topics.RemoveItem += new RemoveItemEventHandler<Topic>(Topic_RemoveItem);
       }
    
       public EntityList<Topic> Topics { get; private set; }
    
       private void Topic_AddItem(Topic item, object args)
       {
         // Replace with your linking code:
         EntityLinker.Link(this).With(item, args);
       }   
    
       private void Topic_RemoveItem(Topic item, object args)
       {
         // Replace with your unlinking code:
         EntityLinker.Unlink(this).From(item, args);
       }   
    }
    
    0 讨论(0)
提交回复
热议问题