Last active
December 14, 2015 13:58
-
-
Save AlexeyRaga/5096860 to your computer and use it in GitHub Desktop.
Using SProcs in repositories
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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