Skip to content

Instantly share code, notes, and snippets.

@AlexeyRaga
Last active December 14, 2015 13:58
Show Gist options
  • Save AlexeyRaga/5096860 to your computer and use it in GitHub Desktop.
Save AlexeyRaga/5096860 to your computer and use it in GitHub Desktop.
Using SProcs in repositories
//private method to use SProc
private void WithStoredProcedure(string storedProcedureName, Action<SqlCommand> commandAction)
{
using (var connection = OpenConnection())
using (var command = connection.Connection.CreateCommand())
{
command.CommandText = storedProcedureName;
command.CommandType = CommandType.StoredProcedure;
commandAction(command);
}
}
//Usage example
public void MarkAsDispatched(string messageId)
{
WithStoredProcedure("MarkAsDispatched", command =>
{
command.Parameters.AddWithValue("@messageId", messageId);
command.ExecuteNonQuery();
});
}
//another example
public IList<MessageData> GetUndispatchedMessages()
{
var undispatchedMessages = new List<MessageData>();
WithStoredProcedure("GetUndispatchedSlice",
command =>
{
//probably should be another method that receives IReader
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
var messageData = new MessageData(
reader.GetString(0),
reader.GetString(1),
reader.GetSqlBinary(2).Value,
reader.GetSqlBinary(3).Value);
undispatchedMessages.Add(messageData);
}
}
});
return undispatchedMessages;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment