PostSharp - 自动事件订阅和集合添加

发布于 2024-07-23 10:07:14 字数 1716 浏览 7 评论 0原文

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

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

我有以下接口:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嗫嚅 2024-07-30 10:07:14

我将使用以下模式:

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文