Latest Replies
Thursday
Feb282008

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.

« How to avoid tight IoC coupling in non-deterministic resolution scenarios? | Main | xLim 2 Engine concept or simple continuous integration for businesses »

Reader Comments

There are no comments for this journal entry. To create a new comment, use the form below.

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>