In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "what is the role of Linq Tracking Changes mechanism". In daily operation, I believe many people have doubts about what role Linq Tracking Changes mechanism plays. Xiaobian consulted all kinds of materials and sorted out simple and easy operation methods. I hope to help you answer the doubts about "what role Linq Tracking Changes mechanism plays"! Next, please follow the small series to learn together!
Linq Tracking Changes mechanism
OK, we can get the data from WCF Service smoothly, so what should we do next? According to MSDN's "Incidentally" description, to implement the update function in the N-Tier case must depend on the UI layer, in ASP.NET can be done through the assistance of ObjectDataSource control, but in WPF, Windows Forms, programmers must implement the client-side Linq Tracking Changes mechanism.
What is Linq Tracking Changes? Simply put, updating a data requires two pieces of information, one is the present value of the Entity Object, and the other is the original value of the Entity Object. When updating the data, we must send these two pieces of information to WCF Service, and then WCF Service will update the present value after obtaining the data row to be updated according to the original value.
The problem is that LINQ To SQL Designer only marks Entity Class as serializable and does not generate any code required for Tracking Changes. On the WPF side, we must record the original value when updating the data row, otherwise we cannot update the data through WCF Service. More precisely, there is a mechanism missing similar to the GetChanges function of DataSet.
So how do we implement this mechanism? Fortunately, the Entity Class generated by LINQ To SQL Designer implements the INotifyPropertyChanging interface, so we can mount events to the PropertyChanging events defined by them, and record the original values in advance when the object property values change, completing the Linq Tracking Changes mechanism. Unfortunately, the Proxy Class generated by WCF Service ignores this interface and does not generate the corresponding code, so the idea of implementing Tracking Changes through the INotifyPropertyChanging interface is impossible.
Second, we can only implement Tracking Changes with the concept of Context, that is, when we get an object, we copy all objects, keep the original value, and then detect whether the object has been changed through another interface INotifyPropertyChanged.
using System; using System.ComponentModel; using System.Collections.Generic; using System.Linq; using System.Text; using System.Reflection; namespace WpfDataConsumer { public class TrackingContext { ……………. public void Initialize(IList objs) { _states.Clear(); _update_original = typeof(TUpdate).GetProperty("Original"); _update_current = typeof(TUpdate).GetProperty("Current"); _update_state = typeof(TUpdate).GetProperty("State"); foreach (T item in objs) { object updateData = Activator.CreateInstance(typeof(TUpdate), false); _update_original.SetValue(updateData, CloneObject(item),null); _update_current.SetValue(updateData, item, null); _update_state.SetValue(updateData, WpfDataConsumer.DataService.UpdateState.UnChanged, null); ((INotifyPropertyChanged)item).PropertyChanged += new PropertyChangedEventHandler(TrackingContext_PropertyChanged); _states.Add(item, (TUpdate)updateData); } } void TrackingContext_PropertyChanged(object sender, PropertyChangedEventArgs e) { if (_states.ContainsKey((T)sender)) { object o = _states[(T)sender]; DataService.UpdateState state = (DataService.UpdateState)_update_state.GetValue(o, null); if (state == WpfDataConsumer.DataService.UpdateState.Insert) return; _update_state.SetValue(o, WpfDataConsumer.DataService.UpdateState.Update, null); } } } }
With the help of Linq Tracking Changes, WCF Server can obtain the original and present values of the data object to be updated, so it is not difficult to update the data.
At this point, the study of "Linq Tracking Changes mechanism" is over, hoping to solve everyone's doubts. Theory and practice can better match to help everyone learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.