views:

90

answers:

1

A repeating routine task that I would like to solve using PostSharp is event subscription and collection addition. I would like to subscribe parent's object procedure to each child object's event (children are conatained in a List). I would also like to add all Lists from parent to a master List on the parent. What aspec shuld i be using or in which direction should i be thinking?

Example of the problem described above is listed below...

I have the following interface:

public interface ITraceable
{
    IList Children {get;set;}
    ChangeStatus Status {get;set;}
    bool IsTraceEnabled {get;set;}
    event EventHandler ChangeHandler
}

With the folowing status types:

public enum ChangeStatus
{
    New,
    Modified,
    Added,
    Deleted
}

The structure and implementation of the above is:

public class Entity : ITraceable
{
    public event EventHandler {get;set;}
    public IList Children {get;set;}
    public ChangeStatus Status {get;set;}
    public bool IsTraceEnabled {get;set;}

    public string Name {get;set;}
    public string Address {get;set;}
    public string Title {get;set;}
    public List<ChildEntity1> ChildEntities {get;set;}
    public List<ChildEntity2> ChildEntities {get;set;}

    public void SubscribeableSub(object sender, EventArgs e)
    {
        Console.WriteLine("Test");
    }
}

public class ChildEntity1 : ITraceable
{
    public event EventHandler {get;set;}
    public IList Children {get;set;}
    public ChangeStatus Status {get;set;}
    public bool IsTraceEnabled {get;set;}

    public string Name1 {get;set;}
    public string Address1 {get;set;}
    public string Title1 {get;set;}
}

public class ChildEntity2 : ITraceable
{
    public event EventHandler {get;set;}
    public IList Children {get;set;}
    public ChangeStatus Status {get;set;}
    public bool IsTraceEnabled {get;set;}

    public string Name2 {get;set;}
    public string Address2 {get;set;}
    public string Title2 {get;set;}
}

Thanks in advandce for any assistance

A: 

I would use the following pattern:

  1. Use a custom collection (for instance inherit from Collection). Override InsertItem and subscribe to child events from there. The constructor of the collection takes the parent as a parameter. So no need for PostSharp here.

  2. You can use PostSharp to automatically implement some events, for instance PropertyChanged (INotifyPropertyChanged). See the sample PostSharp.Samples.Binding for details.

-gael

Gael Fraiteur