Created
September 2, 2019 23:29
-
-
Save evgomes/da39455b1f28deaa4eaf30288797152e to your computer and use it in GitHub Desktop.
ADO.NET implementation example of a repository.
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
public class CategoryRepositoryADO : ICategoryRepository | |
{ | |
public async Task<IEnumerable<Category>> ListAsync() | |
{ | |
var categories = new List<Category>(); | |
using(var connection = new SqlConnection("connection_string")) | |
using(var command = new SqlCommand()) | |
{ | |
command.Connection = connection; | |
command.CommandType = CommandType.StoredProcedure; | |
command.CommandText = "StoredProcedureName"; | |
connection.Open(); | |
using(var reader = await command.ExecuteReaderAsync()) | |
{ | |
while(await reader.ReadAsync()) | |
{ | |
var category = new Category | |
{ | |
Id = Convert.ToInt32(reader["Id"]), | |
// Fill other fields | |
}; | |
} | |
} | |
connection.Close(); | |
} | |
return categories; | |
} | |
// Other methods | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment