Just curious if this is a reasonable idea. I wouldn't do it all the time. The example I'm in now is that of the data context and repository that uses it.
I kind of like this because, compared to calling new in AssignButtonClicked, it gives my code the same quality of feeling independent of resource allocation as when using constructor injection (which I can't do for short lived resources).
void AssignButtonClicked(object sender, EventArgs e)
{
WithPaymentBatchRepository((context, repository) =>
{
foreach (var itemIDs in this.SelectedItemIdsAsInts)
{
repository.SetPaymentBatchId(itemIDs, this.SelectedPaymentBatchId);
}
context.SaveChanges();
});
Refill();
}
static void WithPaymentBatchRepository(Action<EomTool.Domain.Entities.EomEntities, EomTool.Domain.Concrete.PaymentBatchRepository> action)
{
using (var context = new EomTool.Domain.Entities.EomEntities(EomAppCommon.EomAppSettings.ConnStr, false))
{
var repository = new EomTool.Domain.Concrete.PaymentBatchRepository(context);
action(context, repository);
}
}
WithPaymentBatchRepositorygoing to be called elsewhere or only fromAssignButtonClicked? Or there gonna be other helper methods of the same style for other user actions? – Max Yakimets Jan 18 at 11:24