Advice on Minimizing Stored Procedure Parameters - asp.net-mvc

I have an ASP.NET MVC Web Application that interacts with a SQL Server 2008 database via Entity Framework 4.0.
On a particular page, i call a stored procedure in order to pull back some results based on selections on the UI.
Now, the UI has around 20 different input selections, ranging from a textbox, dropdown list, checkboxes, etc.
Each of those inputs are "grouped" into logical sections.
Example:
Search box : "Foo"
Checkbox A1: ticked, Checkbox A2: unticked
Dropdown A: option 3 selected
Checkbox B1: ticked, Checkbox B2: ticked, Checkbox B3: unticked
So i need to call the SPROC like this:
exec SearchPage_FindResults #SearchQuery = 'Foo', #IncludeA1 = 1, #IncludeA2 = 0, #DropDownSelection = 3, #IncludeB1 = 1, #IncludeB2 = 1, #IncludeB3 = 0
The UI is not too important to this question - just wanted to give some perspective.
Essentially, i'm pulling back results for a search query, filtering these results based on a bunch of (optional) selections a user can filter on.
Now, My questions/queries:
What's the best way to pass these parameters to the stored procedure?
Are there any tricks/new ways (e.g SQL Server 2008) to do this? Special "table" parameters/arrays - can we pass through User-Defined-Types? Keep in mind im using Entity Framework 4.0 - but could always use classic ADO.NET for this if required.
What about XML? What are the serialization/de-serialization costs here? Is it worth it?
How about a parameter for each logical section? Comma-seperated perhaps? Just thinking out loud.
This page is particulary important from a user point of view, and needs to perform really well. The stored procedure is already heavy in logic, so i want to minimize the performance implications - so keep that in mind.
With that said - what is the best approach here?

By a quick Googling, it looks like Entity Framework doesn't support them, but you can use Table Valued Parameters in ADO.
You manage that by defining a table type in your server:
CREATE TYPE yourTypeName AS TABLE (
columnName <column type>
-- more columns if you wish, too
)
and then use it by making a SqlCommand, and doing this:
SqlParameter param = command.CreateParameter();
param.ParameterName = "#something"
param.SqlDbType = SqlDbType.Structured
param.Value = // a DataTable which matches your table type, I can stick in code I use if you like
param.TypeName = "table type name";
And your procedures will look like this:
CREATE PROCEDURE procName
#something yourTypeName READONLY
AS
BEGIN
...
Your SQL queries will then use that parameter as if it were a table, though it's read-only.
Performance-wise, I'm not a DBA, and I'm not sure how fast XML is - I gather it's pretty quick. TVPs though are rather blazingly fast, and even the relatively-simple DataTable method I've used runs great. There's more about TVPs here, and quite a few blog posts out there about SQL and TVPs in general.

Related

Pass a table variable from one stored procedure to another

I'm working on a SQL Server 2005 database and I'm trying to pass a table variable from one stored procedure to another. Below is the structure of the table and my attempt.
#MyTable
EmployeeID FirstName LastName
1 Dummy Last
2 Some Name
I tried converting the table into XML and passing the XML as a parameter to the new SP.
SELECT * FROM #MyTable FOR XML AUTO
Result
<_x0040_MyTable EmployeeID="1" FirstName="Dummy" LastName="Last" />
<_x0040_MyTable EmployeeID="2" FirstName="Some" LastName="Name" />
But converting back from this XML to a table has become too complicated for my level of SQL knowledge. Am I on the right path? Is there an easy way?
Note - I have seen a few posts which suggests to use #temp tables and I would not prefer to use that solution. I would like to use the #MyTable parameter.
Erland Sommarskog is your friend here. The link below provides a table showing options. For SQL Server 2005, TVF aren't an option. You will likely need to use XML or a temp table to share the data. I know you say these two are the least desirable options for you, but for 2005, I think they are the most appropriate.
http://www.sommarskog.se/share_data.html

Obtain Trigger generated values after ApplyUpdates with a TClientDataSet

