PostSharp - 自动事件订阅和集合添加
我想使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我将使用以下模式:
使用自定义集合(例如从 Collection 继承)。 重写 InsertItem 并从那里订阅子事件。 集合的构造函数将父级作为参数。 所以这里不需要 PostSharp。
您可以使用PostSharp自动实现一些事件,例如PropertyChanged(INotifyPropertyChanged)。 有关详细信息,请参阅示例 PostSharp.Samples.Binding。
I would use the following pattern:
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.
You can use PostSharp to automatically implement some events, for instance PropertyChanged (INotifyPropertyChanged). See the sample PostSharp.Samples.Binding for details.