当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - Using Dependency Property for Database

问题描述:

In WPF we use Dependency Properties, but I wonder if it is possible to use them in web applications.

E.g. let's say I have a textbox's value "jack". That name comes from database, so when database table field gets updated and changes that name to "john", how can this effect textbox's value, so it changes to "john" too?

网友答案:

In a Web Application, things work a little different then in WPF applications.

In a WebApp a requests come in, the server processes it and then returns a result to the clients browser. If something changes on the server, the server has no way to let the client know this until a new requests comes in and the server can return a new response.

ASP.NET has the concept of DataBinding and DataSources. You can for example configure a SqlDataSource to execute a query and bind the results to a Grid or List control. This will execute when a user loads the page.

Automatically refreshing data when something changes on the server would require the client to constantly poll the server for changes (in a async way with AJAX) to see if there are changes and then update the HTML in the browser. This is what you see happening in apps like Twitter or LinkedIn but it is a complete different concept then what you're used to in WFP apps.

网友答案:

Good idea - but you're missing some of the major issues.

A dependency property is essentially an in memory object that various things subscribe to for changes.

To do the same for a db you need a way to communicate that change (to the application). Since these are out of process it will have to be some cross process or even cross server/network call. All of this needs to be defined and constructed by you manually.

网友答案:

There is no such thing on web as data binding in wpf. DB does not implement interfaces for data changes. What you can do is use DBs stored procedures (compiled) and communicate data change on the trigger to the web instance(s). From there on, one could update view/webpage via Ajax.

This would be "push" method which is of course better solution but harder to implement.

You can also do "data pooling" on timed interval and update page accordingly which is simple to implement but really bad resource vise.

Best regards, P.

网友答案:

Short answer yes you can, it doesn't matter where the data is coming from. Write a class that implements INotifyPropertyChanged which is basically called ViewModel. This class will wrap your data coming from the DB, and notifies the view when values change.

分享给朋友:
您可能感兴趣的文章:
随机阅读: