I have created a data access layer(DAL) in my project and i am using Entity framework5 for CRUD operations. Also i have implemented repository and unit of work patterns in my data access layer.
Now i have come up with a situation where i have to execute a stored procedure which expects a table value parameter as input. How to write a generic function and in which class in DAL that executes any stored procedure with any type of parameters? And i dont want to create hardcode SqlParameters in my business layer.
Currently i have hardcoded this function in my unit of work class.
public void ExecuteSqlCommand(string command, params object[] parameters)
{
DataTable dataTable = new DataTable();
dataTable.Columns.Add(new DataColumn("ProductId"));
dataTable.Columns.Add(new DataColumn("IsListed"));
dataTable.Columns.Add(new DataColumn("WasEverListed"));
dataTable.Columns.Add(new DataColumn("MarketplaceItemNumber"));
SqlParameter param = new SqlParameter("@tvpProductIsListed", SqlDbType.Structured);
param.SqlValue = dataTable;
param.TypeName = "dbo.ProductIsListedTableType";
int ret = dbContext.Database.ExecuteSqlCommand("EXEC " + command + " @tvpProductIsListed, @RetailerId, @BrandId, @MarketplaceId",
param,
new SqlParameter("@RetailerId", 1),
new SqlParameter("@BrandId", 1),
new SqlParameter("@MarketplaceId", 1));
}
Help will be highly appreciated in this problem!