Skip to content

Instantly share code, notes, and snippets.

@evgomes
Created September 2, 2019 23:29
Show Gist options
  • Save evgomes/da39455b1f28deaa4eaf30288797152e to your computer and use it in GitHub Desktop.
Save evgomes/da39455b1f28deaa4eaf30288797152e to your computer and use it in GitHub Desktop.
ADO.NET implementation example of a repository.
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