简体   繁体   English

如何利用委托来提高代码重用性?

[英]How can I improve my code reuse by utilising delegates?

I am writing a class which utilizes a USING statement to connect to the task scheduler on a server and "does things"; 我正在编写一个类,该类利用USING语句连接到服务器上的任务调度程序并“执行操作”; like enable/disable a task, start and stop etc. I want to improve code reuse, perhaps by using delegates, but I'm not sure how to go about it. 例如启用/禁用任务,启动和停止等。我想通过使用委托来提高代码重用性,但是我不确定该如何处理。 Here's some of my current code: 这是我当前的一些代码:

private static void StopRunningTask(string taskName, string hostName)
{
   Regex regex = GetRegexForFindTask(taskName);
   using (TaskService ts = new TaskService(hostName))
   {
      foreach (Task t in ts.FindAllTasks(regex, true))
      {
         if (t.Name.Equals(taskName, StringComparison.OrdinalIgnoreCase) && t.State == TaskState.Running)
           t.Stop();
      }
   }

private static void RunTask(string taskName, string hostName)
{
   Regex regex = GetRegexForFindTask(taskName);
   using (TaskService ts = new TaskService(hostName))
   {
      foreach (Task t in ts.FindAllTasks(regex, true))
      {          
         if (t.Name.Equals(taskName, StringComparison.OrdinalIgnoreCase) && t.State == TaskState.Ready)
         t.Run();
      }
   }

private static void ChangeTaskEnabledState(string taskName, bool enabled, string hostName)
{
   Regex regex = GetRegexForFindTask(taskName);
   using (TaskService ts = new TaskService(hostName))
   {
      foreach (Task t in ts.FindAllTasks(regex, true))
      {
         if (t.Name.Equals(taskName, StringComparison.OrdinalIgnoreCase))
         t.Enabled = enabled;
      }
   }

private static Regex GetRegexForFindTask(string taskName)
{
   var regex = new Regex(string.Format(@"{0}", taskName), RegexOptions.IgnoreCase);
   return regex;
}

You can just provide a callback instead for the method - eg 您可以只为方法提供回调-例如

private static void FindTasks(string taskName, string hostName, Action<Task> callback)
{
    Regex regex = GetRegexForFindTask(taskName);

    using (TaskService ts = new TaskService(hostName))
    {
        foreach (Task t in ts.FindAllTasks(regex, true))
            if (t.Name.Equals(taskName, StringComparison.OrdinalIgnoreCase))
                callback(t);
    }
}

Then to call: 然后致电:

FindTask("SomeTaskName", "SomeHost", (task) => t.Enabled = true);

Obviously though your FindTask implementation looks like it differs slightly (you check for IsEnabled in some cases etc) so you could use something like this where you do that check in the anonymous delegate: 显然,尽管您的FindTask实现看起来略有不同(在某些情况下,您检查IsEnabled等),所以可以在匿名委托中执行以下操作:

FindTask("SomeTaskName", "SomeHost", (task) => 
    { 
        // Check if it is running first
        if(t.IsRunning) t.Enabled = false; 
    });

Or you could pass a predicate to the FindTask method to filter the list - let me just dig up the syntax.. 或者,您可以将谓词传递给FindTask方法以过滤列表-让我来研究一下语法。

Edit: would be something like: 编辑:将是这样的:

public static void FindTask(string taskName, string hostName, Expression<Func<T, bool>> filter, Action<Task> callback)
{
    Regex regex = GetRegexForFindTask(taskName);

    using (TaskService ts = new TaskService(hostName))
    {
        foreach (Task t in ts.FindAllTasks(regex, true).Where(filter)) // Use linq filter here to narrow down the list of tasks
            if (t.Name.Equals(taskName, StringComparison.OrdinalIgnoreCase))
                callback(t);
    }
}

And to use: 并使用:

FindTask("SomeTaskName", "SomeHost", task => task.IsEnabled, (task) => task.IsEnabled = false);

So it would only run the callback on IsEnabled tasks 因此,它将仅对IsEnabled任务运行回调

Obviously then you have to wonder what you can put into the predicate! 显然,您必须怀疑您可以将什么放入谓词中! The balance is yours to determine :) 余额由您确定:)

The only thing you need to think about though, is that allowing another user (3rd party maybe) to use this allows them to do whatever they want to the task since they get an instance of the task back to operate on. 不过,您唯一需要考虑的就是,允许另一个用户(可能是第三方)使用它,因为他们可以重新获得要执行的任务的实例,因此他们可以对任务执行任何操作。 At least with your original implementation, you are constrained to just the operations you specify. 至少在您的原始实现中,您只能使用指定的操作。 (This may not be a problem for you though if it's just for your own use/internal use) (尽管这仅供您自己/内部使用,对您来说可能不是问题)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM