Why do not we resolve delegates in IoC?

Do you see the difference between these two declarations of some imaginary command?

public sealed class DeleteCommand : ICommand
{
  private readonly IRecordSelector _recordSelector;
  private readonly IMessageBoxService _messaging;
  private readonly IRecordManager _recordManager;

  public DeleteCommand(IRecordSelector recordSelector,
    IMessageBoxService messaging, IRecordManager recordManager)
  {
    _recordSelector = recordSelector;
    _messaging = messaging;
    _recordManager = recordManager;
  }

  public void Execute()
  {
    List<XPObject> records;
    if (_recordSelector.TryGetOneOrMoreRecords(out records))
    {
      if (_messaging.Ask("Do you really want to delete '{0}' records?",
        records.Count))
      {
        _recordManager.Remove(records);
        _recordSelector.RefreshData();
      }
    }
  }
}

and this one

Action<IRecordSelector, IMessageBoxService, IRecordManager> command =
  (selector, messaging, manager) =>
{
  List<XPObject> records;
  if (selector.TryGetOneOrMoreRecords(out records))
  {
    if (messaging.Ask("Do you really want to delete '{0}' records?",
      records.Count))
    {
      manager.Remove(records);
      selector.RefreshData();
    }
  }
};

Class version is longer but it can be resolved in IoC. The delegate is more concise but it does not resolve.

So, why do not we resolve delegates in IoCs?

PS: additional difference is that if command is stateless then it could be resolved only once in the IoC scope and the subsequent executions will use “cached” version (Container scope instead of Factory could be used). But I’d trade that for the DRY principle.

0 Responses to “Why do not we resolve delegates in IoC?”


  1. No Comments

Leave a Reply