I have a (Firebird) DB. For most of my tables I have a trigger which fires before insert which will create the Primary Key (PK) for me via a generator as well as write to the newly inserted records a Created Date value and a Created By value. I also have an update trigger which writes to an Updated Date field and an Updated By field.
eg (Client is a table in my DB):
create trigger t_client_id for client
active before insert
as begin
new.client_id = gen_id(gen_client_id, 1);
new.created = current_timestamp;
new.created_by = current_user;
new.lock_vn = 1;
end ^
create trigger t_client_update for client
active before update
as begin
new.updated = current_timestamp;
new.updated_by = current_user;
end ^
When I apply updates thru my ClientDataSet (CDS) - which are attached to remote TDataSetProviders via a TDSProviderConnection, how can I "retrieve" these generated values? If I edit an existing one (which will in turn call the t_client_update trigger, calling RefreshRecord will get the updated and updated_by fields. However, the Doco says to use that method cautiously, so that may not be the correct way to achieve this. I call it straight after I've called ApplyUpdates(-1).
The CDS I use only contains the one record I am attempting to Edit. For a New record, the CDS is in dsInsert mode. Everything is written to the DB ok so I just need to get this new data back out again. I have also tried using a CDS which contains ALL records in the table too to see if it was any simpler but didn't make any difference - unsurprisingly. The reason I need this information is simply to show to the user in DB Aware controls these values. They are read only.
I could call a Get on the record I guess when editing an existing record, using the PK, but that won't help for an Insert as I don't know what the new PK is.
Example of where I attempt to ApplyUpdates to my CDS (actDSSave is a TDataSetPost action)
dsState := actDSSave.DataSource.DataSet.State;
DoApplyUpdates(-1);
if dsState = dsEdit then
TClientDataSet(actDSSave.DataSource.DataSet).RefreshRecord;
I am using TIBQuery for my dataset attached to the remote DataSetProvider. This query SQL is a simple select * from client where client_id = :client_id. I have tried associating this query with a TIBUpdateSQL too as well as trying to set poAutoRefresh to true in the DataSetProvider.
So is it possible to obtain these Trigger generated values this way or do I need to approach it in a different way? Another way I can think of, is to create stored procedures which do CRUD against each table and use that instead (with appropriate in/out params to return this new data) but hopefully I don't have to go down that track. Hopefully I have provided sufficient info here to explain and replicate the issue.
Thanks
EDIT
Realised in above, DoApplyUpdates(-1) is my own method. It's implementation at the moment is simply:
FdatCommon.cdsClient.ApplyUpdates(MaxErrorCount);
FdatCommon is a TDataModule containing my CDS.
You simply can't get "generated" values without new requery (RefreshRecord) of data after Post.
It's because triggers runs on server side when you call ApplyUpdates, but TClientDataSet does not refresh by default posted record. For example other libraries FIBPlus have an option to do it automatically.
About inserts, TIBDataSet have GeneratorField property. Using it, dataset query and increment generator value separatelly before insert. So you will have PK values after post even on inserts. But avoid using it again in trigger.
MIDAS (TClientDataSet) is a great library, but his general / universal architecture loose DB specific features (such as retriving values from inserts) compared to dedicated libraries for specific DBMS, such as FibPlus. By the way I saw TpFIBClientDataSet. It work in conjunction with TpFibDataSet.

Auditing Changes under MVC & Entity Framework (using sprocs)

I have the challenge of needing to audit data changes made by users of an MVC application.
Auditing creation and deletion of records is easy.
Updates is proving to be the problem.
I'm looking for a way to automate this, but the problem I have is that the application is using stored procedures to bring back EF "complex types".
These are then used to build a view model, and after postback, the controller receives a new view model built from the form values passed back from the view. Therefore the original values are no longer available.
Does anyone have any suggestions for a secure way to keep the original values so they can be compared with the updated values, so that changes can be stored?
(I appreciate I could go back to the database for these, but is not efficient, and I would have to retain all the parameters to remake the same call, and find a way to automate that part of the process).
Have you tried an Audit Trigger using the INSERTED and DELETED tables.
http://weblogs.asp.net/jgalloway/archive/2008/01/27/adding-simple-trigger-based-auditing-to-your-sql-server-database.aspx
OR
In your stored procedures for insert,delete,update you can make use FOR XML AUTO. To get the XML for the record and add it to an audit table.
http://www.a2zdotnet.com/View.aspx?Id=71
UPDATE A T-SQL example
BEGIN
-- these tables would be in your database
DECLARE #table TABLE(ID INT IDENTITY(1,1) PRIMARY KEY, STR VARCHAR(10), DT DATETIME)
DECLARE #audit_table TABLE(AuditXML XML, Type VARCHAR(10), Time DATETIME)
-- this is defined at the top of your stored procedure
DECLARE #temp_table TABLE(PK INT)
-- your stored procedure will add an OUTPUT to the temp table
INSERT INTO #table
OUTPUT inserted.ID INTO #temp_table
VALUES ('test1', GetDate()),
('test2', GetDate() + 2)
-- at the end of your stored procedure update your audit table
INSERT INTO #audit_table
VALUES(
(
SELECT *
FROM #table
WHERE ID IN (SELECT PK FROM #temp_table)
FOR XML AUTO
),
'INSERTION',
GETDATE()
)
-- your audit table will have the record data
SELECT * FROM #audit_table
END
In the example above you could make temp_table a clone of table (have all of the columns from table) and in your OUTPUT clause use INSERTED.* INTO #temp_table, this would avoid have to reselect the records before getting the FOR XML AUTO. Another note, for stored procedures that do DELETE you would use DELETED.* instead of INSERTED.* in your OUTPUT.
If using SQL Server I recommend that you look into Change Data Capture (CDC).
It's an out of the box solution for auditing changes to the underlying tables of your application and it's relatively straightforward to set up, so there is no need for a custom solution that you then have to maintain.
If you have any supporting applications for your site, they'll also be covered and it also has the benefit of auditing any changes made directly against the database, such as from a DBA running a script.
Since your asp.net application may be running under one particular account, you'll probably need to add additional tracking information to capture the user who made the change. Fortunately this is also relatively straightforward. The following Stack Overflow question covers an approach to this using the ObjectStateManager
I was lookging for this myself, found this, check out Tracker for EF

Multiple Dynamically Populated Drop Down Lists

I've got a page which will have about ten drop down lists which are generated from my SQL database. What's the best way to populate them? I was going to use a stored procedure with LINQ to return multiple result sets, but it seems a bit overkill. Is there another way of doing it? Using HtmlHelpers or something like that?
Seeing as though everyone seems to be confused by this, I will elaborate.
This isn't to do with caching, that's not what the problem states
This isn't to do with ASP.NET controls such as DropDownList, I tagged it asp.net-mvc
This isn't to do with code-behind models, I thought that was implicitly obvious by how I tagged this question originally as ASP.NET MVC
The problem is that multiple results sets are required on the page for drop down lists (think HTML!). So I have a drop down list for your favourite breed of badger, a drop down list for how many birthdays you've had, a drop down list for how many clouds are in the sky today. All of these are dynamically populated (please note, I am joking, this is a finance system I work on). I need all of them to be on my view page, but I'd rather NOT use the IMultipleResult return type in a LINQ stored procedure to bring back multiple result sets. It just gets messy.
So in basic, I want about 10 drop down lists on my view page, all of which are populated with data from a database (which constantly change). What is the best way to get them on the view?
I would just pass the required data to the view, either in multiple ViewData dictionaries or as a special view model if you want strongly typed access. Then use HtmlHelper.DropDownList() to display the actual drop downs.
Weakly typed solution
Controller
ViewData["Data1"] = SomeRepository.GetList();
ViewData["Data2"] = SomeRepositoty.GetList();
return View();
View
<%= Html.DropDownList("Data1") %>
<%= Html.DropDownList("Data2") %>
Strongly typed solution
View model
public class DataViewModel
{
public IEnumerable<string> Data1 { get; set; }
public IEnumerable<string> Data2 { get; set;}
}
Controller
var model = SomeRepository.GetModel(); // returns an instance of DataViewModel
return View(model);
View
<%= Html.DropDownList("Data1", new SelectList(Model.Data1)) %>
<%= Html.DropDownList("Data2", new SelectList(Model.Data2)) %>
It depends on the data, if the data in the database is not updated that often, then you could have a process that creates XML files once a day. Then use the XML files as the source to the dropdowns, this would speed up the application and limit the calls to the database server.
This is really a question of data management, not anything to do with HtmlHelpers until you get to the very pointy end of things. Anyhow, the first thing I would ask myself is "how is this data updated and what kind of filtering do I need?"
If this list is pretty much constant, then you could go for a bit of caching.
If this list is pretty fluid, then just pull it out of the database as needed and be done with it. Worry about caching if your DB box starts melting.
Either way you probably want to wrap things up in some sort of LookupService class, but there isn't enough to go on to make any more specific recommendations.
I second Wyatt's recommendation about grabbing from the DB each time if the list is fluid.
However, I'm doing a similar thing (with 10+ dropdowns on each page, all very static... the values will rarely change).
The first version of the application had a helper method for each dropdown list (about 20 total by the end of the project) that grabbed from the respective tables and cached via another helper. Before I started caching, there was no db context available for the view (i created it in the controller and didn't pass it), each dropdown had to create a new connection. This got noticeably slow. Plus, I had some problems with my caching routines, and saving them with 20 magic strings in the cache, etc. Also, I had a separate querying object where I had to manually build the relationships, and having to create the 20 relationships for inner joins was a pain.
So... my new version:
I'm using a single "selectables" table. There's a PK, and a "selectable type" (which I have to admit is a string). There's a selectables enumerable, which makes things a bit cleaner. There's a main getAllSelectables() method that looks for the entire result set in the cach (and gets all rows from the db if its not in the cache) and returns it. Then there's a getSelectables(enum) that grabs out only the relevant values, and a third function getSelectListItems(enum) that calls getSelectables(enum) and returns a ienumerable for the mvc helper function.
Hope that helps,
James

NHibernate + ASP.Net MVC - how to order data in strongly typed manned according to user selected field

I'm presenting data for users in a grid (ExtJS) with remote sorting and paging. Let's say I have a grid with some Orders. Order entity looks like Order{OrderNumber, Customer, Date, Address{Street, City, PostCode}}. Customer is mapped by NH as relation, Address is mapped as component. Data presented in the grid are flattened to columns named like this: OrderNumber, Customer.Number, Customer.Name, Date, Address.Street, Address.City, Address.PostCode.
User selects a column which he'd like to sort by and the grid sends the field name to server. Now on server side I need to restore backwards what entity property belongs to grid field name and decide if it's just component or if it's relation and build Criteria with CreateAlias + AddOrder etc. This logic is full of code like:
if (gridField=="Customer.Name"){
cri = cri.createAlias("Customer", "customerAlias");
cri.AddOrder(Order.Asc("customerAlias.Name"));
}
This is much simplified, but it neccesarily looks like this at the moment. I'm looking for some generic smarter solution. Any thoughts? The problem I'm facing now is that I can have a convention for transforming entity properties (including nested components and relations), but than I need to have a method how to determine if the field is mapped like component or relation. This would be quite heavy....
It would be quite heavy. I don't see a simple solution, but if you are planning on re-using this a lot, or need something very robust, you could build a system based on reflection.
Another possibility would be to use some T4 templates, but that would only help the 'string' issue, not the association issue.
How about:
cri = cri.CreateAlias( "Customer", "CustomerAlias" );
string sortProperty = gridField.Replace("Customer.", "CustomerAlias.");
cri.AddOrder( Order.Asc(sortProperty) );

Resources