Workshop

for RuBoard

This workshop will help reinforce the concepts covered in today's lesson.

Quiz

1:

What are the benefits of creating a data factory class?

A1:

Using a data factory can have several benefits, including reducing the amount of code you have to write in your data access classes, abstracting the provider used, abstracting database-specific syntax, and caching command objects for reuse.

2:

What method can you use to dynamically load assemblies and create objects based on their type information?

A2:

In the System.Reflection namespace, the Assembly class exposes shared LoadFrom , Load , and LoadWithPartialName methods to load an assembly into memory, whereas the Activator class exposes a shared CreateInstance method to create instances of a type at runtime.

3:

How can database independence be achieved in a data factory?

A3:

To make the data factory database independent, you must not only abstract the provider objects used but also the SQL syntax used to execute statements against the data store. The DataFactory class shown today uses XML files, called statement files, in which the database-specific SQL syntax remains.

Exercise

Q1:

Write a simple method that uses the DataFactory to update the Description column of a particular ISBN and then deletes the ISBN in the context of a single local transaction.

A1:

One possible solution to the exercise in C# is as follows :

 virtual void UpdTitles(string isbn, string desc) {     // Create the DataFactory     DataFactory e = new DataFactory(_connect,"SqlClient");     // Start a transaction     IDbTransaction trans = e.BeginTransaction(IsolationLevel.ReadCommitted);     // Setup the parameters     HybridDictionary parms = new HybridDictionary();     parms.Add("isbn", isbn);     parms.Add("desc", desc);     try     {         int retval = 0;         int rows = e.ExecuteNonQuery("UpdateTitle", parms,ref retval,trans);         Console.WriteLine(rows); //should be 1         parms.Remove("desc");         rows = e.ExecuteNonQuery("DeleteTitle", parms, ref retval, trans);         Console.WriteLine(rows); //should be 1         // success so commit the transaction        trans.Commit();     }     catch (DataFactoryException ex)     {        // Error so rollback        trans.Rollback();        Console.WriteLine(ex.Message);        Console.WriteLine(ex.InnerException.Message);     } } 

The statement files for UpdateTitle and DeleteTitle can then be written as follows:

 <?xml version="1.0" encoding="utf-8" ?> <DataFactory>     <Statement name="UpdateTitle" type="Text">         <Sql>UPDATE Titles SET Description=@desc WHERE isbn = @isbn</Sql>         <Parameters>             <Param name="isbn" SqlName="@isbn" type="string"               maxLength="10" direction="Input" />             <Param name="desc" SqlName="@desc" type="string"               maxLength="250" direction="Input" />         </Parameters>     </Statement> </DataFactory> <?xml version="1.0" encoding="utf-8" ?> <DataFactory>     <Statement name="DeleteTitle" type="Text">         <Sql>DELETE FROM Titles WHERE ISBN=@isbn</Sql>         <Parameters>             <Param name="isbn" SqlName="@isbn" type="string"               maxLength="10" direction="Input" />             <Param name="returnVal" SqlName="@RETURN_VALUE" type="integer"               maxLength="4" direction="ReturnValue" />         </Parameters>     </Statement> </DataFactory> 
for RuBoard


Sams Teach Yourself Ado. Net in 21 Days
Sams Teach Yourself ADO.NET in 21 Days
ISBN: 0672323869
EAN: 2147483647
Year: 2002
Pages: 158
Authors: Dan Fox

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net