Need to dynamically instantiate different objects in C#/ASP.NET - Visual Studio 2010
I'm refactoring some code for a colleague. The program in question accesses a SQL database and uses stored procedures to pull different datasets. There is a datacontext setup in Visual Studio, so all of these stored procedures can be accessed through the designer.cs file. The results look like they are returned as differently typed objects (ie, StoredProcAResult, StoredProcBResult) etc...
Anyway, I am not allowed to change the SQL database at all, but I can mess with the ASPX and CS files. there are currently three files which perform almost identical functions (displaying a set of data). Each page calls a different stored procedure - so, say ViewNewProducts, ViewAgedProducts, ViewExpiredProducts. The view is identical, so I want to roll these three pages into one.
If I could touch the SQL, I'd rewrite the stored proc to accept parameters and return the proper data. However, I can't - so I've made one ASPX/CS file, have a variable set to the dataset the user needs (New, Aged, or Expired) and now find myself dealing with this line:
Utility.EnhanceGrid(dataBoundItem.Cells, dataSet.salesperson, dataSet.sku);
The problem is setting the dataSet. The original individual files just used something like:
var dataSet = (INVENTORY.ViewExpiredProductsResult)dataBoundItem.DataItem;
But now I don't know which stored proc is going to be called until the user selects.
I tried to use a switch statement, but I can't declare the var as null... I can't type the variable... is there a way to use a string value to set the type of the var?
I have a workaround with this code, but I hate repeating myself and this feels awfully sloppy...any advice would be appreciated.
switch (usersSelection())
{
case "New":
ViewNewProductsResult newResult = (INVENTORY.ViewNewProductsResult)dataBoundItem.DataItem;
Utility.EnhanceGrid(dataBoundItem.Cells, newResult.salesperson, newResult.sku);
开发者_开发问答 break;
case "Aged":
ViewAgedProductsResult agedResult = (INVENTORY.ViewAgedProductsResult)dataBoundItem.DataItem;
Utility.EnhanceGrid(dataBoundItem.Cells, agedResult.salesperson, agedResult.sku);
break;
case "Expired":
ViewExpiredProductsResult expiredResult = (INVENTORY.ViewExpiredProductsResult)dataBoundItem.DataItem;
Utility.EnhanceGrid(dataBoundItem.Cells, expiredResult.salesperson, expiredResult.sku);
break;
}
If using .NET Framework 4, you can use the dynamic
type. So your code would like this:
dynamic dataSet = null;
switch (usersSelection())
{
case "New":
dataSet = (INVENTORY.ViewNewProductsResult)dataBoundItem.DataItem;
break;
case "Aged":
dataSet = (INVENTORY.ViewAgedProductsResult)dataBoundItem.DataItem;
break;
case "Expired":
dataSet = (INVENTORY.ViewExpiredProductsResult)dataBoundItem.DataItem;
break;
}
if (dataSet != null)
{
Utility.EnhanceGrid(dataBoundItem.Cells, dataSet.salesperson, dataSet.sku);
}
The actual type would be determined at runtime but since each type has salesperson and sku it should work.
More on dynamic here MSDN Dynamic Type
I can't type the var as null
Type it as object instead of var
As a sidenote, there is Activator.CreateInstance that will allow you to instantiate by name (and many other overloads)
精彩评论