Workshop

for RuBoard

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

Quiz

1:

What is the purpose of creating a base class for your data services tier ?

A1:

Because there is some functionality that will be common across data access classes, both within a particular application and within an organization, it makes sense to implement that functionality once and then reuse it across all your classes. By factoring the common code into an abstract base class, you or other developers can take advantage of implementation inheritance in .NET to reuse core code that handles the common features, such as connections, logging, and exception handling.

2:

Why would you need to create a separate base class for classes that will use Component Services?

A2:

For a object to utilize the services of Component Services, such as distributed transactions, object pooling, and just-in-time activation, the class must ultimately be derived from the ServicedComponent class in the System.EnterpriseServices namespace. Because .NET supports only inheritance from a single base class, your base class needs to inherit from ServicedComponent .

3:

What are some considerations you should be aware of as you design data classes?

A3:

The most fundamental decision regards the granularity and design methods themselves . Typically, your classes will encapsulate more functionality if they are application-centric rather than data-centric. The former approach entails creating methods that map to the functionality of your application rather than mapping only to the data itself. Other considerations include using naming conventions and constructors and taking advantage of overloading in your classes.

4:

Why might you use a provider factory class in your data services tier?

A4:

A provider factory class can be used to offload the creation of provider-specific objects into a separate class that your data access classes can rely on. This allows the decision as to which provider to use to be delayed until runtime. This works by programming to the interfaces that all providers support rather than the concrete classes themselves.

Exercise

Q1:

Augment the Books class shown in Listing 17.4 to add a GetReviews method that retrieves the reviews associated with a particular ISBN using the usp_GetReviews stored procedure and the ProviderFactory class shown in Listing 17.5.

A1:

As with the GetTitles method, one possible solution is to create a method that uses the CreateDataAdapter and CreateParameter methods of the ProviderFactory class to create the appropriate objects and fill a DataSet , as shown in the following code:

 Public Function GetReviews(ByVal isbn As String) As DataSet   Dim da As IDbDataAdapter   Dim reviewDs As New DataSet()   da = _pf.CreateDataAdapter("usp_GetReviews", MyBase.Con)   da.SelectCommand.CommandType = CommandType.StoredProcedure   ' Validate   If isbn Is Nothing OrElse isbn.Length = 0 Then     Throw New ArgumentNullException("isbn", "Cannot be null or empty")     Return Nothing   End If   Try     da.SelectCommand.Parameters.Add( _       _pf.CreateParameter("@isbn", DbType.String))     da.SelectCommand.Parameters(0).Value = isbn     da.Fill(reviewDs)     Return reviewDs   Catch e As Exception     da = Nothing     reviewDs = Nothing     Call MyBase.ThrowComputeBookException("GetReviews Failed on ISBN " & isbn, e)   Finally     If Not MyBase.Con Is Nothing Then MyBase.Con.Close()   End Try  End Function 
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