I can reproduce it using Func<>
like so :
public SimilarSyntax<T>(Func<T> f)
{
Begin();
var result = f();
End();
return result;
}
However, this isn't quite the same because f
can't assign variables known by the caller.
I would like to be able to replicate using
syntax to clean up some code.
Actual code
void SomeProcedure(object param)
{
Class1 get1;
Class2 get2;
...
ClassN getN;
try
{
_context.SetReadUnCommitted();
get1 _context.Get1(param);
...
get2 _context.Get2(param);
}
finally
{
_context.PutBackOriginalIsolationLevel();
}
_context.Get3(param);
}
What I'm attempting to have (or other syntaxe like the one of using
)
void SomeProcedure(object param)
{
Class1 get1;
Class2 get2;
...
ClassN getN;
_context.ExecuteInReadUnCommitted
(
() =>
{
get1 = _context.Get1(param);
get2 = _context.Get2(param);
}
);
_context.Get3(param);
}
public class Context
{
public void ExecuteInReadUnCommitted(Action action)
{
try
{
this.SetReadUnCommitted();
action();
}
finally
{
this.PutBackOriginalIsolationLevel();
}
}
}