开发者

how to make c# method more functional using C# and Linq

开发者 https://www.devze.com 2022-12-08 08:35 出处:网络
private void DoSomeWork() { ManualResetEvent[] waitEvents = new ManualResetEvent[rowCount]; int i = 0; foreach (DataRow row in StoredProcedureParameters.Tables[0].Rows)
private void DoSomeWork()
{
    ManualResetEvent[] waitEvents = new ManualResetEvent[rowCount];

    int i = 0;

    foreach (DataRow row in StoredProcedureParameters.Tables[0].Rows)
    {
        waitEvents[i] = new ManualResetEvent(false);
        var workerClass = new WorkerClass();

        var colle开发者_如何学Pythonction = new Dictionary<string, object>();
        foreach (DataColumn dataColumn in StoredProcedureParameters.Tables[0].Columns)
        {
            collection[dataColumn.ColumnName] = row[dataColumn.ColumnName];
        }

        workerClass.Parameters = collection;
        workerClass.Event = waitEvents[i];
        i++;

        ThreadPool.QueueUserWorkItem(WorkerFunction, workerClass);
    }
}

private class WorkerClass
    {
        public ManualResetEvent Event;
        public Dictionary<string, object> Parameters;
    }


Well, a few improvements:

private void DoSomeWork()
{
    DataTable table = StoredProcedureParameters.Tables[0];
    var columns = table.Columns.Select(x => x.ColumnName).ToList();

    var workers = table.Rows.Select(row => new WorkerClass
               (new ManualResetEvent(false), 
                columns.ToDictionary(column => row[column]));

    foreach (WorkerClass worker in workers)
    {
        worker.Start();
    }
}

...

// This is now immutable, and contains the work that needs to be done for the
// data it contains. (Only "shallow immutability" admittedly.)
class WorkerClass
{
    private readonly ManualResetEvent resetEvent;
    private readonly Dictionary<string, object> parameters;

    public WorkerClass(ManualResetEvent resetEvent, 
                       Dictionary<string, object> parameters)
    {
        this.resetEvent = resetEvent;
        this.parameters = parameters;
    }

    public void Start()
    {
        ThreadPool.QueueUserWorkItem(WorkerFunction);
    }

    private void WorkerFunction(object ignored)
    {
        // Whatever
    }
}


Skeet contribution is sweet but I'd already began to wrote my version:


ManualResetEvent[] waitEvents = 
  Array.ConvertAll(
      StoredProcedureParameters.Tables[0].Rows.OfType<DataRow>().ToArray(),
      (row) =>
      {
          ManualResetEvent mre = new ManualResetEvent(false);
          ThreadPool.QueueUserWorkItem(
              WorkerFunction, 
              new WorkerClass()
              {
                  Event = mre,
                  Parameters = row.Table.Columns.OfType<DataColumn>()
                      .ToDictionary(
                          column => column.ColumnName, 
                          column => (object)null)
              });
          return mre;
      });
0

精彩评论

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