1

我想使用 PostSharp 解决的一个重复的例行任务是事件订阅和集合添加。我想为每个子对象的事件订阅父对象过程(子对象包含在列表中)。我还想将父级的所有列表添加到父级的主列表中。我应该使用什么方面或者我应该考虑哪个方向?

下面列出了上述问题的示例...

我有以下界面:

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

具有以下状态类型:

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

上面的结构和实现是:

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;}
}
4

1 回答 1

0

我会使用以下模式:

  1. 使用自定义集合(例如从 Collection 继承)。覆盖 InsertItem 并从那里订阅子事件。集合的构造函数将父对象作为参数。所以这里不需要 PostSharp。

  2. 您可以使用 PostSharp 自动实现一些事件,例如 PropertyChanged (INotifyPropertyChanged)。有关详细信息,请参阅示例 PostSharp.Samples.Binding。

于 2009-06-02T15:51:08.047 回答