When I find a new idea, I always stick to it and do not see any weaknesses. Bad things happen when I start using a new idea in a large project and later discover that the idea was very bad and I should not use it in any project.
That is why, having a new idea and being ready to use it in a new large project, I need your opinion on this, especially negative .
For a long time, I was bored with typing again and again or copy-paste the following blocks into projects that need to be directly accessed by the database:
string connectionString = Settings.RetrieveConnectionString(Database.MainSqlDatabase);
using (SqlConnection sqlConnection = new SqlConnection(connectionString))
{
sqlConnection.Open();
using (SqlCommand getProductQuantities = new SqlCommand("select ProductId, AvailableQuantity from Shop.Product where ShopId = @shopId", sqlConnection))
{
getProductQuantities.Parameters.AddWithValue("@shopId", this.Shop.Id);
using (SqlDataReader dataReader = getProductQuantities.ExecuteReader())
{
while (dataReader.Read())
{
yield return new Tuple<int, int>((int)dataReader["ProductId"], Convert.ToInt32(dataReader["AvailableQuantity"]));
}
}
}
}
, , - , , :
IEnumerable<Tuple<int, int>> quantities = DataAccess<Tuple<int, int>>.ReadManyRows(
"select ProductId, AvailableQuantity from Shop.Product where ShopId = @shopId",
new Dictionary<string, object> { { "@shopId", this.Shop.Id } },
new DataAccess<string>.Yield(
dataReader =>
{
return new Tuple<int, int>(
(int)dataReader["ProductId"],
Convert.ToInt32(dataReader["AvailableQuantity"]);
}));
:
,
( , , ),
(, while ..),
Intellisense,
, .
:
IEnumerable<string> productNames = DataAccess<string>.ReadManyRows(
"select distinct ProductName from Shop.Product",
new DataAccess<string>.Yield(dataReader => { return (string)dataReader["ProductName"]; }));
ExecuteNonQuery, ExecuteScalar ReadManyRows DataAccess<T>.ReadManyRows , .
:
. , , SqlCommand. , SqlCommand .
SqlCommand s. , , DataAccess , , SqlCommand ExecuteReader(CommandBehavior.SingleRow).
( ).
, DataAccess<T>.ReadManyRows?