How do I combine similar method calls into a delegate pattern?
Posted
by Daniel T.
on Stack Overflow
See other posts from Stack Overflow
or by Daniel T.
Published on 2010-04-30T21:29:34Z
Indexed on
2010/04/30
21:47 UTC
Read the original article
Hit count: 223
I have three methods:
public void Save<T>(T entity)
{
using (new Transaction())
{
Session.Save(entity);
}
}
public void Create<T>(T entity)
{
using (new Transaction())
{
Session.Create(entity);
}
}
public void Delete<T>(T entity)
{
using (new Transaction())
{
Session.Delete(entity);
}
}
As you can see, the only thing that differs is the method call inside the using
block. How can I rewrite this so it's something like this instead:
public void Save<T>(T entity)
{
TransactionWrapper(Session.Save(entity));
}
public void Create<T>(T entity)
{
TransactionWrapper(Session.Create(entity));
}
public void Save<T>(T entity)
{
TransactionWrapper(Session.Save(entity));
}
So in other words, I pass a method call as a parameter, and the TransactionWrapper
method wraps a transaction around the method call.
© Stack Overflow or respective owner