I have three database operations like so:
public void Add<T>(T entity)
{
using (var transaction = Session.BeginTransaction())
{
if (entity is IEnumerable)
{
foreach (var item in (IEnumerable) entity)
{
Session.Save(item);
}
}
else
{
Session.Save(entity);
}
transaction.Commit();
}
}
public void Update<T>(T entity)
{
using (var transaction = Session.BeginTransaction())
{
if (entity is IEnumerable)
{
foreach (var item in (IEnumerable) entity)
{
Session.Update(item);
}
}
else
{
Session.Update(entity);
}
transaction.Commit();
}
}
public void Delete<T>(T entity)
{
using (var transaction = Session.BeginTransaction())
{
if (entity is IEnumerable)
{
foreach (var item in (IEnumerable)entity)
{
Session.Delete(item);
}
}
else
{
Session.Delete(entity);
}
transaction.Commit();
}
}
As you can see, the only thing that differs is the Session.[something]
part. How would I refactor this into only one method?