August 28, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

DLINQ: Submitting Your Changes

  • June 16, 2006
  • By Sahil Malik
  • Send Email »
  • More Articles »

At this point, you have three options that are specified by the "RefreshMode" enumeration, the idea being that right before the actual update query is executed, the data on the client is "refreshed" with the data in the database at the time of the update. The data is examined and a decision is made. The decision could be one of the following:

  • Keep changes
  • Keep current values
  • Overwrite current values

KeepChanges

You can "keep changes" in scenarios where a conflict has occurred by overwriting the database values with your changed values. Thus, the data in Table 2 would be persisted in the database.

  AnimalName AnimalWeight AnimalType
Final Data Stinky 20 skunk

Table 2. KeepChanges Scenario Data

This 20-pound skunk looks like this in code:

try {
   context.SubmitChanges(ConflictMode.ContinueOnConflict);
}
catch (OptimisticConcurrencyException e) {
   e.Resolve(RefreshMode.KeepChanges);
}
//submit succeeds on second try
context.SubmitChanges(ConflictMode.FailOnFirstConflict);

KeepCurrentValues

In this scenario, User1 would choose to "keep his current values" and overwrite whatever is in the database. But, what are the current values User1 has? The current values would be Stinky, the 10-pound skunk. Yes, the skunk is still 10 pounds in weight, because that is what the original data queried by User1 was and User1 intends to keep his current values. This looks like the following in code:

try {
   context.SubmitChanges(ConflictMode.ContinueOnConflict);
}
catch (OptimisticConcurrencyException e) {
   foreach (OptimisticConcurrencyConflict c in e.Conflicts) {
      cc.Resolve(RefreshMode.KeepCurrentValues);
   }
}

This would result in a database record like Table 3.

  AnimalName AnimalWeight AnimalType
Final Data Stinky 10 Skunk

Table 3. KeepCurrentValues Scenario Data

OverwriteCurrentValues

OverwriteCurrentValues is the converse of KeepCurrentValues. Here, User1 overwrites his own values with the database values in case a conflict has occurred. Thus, after conflict resolution, the database may look like Table 4.

  AnimalName AnimalWeight AnimalType
Final Data Pinky 20 Dog

Table 4. OverwriteCurrentValues Scenario Data

This would look like the following in code:

try {
   context.SubmitChanges(ConflictMode.ContinueOnConflict);
}
catch (OptimisticConcurrencyException e) {
   foreach (OptimisticConcurrencyConflict c in e.Conflicts) {
      cc.Resolve(RefreshMode.OverwriteCurrentValues);
   }
}

Frequently, you also may wish to override the inbuilt conflict detection and resolution logic and take things into your own hands. In those scenarios, you simply can access the conflicting entity using OptimisticConcurrencyConflict.Object. You also can access the individual members that are conflicting by using the OptimisticConcurrencyConflict.GetMemberConflicts method, which returns you an IEnumerable of OptimisticConcurrencyMemberConflict. Each OptimisticConcurrencyMemberConflict has three properties: CurrentValue, OriginalValue, and DatabaseValue. Given all this information, you now cantake a custom informed decision (per your custom logic) and persist the change that you want to persist.

DLINQ Ushering in Change

Every WinForms or ASP.NET application used ADO.NET to work with its underlying data store. However, the DataSet implementation in ADO.NET left a lot to be desired. So, many of us simply wrote everything ourselves, from custom business objects to custom translation engines between business objects and relational data. DLINQ, on the other hand, performs this translation for you in a way that aligns with the LINQ strategy in .NET 3.0.

Although it is a good thing that such support is in the framework now, it is also overwhelming to fathom the possibilities and expanse of change DLINQ and other enhancements in ADO.NET 3.0 bring to the table. But, it is something that you cannot afford to ignore.

About the Author

Sahil Malik has worked for a number of top-notch clients in Microsoft technologies ranging from DOS to .NET. He is the author of Pro ADO.NET 2.0 and co-author of Pro ADO.NET with VB.NET 1.1. Sahil is currently also working on a multimedia series on ADO.NET 2.0 for Keystone Learning. For his community involvement, contributions, and speaking, he has also been awarded the Microsoft MVP award.





Page 2 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel