订阅WCF服务的DataContract代理类的PropertyChanged
我喜欢在客户端类上扩展的想法,这些类是使用部分类的 WCF 服务的数据契约。但我遇到了一个严重破坏聚会的问题。
想象一下,在服务器端我有一个类:
[DataContract]
public class SolidObject
{
[DataMember]
public Point Position { get; set; }
[DataMember]
public Size Size { get; set; }
}
在客户端我生成了一个代理类,该代理类在业务逻辑层中使用。根据业务逻辑的需要,我这样扩展它:
public partial class SolidObject
{
public Rect Bounds { get { return new Rect(Position.X - Size.Width / 2, Position.Y - Size.Height / 2, Size.Width, Size.Height); }}
}
现在我想确保只要 Position 或 Size 发生变化,就会调用 Bounds chage 事件。通过代码很容易做到:
PropertyChanged += (sender, e) =>
{
if ((e.PropertyName == "Position") || (e.PropertyName == "Size")) PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Bounds"));
};
问题是放置此代码的好位置在哪里。
如果对象不是由服务调用创建的,我会将其放入构造函数中。但 WCF 服务忽略客户端的构造函数,请参阅 构造函数没有出现在我的 WCF 客户端中,序列化问题?。
现在,在服务响应之后,我的程序立即搜索数据契约层次结构,获取所需的对象并添加事件处理程序。但我不认为这是正确的事情。
所以我很感兴趣的是哪里做得更好,或者也许应该改变整个方法。任何想法表示赞赏。
I like the idea of extending on the client side classes that are data contracts of WCF services using partial classes. But I encountered a problem that considerably spoils the party.
Imagine on the server side I have a class:
[DataContract]
public class SolidObject
{
[DataMember]
public Point Position { get; set; }
[DataMember]
public Size Size { get; set; }
}
On the client side I have a proxy class generated, which is used there in the business logic layer. According to the needs of the business logic I extend it this way:
public partial class SolidObject
{
public Rect Bounds { get { return new Rect(Position.X - Size.Width / 2, Position.Y - Size.Height / 2, Size.Width, Size.Height); }}
}
Now I want to ensure that anytime either Position or Size changes, then Bounds chage event is invoked. It is easy to do by the code:
PropertyChanged += (sender, e) =>
{
if ((e.PropertyName == "Position") || (e.PropertyName == "Size")) PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Bounds"));
};
The question is where the good place to put this code in is.
If objects weren't created by a service call, I'd put it into the constructor. But WCF services ignore constructors on the client side, see constructor not showing up in my WCF client, serialization problem?.
Now, right after service response my program searches through data contract hierarchy, gets desired objects and adds event handlers. But I don't think it's a right thing.
So I'm interesting in where to it is better to do or, maybe, reasoning that the whole approach should be changed. Any ideas appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 [OnDeserlialized] 属性插入一个在反序列化后调用的方法。
You could use the [OnDeserlialized] attribute to insert a method that gets called after deserizlization.
我建议使用视图模型而不是部分类。
I'd recommend using a view model rather than a partial class.
我想说,如果要实现业务逻辑,请不要使用生成的类。通过客户端实现 SolidObject 来保持模型干净。当您生成代理时,您可以使用重用此 dll 中的类选项。
对于一个实体来说,附加到它自己的事件是没有意义的。实现将是..
I would say do not use generated class if there is business logic to be implemented.Keep your model clean by having client side implementation of SolidObject. When you are generating proxies you can use option of reuse classes from this dll.
It doesnot make sense for an entity to attach to its own event.The implementation would be..