IService接口,用于调用服务。每个方法一个代理或全局类级别代理
我正在从 Silverlight 应用程序 (MVVM) 和 Windows Phone 使用 WCF 服务。我有一个 Service 类(自动生成)和一个 IServiceRepository 如下所示
public interface IServiceRepository
{
event EventHandler<SomeEventArgs> GetDataCompleted;
void Data GetData();
// 10 more methods for fetching different data.
}
我的 SerViceRepository 如下所示
public class ServiceRepository : IServiceRepository
{
public event EventHandler<SomeEventArgs> GetDataCompleted;
public void Data GetData()
{
var proxy = new ActualServiceRefClient();
proxy.GetDataCompleted += PrivateGetDataCompleted;
proy.GetDatAsync();
}
private void PrivateGetDataCompleted(object s, SomeEventArgs e)
{
// Error check and all
if(GetDataCompleted != null)
GetDataCompleted(this, new SomeEventArgs(...));
}
}
我从 ViewModel 中调用此方法。现在我的问题是......
- 现在我正在创建代理 类和附加事件处理程序 在每个方法中都有它。我应该做什么 它在构造函数中 服务存储库?正如我所说,我有 大约有10到12种服务方法 称呼。
- 我应该在已完成的方法中取消注册事件处理程序吗?
I am consuming WCF services from a Silverlight application (MVVM) and windows phone. I have a Service class (Auto-generated) and one IServiceRepository looks like the following
public interface IServiceRepository
{
event EventHandler<SomeEventArgs> GetDataCompleted;
void Data GetData();
// 10 more methods for fetching different data.
}
My SerViceRepository looks like the following
public class ServiceRepository : IServiceRepository
{
public event EventHandler<SomeEventArgs> GetDataCompleted;
public void Data GetData()
{
var proxy = new ActualServiceRefClient();
proxy.GetDataCompleted += PrivateGetDataCompleted;
proy.GetDatAsync();
}
private void PrivateGetDataCompleted(object s, SomeEventArgs e)
{
// Error check and all
if(GetDataCompleted != null)
GetDataCompleted(this, new SomeEventArgs(...));
}
}
I am calling this methods from my ViewModels. Now my questions are ...
- Right now I am creating the proxy
class and attaching event handler
with it in every method. Should I do
it in the constructor of
ServiceRepository? As I said I have
around 10 to 12 service methods to
call. - Should I unregister the event handler in the completed method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定它去哪里重要。如果您只有一个正在调用的实际服务方法,则将事件连接放在构造函数中是有意义的。如果服务存储库中的 10 个数据方法中的每一个都有一个实际的服务方法,那么将它们分别连接到 10-12 个单独的方法中会更有意义。
这要看情况。如果您保留服务存储库的实例,并对其进行多次调用,那么您应该将事件连接移至构造函数,或者确保不在每次调用时重新连接事件处理程序。或者,您可以按照您所说的那样取消注册,但我认为最好在对象的生命周期内注册这些事件处理程序一次。如果您只是为每个罐头创建服务存储库的新实例,则无需取消注册事件处理程序。
希望这有帮助!
I'm not sure it matters where it goes. If you have just one one actual service method you are calling, putting the event wiring in the constructor makes sense. If there is one actual service method for each of the 10 data methods in your service repository, then it makes more sense to have them wired up in each of the 10-12 individual methods.
It depends. If you are keeping an instance of the Service Repository around, making several calls to it, then you should either move the event wiring to the constructor, or make sure you don't re-wire the event handler on each call. Alternatively, you CAN unregister as you said, but I think it's best to register those event handlers once for the lifetime of the object. If you are simply creating a new instance of your Service Repository for each can, there is no need to unregister the event handlers.
Hope this helps!