Presentation is loading. Please wait.

Presentation is loading. Please wait.

ADO.NET in Action Promises Realized.

Similar presentations


Presentation on theme: "ADO.NET in Action Promises Realized."— Presentation transcript:

1 http://adoguy.com ADO.NET in Action Promises Realized

2 http://adoguy.com Who I am Shawn Wildermuth, swildermuth@adoguy.com Shawn Wildermuth, swildermuth@adoguy.com swildermuth@adoguy.com INETA Speaker INETA Speaker Author of “Pragmatic ADO.NET”, Addison- Wesley Author of “Pragmatic ADO.NET”, Addison- Wesley Editor of http://ondotnet.com Editor of http://ondotnet.comhttp://ondotnet.com For More Info: http://adoguy.com For More Info: http://adoguy.comhttp://adoguy.com This Presentation can be found at: This Presentation can be found at: –http://adoguy.com/presentations http://adoguy.com/presentations

3 http://adoguy.com ADO.NET in Action Disconnected Concurrency Disconnected Concurrency DataSets in Web Services DataSets in Web Services Better Business Objects Better Business Objects Improving Typed DataSets Improving Typed DataSets

4 http://adoguy.com Disconnected Concurrency The hard-part of a disconnected architecture is concurrency The hard-part of a disconnected architecture is concurrency –Optimistic Concurrency Supported by CommandBuilders (but inefficient) –Pessimistic Concurrency can be achieved with Check-out, Check-in –Destructive Concurrency can only be handled by writing own updates

5 http://adoguy.com Optimistic Concurrency Methods There are at least three methods: There are at least three methods: –Timestamp –Comparison –Checksum

6 http://adoguy.com CommandBuilder Concurrency CommandBuilders CommandBuilders –Allows updates and deletes where every field in the database is the same as when they were retrieved –Robust –Inefficient

7 http://adoguy.com CommandBuilder Sample DELETE FROM CUSTOMER WHERE ( (CustomerID = @p3) AND ((FirstName IS NULL AND @p4 IS NULL) OR (FirstName = @p5)) AND ((FirstName IS NULL AND @p4 IS NULL) OR (FirstName = @p5)) AND ((LastName IS NULL AND @p6 IS NULL) OR (LastName = @p7)) AND ((LastName IS NULL AND @p6 IS NULL) OR (LastName = @p7)) AND ((MiddleName IS NULL AND @p8 IS NULL) OR (MiddleName = @p9)) AND ((MiddleName IS NULL AND @p8 IS NULL) OR (MiddleName = @p9)) AND ((Address IS NULL AND @p10 IS NULL) OR (Address = @p11)) AND ((Address IS NULL AND @p10 IS NULL) OR (Address = @p11)) AND ((Apartment IS NULL AND @p12 IS NULL) OR (Apartment = @p13)) AND ((Apartment IS NULL AND @p12 IS NULL) OR (Apartment = @p13)) AND ((City IS NULL AND @p14 IS NULL) OR (City = @p15)) AND ((City IS NULL AND @p14 IS NULL) OR (City = @p15)) AND ((State IS NULL AND @p16 IS NULL) OR (State = @p17)) AND ((State IS NULL AND @p16 IS NULL) OR (State = @p17)) AND ((Zip IS NULL AND @p18 IS NULL) OR (Zip = @p19)) AND ((Zip IS NULL AND @p18 IS NULL) OR (Zip = @p19)) AND ((HomePhone IS NULL AND @p20 IS NULL) OR (HomePhone = @p21)) AND ((HomePhone IS NULL AND @p20 IS NULL) OR (HomePhone = @p21)) AND ((BusinessPhone IS NULL AND @p22 IS NULL) OR (BusinessPhone = @p23)) AND ((BusinessPhone IS NULL AND @p22 IS NULL) OR (BusinessPhone = @p23)) AND ((DOB IS NULL AND @p24 IS NULL) OR (DOB = @p25)) AND ((DOB IS NULL AND @p24 IS NULL) OR (DOB = @p25)) AND ((Discount IS NULL AND @p26 IS NULL) OR (Discount = @p27)) AND ((Discount IS NULL AND @p26 IS NULL) OR (Discount = @p27)) AND ((CheckedOut IS NULL AND @p28 IS NULL) OR (CheckedOut = @p29)) ) ((CheckedOut IS NULL AND @p28 IS NULL) OR (CheckedOut = @p29)) )

8 http://adoguy.com Custom Optimistic Concurrency To make it more efficient, we can use Timestamp: To make it more efficient, we can use Timestamp: –DELETE FROM CUSTOMER WHERE (CustomerID = @p3) AND (Stamp = @p4) –DataAdapter.DeleteCommand.CommandText!

