views:

280

answers:

2

How can we apply Sql Dependency in Asp.Net MVC for cached objects?

+1  A: 

MSDN says:

To set up a dependency, you need to associate a SqlDependency object to one or more SqlCommand objects. To receive notifications, you need to subscribe to the OnChange event.

If you are using an ORM for your data model, I'm not sure how this applies.

More info here:

http://msdn.microsoft.com/en-us/library/t9x04ed2.aspx

Robert Harvey
Well, SqlDependency requires a SQL query which can only use features allowed in SQL Server query notifications. Since your ORM very likely does not know about this feature, it means you have to write a standard SQL query. Your ORM will very likely be able to give you the SQL query it will use to execute a certain ORM-style query. So your choices are to manually write the query which covers the data which is likely to change, or use the ORM's query, and hope that it falls within the rules of what is allowed for a SQL Server query notification.
Craig Stuntz
@Craig: Thanks for the info.
Robert Harvey
One other thing I forgot to add: Query notifications put a certain degree of load on the SQL Server. So you would prefer to use as few of them as possible. Therefore, if you are composing queries via your ORM, you probably don't want to create a query notification for every ORM query you compose. You would probably prefer to use a single, manually written query, which could provide appropriate notifications for a wide variety of ORM queries which touch the same data.
Craig Stuntz
+1  A: 

http://code.sayao.net/2009/07/caching-html-helpers-on-aspnet-mvc.html

zapping
Current versions of SQL Server do not use polling to implement query notifications.
Craig Stuntz