So, I think it’s a bit odd to have a Save method which does not return the entity which has just been successfully saved. However, the entity passed into Save is modified by the function.
Example:
//T SaveOrUpdate(T entity); from IDao
PlaylistDao.SaveOrUpdate(playlist);
This function takes a playlist, saves it to the database, and returns the saved playlist. But, the paramater being passed into SaveOrUpdate is reference equals to its return value.
With this in mind, which is a more clear implementation:
public void SavePlaylist(Playlist playlist)
{
try
{
NHibernateSessionManager.Instance.BeginTransaction();
PlaylistDao.SaveOrUpdate(playlist);
NHibernateSessionManager.Instance.CommitTransaction();
}
catch (Exception exception)
{
Logger.Error(exception);
throw;
}
}
vs
public Playlist SavePlaylist(Playlist playlist)
{
Playlist savedPlayist;
try
{
NHibernateSessionManager.Instance.BeginTransaction();
savedPlayist = PlaylistDao.SaveOrUpdate(playlist);
NHibernateSessionManager.Instance.CommitTransaction();
}
catch (Exception exception)
{
Logger.Error(exception);
throw;
}
return savedPlayist;
}
I think that the second function is more clear to someone who has not seen the code before, but the first implementation is more succinct and more clear once the developer understands. Any thoughts?
UPDATE: To be clear, SaveOrUpdate has side-effects on playlist. When playlist is saved to the database, its ID field is updated with a value provided by the DB.
I would go for the first method but with a small change–pass the argument with
ref. (See the comments.) The problem I have with the second is that you are creating an extra variable unnecessarily. Why not just add some<summary>tags to the first method, indicating in intellisense what is going to happen to clarify any confusion? For me, this is better than creating an extra variable to essentially do the same thing.