9 http://adoguy.com Concurrency Violations DataAdapters throw DBConcurrencyException DataAdapters throw DBConcurrencyException –This is thrown whenever the affected rows of an update is zero. –False positives are possible. DataAdapter.ContinueUpdateOnError – –Each violated row will have errors associated with it. – –DataRow.HasErrors will tell you which rows are problematic

10 http://adoguy.com Handling Concurrency Violations Three options: Three options: –Tell the user the data is stale, and offer to ‘freshen’ it and lose their changes –Ask the user if (s)he wants to overwrite  needs to be done without a CommandBuilder –Field-level collision detection  Allow field to be updated if it hasn’t changed

11 http://adoguy.com DataSets in Web Services Can you use DataSets in Web Services? Can you use DataSets in Web Services? –At first glance, the answer is no  DataSets serialize to DiffGrams, DataSets  DiffGrams are not platform agnostic  Do not communicate the full schema

12 http://adoguy.com DataSets in WebServices (2) –Would you want to use DataSets anyway?  Great container for Structured XML  Database integration  Easy to intermingle database/XML data –When *not* to use DataSets  When creating XML Documents from non- database sources

13 http://adoguy.com Possible Solutions? DataSets can hold XML, so you can: DataSets can hold XML, so you can: –return myDataSet.GetXml() How about XmlDataDocument? How about XmlDataDocument? –return new XmlDataDocument(myDataSet)

14 http://adoguy.com Web Services and Schema DataSet schema is XSD! DataSet schema is XSD! –When using DataSets, DocLiteral is your friend  DataSets can define and be the holder for the documents that Web Services throw around –DataSet schema can define our WSDL Types  Yassar Shohoud’s book shows you how  “Real World XML Web Services”

15 http://adoguy.com Better Business Objects Using Typed DataSets Using Typed DataSets –Strong Typing –XSD Based Schema –Simple to Setup relationships, constraints, etc. –Not very much use if you have amorphous data

16 http://adoguy.com DataSet Structure

17 http://adoguy.com Typed DataSet Structure * TypedDataSet Customers CustomerRowCustomerIDColumn 1 * 1 * 1 * DataRow DataSet DataColumn ConstraintDataRelation DataTable 1 * 1 * 1 1 * 1 * 1 * 1 * Legend 1 *.NET Class Composition Generalization 1 *

18 http://adoguy.com Better Business Objects Writing Business Objects normally involve three things: Writing Business Objects normally involve three things: –Relational-to-Hierarchical Mapping –Data Access –Business Rules Traditional Business Objects Traditional Business Objects –Set of classes –Strongly Typed

19 http://adoguy.com Better Business Objects Writing Business Objects with Typed DataSets Writing Business Objects with Typed DataSets –Relational-to-Hierarchical Mapping for free –Data Access is handled by ADO.NET –Write your business rules and you’re done! Typed DataSet Typed DataSet –ARE a set of classes –ARE Strongly Typed

20 http://adoguy.com Better Business Objects Business Rules in Typed DataSets Business Rules in Typed DataSets –Event Driven  Catch OnRowChanging/OnRowChanged Events  Handle Business Logic to ensure that rules are enforced –Derivation  Derive from Typed DataSet  Problems with Microsoft’s Generated code to allow this  See http://adoguy.com/powertoys for a solution

21 http://adoguy.com Improving Typed DataSets Typed DataSets support Annotations Typed DataSets support Annotations –Annotations improve usability of generated code –Types can be renamed  Instead of CustomerTable, CustomerRow, how about Customer –Null Behavior can be changes  Throws exception by default  Returning empty or null references might be better

22 http://adoguy.com Annotations Annotations in the Typed DataSet XSD: Annotations in the Typed DataSet XSD: –typedName: Specifies the name of an object. –typedPlural: Specifies the name of the collection of objects.  typedParent: Specifies the name of the parent relationship.  typedChildren: Specifies the name of the child relationship.

23 http://adoguy.com Annotations - Continued Annotations in the Typed DataSet XSD: Annotations in the Typed DataSet XSD: –nullValue: Specifies how to handle a DBNull value.  Replacement Value: The value to return instead of a null –codegen:nullValue=""  _throw: Throws an exception when the value is null –codegen:nullValue=_throw  null: Returns a null reference. –If a value type is encountered an exception is thrown.  empty: Returns a reference created with an empty constructor. –For strings, it returns String.Empty. For value types, an exception is thrown.

24 http://adoguy.com Questions?


Download ppt "ADO.NET in Action Promises Realized."

Similar presentations


Ads by Google