C# 用于调用服务的IService接口。每个方法一个代理或全局类级代理

C# 用于调用服务的IService接口。每个方法一个代理或全局类级代理,c#,mvvm,event-handling,windows-phone-7,mvvm-light,C#,Mvvm,Event Handling,Windows Phone 7,Mvvm Light,我正在使用Silverlight应用程序(MVVM)和windows phone提供的WCF服务。我有一个服务类(自动生成)和一个IServiceRepository,如下所示 public interface IServiceRepository { event EventHandler<SomeEventArgs> GetDataCompleted; void Data GetData(); // 10 more methods for fetching differ

我正在使用Silverlight应用程序(MVVM)和windows phone提供的WCF服务。我有一个服务类(自动生成)和一个IServiceRepository,如下所示

public interface IServiceRepository
{
  event EventHandler<SomeEventArgs> GetDataCompleted;
  void Data GetData();
  // 10 more methods for fetching different data.
}
 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(...));
       }
    }
公共接口IServiceRepository
{
事件事件处理程序GetDataCompleted;
void Data GetData();
//还有10种获取不同数据的方法。
}
我的SerViceRepository如下所示

public interface IServiceRepository
{
  event EventHandler<SomeEventArgs> GetDataCompleted;
  void Data GetData();
  // 10 more methods for fetching different data.
}
 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(...));
       }
    }
公共类服务存储库:IServiceRepository
{
公共事件EventHandler GetDataCompleted;
公共void数据GetData()
{
var proxy=新的ActualServiceRefClient();
proxy.GetDataCompleted+=PrivateGetDataCompleted;
proy.GetDatAsync();
}
private void PrivateGetDataCompleted(对象s,SomeEventArgs e)
{
//错误检查和所有
如果(GetDataCompleted!=null)
GetDataCompleted(这是新的SomeEventArgs(…);
}
}
我从ViewModels调用此方法。现在我的问题是

  • 现在我正在创建代理 类和附加事件处理程序 在每种方法中都使用它。我该怎么办 它在 服务存储库?正如我所说,我有 大约10到12种服务方法 打电话
  • 我应该在已完成的方法中注销事件处理程序吗
  • 我不确定它去哪里重要。如果您只有一个实际调用的服务方法,那么将事件连接放在构造函数中是有意义的。如果您的服务存储库中10个数据方法中的每一个都有一个实际的服务方法,那么将它们连接到10-12个单独的方法中就更有意义了

  • 视情况而定。如果要保留服务存储库的一个实例,并对其进行多次调用,则应该将事件连接移动到构造函数,或者确保不要在每次调用时重新连接事件处理程序。或者,您可以如您所说的那样取消注册,但我认为最好在对象的生命周期内注册这些事件处理程序一次。如果只是为每个can创建服务存储库的新实例,则无需注销事件处理程序


  • 希望这有帮助

    问题是我有大约15个服务方法和15个事件要注册。我的每个视图模型都调用两个或三个服务方法。通常情况下,如果我将事件处理程序保留一段时间不会有问题,但在Windows phone 7中,最大峰值内存为90 Mb,我们担心内存问题。我想我会在每次需要时实例化服务存储库,并在每个方法中连接事件处理程序。让服务存储库超出范围,垃圾收集器将回收内存。谢谢。我想这是MVC和LINQDataContext的建议方式。虽然我找不到链接