Search code examples
asp.netcastle-activerecord

Model collections of the same class held by several other classes


How do I model the following using Castle ActiveRecord?

I have two classes, Customer and Task.

I would like to reuse a third class, Note, stored in a Collection in each of the Customer and Task classes.

public class Note
{
    public int ID { get; set; }
    public string Subject { get; set; }
    public string Body { get; set; }
}

public class Customer
{ 
    public int ID { get; set; }
    public IList<Note> Notes { get; set; }
}

public class Task
{
    public int ID { get; set; }
    public IList<Note> Notes { get; set; }
}

I would then like to be able to pass the Notes collection to a Gridview, Listview or Repeater in the relevant ASP.Net page for the Customer or Task classes.


Solution

  • We settled on the following pattern:

    [ActiveRecord]
    public class Note
    {
        [PrimaryKey]
        public int ID { get; set; }
    
        [Property]
        public string Subject { get; set; }
    
        [Property]
        public string Body { get; set; }
    
        [BelongsTo]
        public Customer Customer { get; set; }
    
        [BelongsTo]
        public Customer Task{ get; set; }
    }
    
    [ActiveRecord]
    public class Customer
    { 
        [PrimaryKey]
        public int ID { get; set; }
    
        [HasMany]
        public IList<Note> Notes { get; set; }
    }
    
    [ActiveRecord]
    public class Task
    { 
        [PrimaryKey]
        public int ID { get; set; }
    
        [HasMany]
        public IList<Note> Notes { get; set; }
    }