开发者

Chain of events / Proxy to original object

I have a class which is inherited from context bound object. Class has attribute on some properties. When some property is changed, PostProcess(IMessage msg, IMessage msgReturn) raise an event and from the event again a new property with same attribute is fired. Second change should also call PostProcess, but it is not happening. Probably because, the object where second property is changed is not original .net object but MarshalByRefObject / ContextBoundObject / Proxy Object. My query is how to cast the proxy to original object. I tried casting and SynchonizationAttribute, but it does not help. Just to let you know the events are executing in Async manner so it does not block code execution, and both proxy and original object exist in same app domain.

I tried with two object, one holding reference of second, and when property of first is changed, it try to change property of second, but it did not invoke PostProcess.

Basically I need to make a tree where various objects are depending on property of other objects. And when any one property is changed, it should trigger its watcher, and this could spread like a chain untill no watcher is found. I am trying it with ContextBoundObject.

Sample:

public class PowerSwitch : ObjectBase
{
    [Watchable]
    public bool IsOn { get;开发者_StackOverflow社区 set; }
    public bool IsWorking { get; set; }
}

public class Bulb : ObjectBase
{
    public Color Color { get; set; }
    [Watchable]
    public bool IsOn { get; set; }
    protected override void Update(object sender, PropertyChangeEventArgs e)
    {
        ((Bulb)this).IsOn = !((Bulb)this).IsOn; 
          //<-- b1.update should be called after this, but it is not
    }
}

[Watchable]
public class ObjectBase : ContextBoundObject
{
     public virtual void Watch(ObjectBase watch, string propertyName)
     {
         watch.Watcher.Add(this, propertyName);
     }

     protected virtual void Update(object sender, 
                            PropertyChangeEventArgs e) { }

     public Dictionary<ObjectBase, string> Watcher
                      = new Dictionary<ObjectBase, string>();

     internal void NotifyWatcher(
             PropertyChangeEventArgs propertyChangeEventArgs)
     {
          Watcher.Where(sk => sk.Value == propertyChangeEventArgs.Name)
                        .ToList()
                        .ForEach((item) =>
                        {
                            item.Key.Update(this, propertyChangeEventArgs);

                            });
          }
    }

Main method

        PowerSwitch s1 = new PowerSwitch();
        Bulb b1 = new Bulb();
        b1.Watch(s1, "IsOn");
        s1.IsOn = true; //<-- b1.update is called after this

Please suggest alternate or better way to implement what I want to achieve.


It looks like you're very close to the observer pattern, where Observers subscribe to status notifications on a Subject.

In that pattern, b1.IsOn = true would contain code that cycles through the Observers and notifies them of changes. I guess if you wanted to observe a lot of properties on one object, you could encapsulate the notification code. Then b1.IsOn could just have a single line that says something like:

this.SendNotification("IsOn", Value);

This seems a lot like what you're doing... If you read up on the pattern you might end up feeling a little more confident, and maybe you could make a few changes to standardize your code.

By the way, there is something built into .Net for this -- IObservable(T). I haven't used this, but it looks strong.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